Introduction to Spring Reactor

Reading Time: 3 minutes

In this Spring Reactor blog, we will learn what is reactive programming. Why we need reactive Programming? How we can get started with reactive behaviour in a Spring Boot project and start producing and consuming messages in the same application itself.

blog3

In above figure you can see working model of traditional rest api. Every time when clients send request to application. Each request will be assign to a particular thread and it will interact with database to get response back . It cannot take any more requests from the client. Until it receives the response from the database and returns the response to the client. In real life application we get limited number of thread. Suppose If we have 25 thread we can handle only 25 concurrent request at a time.

And that was the big reason for reactive programming to get into action. let’s see through an example how reactive programming handle this problem .

Reactive Programming

blogs

In reactive programming when a request came to an application it is just assigned to any thread. Now, this thread will go to the database for fetching the data but the advantage here it will not wait to get a response back. It will just send an event to the database. it will also inform the database that just does your task and whenever a response is a ready assign it to any available thread,and publish a complete event.

Now, as you can see here all threads are completly free to take n number of request. In this approach no single thread is getting block, so we can handle tons of concurrent request with a very less number of thread.This is also known as asynchronus non blocking request.

Reactive programming Library

There are three reactive programming library :

  • Project Reactor
  • RxJava
  • Jdk9 Flow Reactive stream

In this blog we would use Project Reactor . It is recommonded library to work with spring boot framework.

In project Reactor there are two datatypes through which we design reactive programming :-

  • Flux
  • Mono

When we start Spring Boot application we add dependency of Spring Web but as we are going to start with Reactive Programming We need to replace this dependency by Spring Reactive Web.

Now we are ready to work with Spring Reactor Let’s see example of mono and flux.

Mono Example :-

package com.Knoldus.Spring_Reactor;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
public class MonoTest {
    @Test
    public void testMono(){
        Mono<String> greetings = Mono.just("Hello").log();
        greetings.subscribe(System.out::println);
    }
}
Output:
13:49:59.619 [main] DEBUG reactor.util.Loggers - Using Slf4j logging framework
13:49:59.634 [main] INFO reactor.Mono.Just.1 - | onSubscribe([Synchronous Fuseable] Operators.ScalarSubscription)
13:49:59.636 [main] INFO reactor.Mono.Just.1 - | request(unbounded)
13:49:59.636 [main] INFO reactor.Mono.Just.1 - | onNext(Hello)
Hello
13:49:59.637 [main] INFO reactor.Mono.Just.1 - | onComplete()

Process finished with exit code 0

Flux Example:-

package com.shivam.Spring_Reactor;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
public class FluxTest {
    @Test
    public void testFlux(){
      Flux<String> intro =  Flux.just("Shivam", "Knoldus","Java-Studio").log();
      intro.subscribe(System.out::println);
    }
}
13:53:03.539 [main] DEBUG reactor.util.Loggers - Using Slf4j logging framework
13:53:03.552 [main] INFO reactor.Flux.Array.1 - | onSubscribe([Synchronous Fuseable] FluxArray.ArraySubscription)
13:53:03.554 [main] INFO reactor.Flux.Array.1 - | request(unbounded)
13:53:03.554 [main] INFO reactor.Flux.Array.1 - | onNext(Shivam)
Shivam
13:53:03.555 [main] INFO reactor.Flux.Array.1 - | onNext(Knoldus)
Knoldus
13:53:03.555 [main] INFO reactor.Flux.Array.1 - | onNext(Java-Studio)
Java-Studio
13:53:03.555 [main] INFO reactor.Flux.Array.1 - | onComplete()

Here Mono and Flux are Publisher so Subscriber needs to call subscribe( ) method of publisher. Once you call subscribe method, Publisher starts emmiting event. Here we are using log( ) method to print each and every execution so that we can verify that Mono and Flux are Following Reactive Stream Workflow or not.

So this is all enough for this blog, to know more about Spring Reactor you can visit this link.

https://projectreactor.io/docs/core/release/api/

Written by 

I'm a Software Consultant at Knoldus . I have completed my B.tech in Computer Science stream from IMS Engineering College, Ghaziabad. I love to explore new technologies and have great interest in problem solving.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading