Spring 5: Gets reactive

Spring 5 is going to include support for the reactive programming model [1].

Here’s an example:

import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.reactive.WebClient;
import reactor.core.publisher.Mono;

import static org.springframework.web.client.reactive.ClientWebRequestBuilders.get;
import static org.springframework.web.client.reactive.ResponseExtractors.body;

public class ReactiveController {

    public Mono<String> sayHello() {
        WebClient reactiveHttpClient = new WebClient(new ReactorClientHttpConnector());
        return reactiveHttpClient


Find a working solution at GitHub. It is based on Spring 5 milestone 1 and the current state of the experimental spring-boot-*-web-reactive libraries.

It is much like a normal Spring MVC controller. It is only the return type (Mono) that is different and the fact that the code is non-blocking itself: using the reactive HTTP WebClient.

Clearly, Pivotal believe in this model. And so do Oracle: who will include reactive types into Java 9 [2] that adheres to the Reactive Streams Specification [3]. Vert.x is another framework that is fully based on reactive programming. And there are many other languages and frameworks that supports it. So it seems like this model is here to stay.

Reasons for adapting this model

A rather pervasive promise that I have encountered is that reactive applications can exhibit superior performance. They can scale extremely well – apparently only to be limited by the networking capabilities of the underlying platform.

Thats all fine. But we’re not all Google, Facebook or Twitter. So to be honest I’m pretty satisfied with the juice that I can get out of the typical non-reactive Web application. But surely, there must be other wins too…

So what is it that makes the reactive model compelling ?

I am not going to pretend to be the expert here. But I’ve found these interesting remarks about it [4]:

  1. The scalability of the application is not limited by choices in the application code – nor is it limited by any configuration of the Web container thread pools etc.
  2. Inversion of control: The responsibility of handling the concurrency has been inverted:  it is no longer the developers responsibility to tune it and get it right – it is entirely handled by the underlying platforms networking/buffering capabilities.

In my opinion that sounds really interesting. Imagine to be able to not care about tuning thread pools in the Web container – or to not care about ExecutorService threadpools tuning in the source code! Just leave it to the machinery below.

Potential issues

There’s a flipside to this as well [4]: a reactive application is likely more difficult to troubleshoot. And we can shoot ourselves in the foot if we by accident introduce blocking code (fx blocking RDBMS I/O drivers).

Add to that the fact that currently most traditional Java based integration libraries are blocking: so we will have to be creative to make it tick correctly (fx using the blocking-to-reactive pattern).

What do you think?

Are there any compelling reasons, other than those mentioned in this post, for us to switch from the imperative programming model into the reactive programming model?


[1] Spring 5 Milestone 1 now includes reactive functionality:

[2] Doug Lea’s contribution of reactive types to JDK9:

[3] The Reactive Streams Specification:

[4] Notes from spring.io on reactive programming:

Tagged with: ,
Posted in Spring
3 comments on “Spring 5: Gets reactive
  1. lutzhuehnken says:

    Make sure to also check out Akka HTTP (http://doc.akka.io/docs/akka/current/java/http/index.html) and Akka Streams (http://doc.akka.io/docs/akka/current/java/stream/index.html). Akka has been reactive from the start!

  2. Dog Lea? (This is probably a typo.)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: