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;

@RestController
public class ReactiveController {

    @RequestMapping("/reactive-controller")
    public Mono<String> sayHello() {
        WebClient reactiveHttpClient = new WebClient(new ReactorClientHttpConnector());
        return reactiveHttpClient
                .perform(get("http://localhost:8080/traditional-controller").accept(MediaType.TEXT_PLAIN))
                .extract(body(String.class));

    }
}

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?

References

[1] Spring 5 Milestone 1 now includes reactive functionality:
https://spring.io/blog/2016/07/28/reactive-programming-with-spring-5-0-m1

[2] Doug Lea’s contribution of reactive types to JDK9:
http://hg.openjdk.java.net/jdk9/dev/jdk/file/tip/src/java.base/share/classes/java/util/concurrent/Flow.java

[3] The Reactive Streams Specification:
http://www.reactive-streams.org/

[4] Notes from spring.io on reactive programming:
https://spring.io/blog/2016/06/07/notes-on-reactive-programming-part-i-the-reactive-landscape
https://spring.io/blog/2016/06/13/notes-on-reactive-programming-part-ii-writing-some-code
https://spring.io/blog/2016/07/20/notes-on-reactive-programming-part-iii-a-simple-http-server-application

Latest Comments

  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. Tasos Zervos (@TasosZervos) says:

    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