Event Streaming Using Spring Webflux

1. Spring Webflux Overview

Spring Framework 5 includes a new webflux module for supporting reactive applications. If we want systems that are Responsive, Resilient, Elastic and Message Driven then we need “Reactive Systems” and spring webflux was what you were looking for.

It was created to solve the problem of the need for a non-blocking web stack to handle concurrency with a small number of threads. The term “reactive” refers instead to programming models that are built around reacting to change. In this direction non ­blocking is reactive because instead of being blocked we are now in the mode of reacting to notifications as operations complete or data becomes available.

Spring webflux is fully non-blocking, and supports reactive streams back pressure, and runs on servers such as Netty, Undertow, and Servlet 3.1+ containers.

Reactive web application stack overview

2. About non-blocking

Usually, when a client makes a request for a servlet, I/O on the server is managed by a specific thread taken from a limited pool. If the operation is time extensive or has some Latency, for example, because we are calling another service, the I/O results in a blocked state until we get a response. This could not be a problem in a system with a small load, but can Limit performance on a high scaling one. One solution could be to rise up thread pool size, but this cannot be a good idea, due to increased resources consumption. In Spring WebFLux, and non-bLocking servers in general, it is assumed that applications will not block, and therefore non-blocking servers use a small, fixed- size thread pool (event loop workers) to handle requests.

Server part consists of a RestController that has a route responding to “/temperatures” path using GET HTTP method. Flux is the reactive component that we use to send multiple objects to the client. We can create in many different ways, but for this simple scenario we used the generation starting from a java8 stream.

The delayElements method is used to insert a delay for every item sent to the client.

4.3. Client part

Now Let’s write down a simple spring boot client application that connects to the server and retrieves temperatures from that.

The client makes use of the WebClient class to manage connection with the server part. Once specified url and media type accepted by the client, we can retrieve the flux and subscribe to each event sent on it, in this case printing each element on the console.

5. Conclusions

In this article we have seen how to produce and consume a stream of data in a reactive way using the new Spring Webflux module. All of the code snippets, mentioned in the article, can be found in my Github repository.
If you were looking for a solution on how to write unit and integration tests using spring boot 2 you can read this my other post.