How to start with Spring WebFlux
An introduction to reactive programming realized by Spring
Intro
The project of Spring Webflux was realized and included in the Spring Suite by the version 5.x.x.
The target of this framework is to boost of the developing of reactive programming in Java and to reduce the concurrency reducing the hardware needed.
In this story I’ll try to introduce the idea of reactive programming with a simple example and take an overview of the scenarios where it is possible to use it.
Let’s go!
Reactive programming
The reactive programming is used to realize the non-blocking model of architecture. The web application that decides to use reactive programming needs to realize the logic based on subscribe and events.
The flow of the web application now became asynchronous and react in the response of an event.
The architect and also the developer need to keep in mind these concepts designing the method or the class thinking differently by the “standard synchronous programming”.
The reactive programming is often connected with the functional programming (what do and not how to do) that is included and prerequisite in this context of non-blocking flows.
A simple example
To represent the asynchronous calls we’ll create a REST controller that manages the objects Mono and Flux, included in the Reactor framework and used to transmit single objects (Mono) or lists of objects (Flux).
The client side will be composed of another web application and a WebClient, also included in Web Flux that permits to invoke the REST services and subscribe to wait for their responses.
To realize this example of utilization of Spring Web Flux we start creating a new Java Spring Boot project with Spring Initializr that will include the required dependencies:
Imported the project into Eclipse, we’ll ready to start. The first step will be to enable the utilization of Web Flux in the Spring Context introducing the annotation @EnableWebFlux on the controller:
The second step will be to realize the REST Controller that expose the two example methods, one for Mono and one for Flux:
The class Mono represent a single object to exchange in the communication between server and client, otherwise the class Flux represents a set of one or more objects that can be a push to the caller even with a specific delay.
The target of this class, as well as expose the REST services, is to create a level of decoupling between the reactive framework and the software developed by the lower layers of the application.
In fact the class of repository does not have any connection with the Web Flux framework or with Reactor, for this reason is it possible to adapt this framework also to an existing project (if necessary):
In the repository class, we added a sleep to emulate in the phase of test the delay of the reading of the resources.
The last step will be to create a client Spring Boot application, only with a Controller for simplicity, and in the Controller we will include the Web Client with the calls to the two REST services:
The minimal steps to invoke a reactive REST services will be:
- Initialize the Web Client passing the URL
- Execute the call to the service and predispose to retrieve the body of the response
- Subscribe to the events
The call of the service will be executed concurrently with the execution of the method, while the management of the event in the response will be asynchronous.
We have executed the realized software and we saw that the ‘Finish method’ log was executed before of the retrieving of the events:
The events Mono and Flux have been pushed after 3 e 10 seconds in line with the timeout setting in the repository class.
Utilization scenarios
The utilization of the Web Flux framework is advisable in the context of Java application where it is conceivable the realization of mechanisms of asynchronous exchange of information, for example:
- MOBILE APP: In mobile apps that need to receive asynchronous notify in subscription to events
- WEB APPLICATION: In general, is advisable to use this framework in Web Applications that receive notify on events without block the software execution
- PARALLEL AND DISTRIBUTED COMPUTING: In web applications for parallel computing where is not necessary to block the execution of the main program, but to collect the notifies after the completion of the invoked services
- MESSAGE DRIVEN APPLICATION: In applications when the execution is driven by the exchange of messages
- OTHERS?
This new framework clearly simplifies the paradigm of subscribe and notify, especially for REST web services and opens the doors of the Java programming to a new way to think and realize the Web Applications.
Thank you for your time!
Reference: Spring Webflux reference and Reactor
GitHub: project SpringWebFluxExample and SpringWebFluxClientExample