REACTIVE PROGRAMMING WITH SPRING WEBFLUX
REACTIVE PROGRAMMING
Recently we are frequently hearing about a new paradigm of coding that is gaining more and more attention, the so mentioned Reactive Programming. The flow of information on the internet regarding what this “hype” is all about is getting noisier, lots and lots of information explaining this model is flowing throughout several technology channels.
Reactive Programming is a model of coding where the communication mainly happens throughout a non-blocking stream of data. This model of programming, turn your code to be “reactive”, reacting to change and not being blocked, as performing operations that read and wait for responses from a database or file. Hence, in this manner, we enter in a mode where we are reacting to events, as data are becoming available. Its principles are based on the Reactive Manifesto.
BACKPRESSURE
One important aspect, worth to mention, regarding this non-blocking processing, is the concept of the backpressure. This is a mechanism that must ensure producers don’t overwhelm consumers. So, for instance, if the consumer can’t handle more than 100 HTTP Requests by second, if eventually, this occurs, the consumer must slow down the flow or even stop for a moment, until the situation gets normalized again.
Hence, in order to deal with an enormous flow of data, this is an important aspect to take into consideration, this way we can ensure the producers won’t overwhelm the consumers.
SPECIFICATION, IMPLEMENTATIONS, LIBRARIES AND SPRING WEBFLUX
Reactive Programming, Reactive Streams, and Reactor. What do they have to do with each other? At the start, as we faced this terms reading about the subject, it might be confused figure out what is the difference between them, as we are always seeing those words being used interchangeably. But, actually, they are not exactly the same thing. So, Just to get a clearer view of them, let’s summarize:
- Reactive Programming: this is the paradigm, the model that dictates and the reason for the existence of the others below
- Reactive Streams: this is a specification that defines how should work an API that implements and follows the Reactive Programming paradigm. By the way, JDK 9 it comes already prepared with it.
- Reactor: this is a Java implementation of the Reactive Streams specification, consequently bring to Java the Reactive Programming model.
Spring WebFlux, is the “reaction” of the Spring for this paradigm to use on web applications. It is a web framework that brings the support for the reactive programming model. The Spring WebFlux it is implemented using the Project Reactor, the library chosen by Spring.
The WebFlux It is not a replacement for the Spring MVC, actually, they can complement each other, working together at the same solution. We are going to use it in our sample to better understand how everything works.
THE PROBLEM AND THE SOLUTION, HANDS ON!
Let’s travel a little bit to the future, not so very far, we are kind of in this future already. Imagine a Highway, those with a heavy traffic, with a flow of vehicles that could reach something like 5.000 vehicles by hour at the highest peak of a day, even more on holidays. In this future, all vehicles are obligated to have installed an RFID that transmits some data calculated by the vehicle’s computer. In some places in this highway, we have receptors installed, receiving all information of the vehicles, like plate number, weight, speed, color, etc.
That information is flowing as a data stream all the time around to a host of...
Read the whole article at... (the source code is at GitHub)
https://ualterazambuja.com/2018/04/22/reactive-programming-with-spring-webflux/