How to start with your first Spring Reactor Project

Reading Time: 3 minutes

Hi, In this blog, we will introduce Spring Reactor which you can kickstart using the given example and will be able to understand what are Mono and Flux which are basic building blocks of the streams used by Project Reactor. So let’s Begin!

Making the Spring Boot Project with Maven

We will be using one of the many Maven archetypes to create a sample project for our example. To create the project execute the following command in a directory that you will use as a workspace:

Creating a Project

mvn archetype:generate -DgroupId=com.javacodegeeks.example -DartifactId=JCG-BootReactor-Example -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

If you are running maven for the first time, it will take a few seconds to accomplish the generate command because maven has to download all the required plugins and artifacts in order to make the generation task. Once we run this project, we will see the following output and the project will be created:

Once you have created the project, feel free to open it in your favorite IDE. The next step is to add appropriate Maven Dependencies to the project. We will work with the following dependencies in our project:

Spring-boot-starter-web: This dependency marks this project as a Web project and it adds dependencies in order to create controllers and make Web-related classes.
Reactor-bus: This is the dependency that brings all the Reactor related dependencies into the project classpath.
Spring-boot-starter-test: This dependency collects all test-related JARs into the project like JUnit and Mockito.
Here is the pom.xml file with the appropriate dependencies added:

pom.xml

<parent>
 <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.10.RELEASE</version>
  <relativePath/> 
</parent>
 
<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
</properties> 
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>  <dependency>
    <groupId>io.projectreactor</groupId>
    <artifactId>reactor-bus</artifactId>
    <version>2.0.8.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies> 
<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

Producing a stream of Data

In order for an application to be reactive, we must have a constant flow of data to be our first logical step. Without this data, the application will not have anything to react to.

The reactive core provides us with two data types that allow us to generate a stream of data.

  • Flux
  • Mono

Flux

Using Flux we can have a stream of data that can produce 0…n elements.

Flux<Integer> streamOfIntegers = Flux.just(1, 2, 3, 4, 5);

In this case, we have a static stream of 5 elements.

Mono

Using Mono, we can produce a stream of 0..1 element at a time.

Mono<Integer> streamOfIntegers = Mono.just(1);

here we are limited to only 1 element in the produced stream.

Collecting elements

We will use the subscribe() method to collect the data from the stream. See the example given below.

List<Integer> elements = new ArrayList<>();
Flux.just(1, 2, 3, 4, 5)
.log()
.subscribe(elements::add);

assertThat(elements).containsExactly(1, 2, 3, 4, 5);

Flux.just(1, 2, 3, 4, 5)
.log()
.subscribe(new Subscriber<Integer>() {
    @Override
    public void onSubscribe(Subscription s) {
        s.request(Long.MAX_VALUE);
    }

    @Override
    public void onNext(Integer integer) {
      elements.add(integer);
    }

    @Override
    public void onError(Throwable t) {}

    @Override
    public void onComplete() {}

});

With the above example, you can simply get an overview of what are the building blocks of a reactive application using a spring reactor. In the next blog of this series, we will talk about building this application from end to end. Thanks for Reading!

References

https://projectreactor.io/docs/core/release/reference/#getting-started

knoldus

Written by 

Hey, I am a software consultant at Knoldus working on Java and Functional Programming.