Intro to Spring Reactor: Part 1

Reading Time: 2 minutes

The world is a stage where all of us are artists and Constant learning is the foundation of success.
In order to continue your learning with something new, here were are going to learn about Spring Reactor.

Let’s start with the basics about what is Reactive Programming first.

Reactive Programming

It’s important to understand the difference between reactive programming and reactive systems. We use both these terms quite often and easily misunderstand one for the other.
Reactive systems are a result of a specific architectural style.
On other hand, reactive programming is a programming paradigm where the focus is on developing asynchronous and non-blocking components.
The core of reactive programming is a data stream that helps in leading to a non-blocking execution and hence to better scalability with fewer threads of execution.

Reactive Streams

It was created to define a set of interfaces, methods, and protocols that can describe the necessary operations and entities.

Spring Reactor

With the uprise of Microservices, the necessity of asynchronous communication between the involved services became a mainstream requirement. asynchronous communication is also desirable for time-consuming requests within the same application as well. This is where the actual use-case of the Spring Reactor.

From the background of non-reactive Java development, going reactive can be quite a steep learning curve. When comparing it to the Java 8 Stream API it becomes more challenging, as they could be mistaken for being the same high-level abstractions.

Let’s look into the Spring Reactor components one by one.

Reactor in the JVM

The Reactor, as stated by Spring itself, is a foundational framework for asynchronous applications on the JVM which on modest hardware, makes it possible to process over 15,000,000 events per second with the fastest non-blocking Dispatcher.

Maven Dependencies

<dependency>
    <groupId>io.projectreactor</groupId>
    <artifactId>reactor-core</artifactId>
    <version>3.4.16</version>
</dependency>

<dependency> 
    <groupId>ch.qos.logback</groupId> 
    <artifactId>logback-classic</artifactId> 
    <version>1.2.6</version> 
</dependency>

Working with Stream of Data

While we are talking about Stream there are basically two procedures that we can think of.
– Producing
– Subscribing

Producing Stream of Data

Spring reactor gave us two ways to produce data:
– Mono
– Flux

Mono

It is a stream of 0,1 elements.
e.g,

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

Flux

It’s a stream that can emit 0..n elements.
e.g,

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

It is almost like Mono, but with more than one element.

Subscribing to a Stream of Data

We use the subscribe() method to collect all the elements in a stream.

List<Integer> elements = new ArrayList<>();

Flux.just(1, 2, 3, 4)
  .log()
  .subscribe(elements::add);

The Flow of Elements

The basic flow of code is as below:

  1. onSubscribe() – This is called when we subscribe to our stream. The data won’t start flowing until we subscribe.
  2. request(unbounded) – When we call subscribe, behind the scenes we are creating a Subscription that requests elements from the stream. In this case, it defaults to unbounded, meaning it requests every single element available.
  3. onNext() – This is called on every single element to proceed to the next item.
  4. onComplete() – This is called last, after receiving the last element. There’s actually an onError() as well, which would be called if there is an exception, but in this case, there isn’t.

This is pretty much from the blog, we will see furthermore concepts in future blogs which will contain more coding and details about various components of the Spring reactor.
Also, if you liked the article, please give me a thumbs up and I will keep writing blogs like this for you in the future as well. Keep reading and Keep coding

Reference

knoldus

Written by 

I am a genius middle-class noble philanthropist(by heart) . & Simply a technology enthusiast who loves to share and gain knowledge through blogs.