WebFlux framework - Spring 5

What is Reactive Programming?


In plain terms reactive programming is about non-blocking applications that are asynchronous and event-driven and require a small number of threads to scale vertically (i.e. within the JVM) rather than horizontally (i.e. through clustering).
A key aspect of reactive applications is the concept of backpressure which is a mechanism to ensure producers don’t overwhelm consumers. For example in a pipeline of reactive components extending from the database to the HTTP response when the HTTP connection is too slow the data repository can also slow down or stop completely until network capacity frees up.
Reactive programming also leads to a major shift from imperative to declarative async composition of logic. It is comparable to writing blocking code vs using theCompletableFuture from Java 8 to compose follow-up actions via lambda expressions.
For a longer introduction check the blog series "Notes on Reactive Programming" by Dave Syer.

Reactive API and Building Blocks
Spring Framework 5 embraces Reactive Streams as the contract for communicating backpressure across async components and libraries. Reactive Streams is a specification created through industry collaboration that has also been adopted in Java 9 as java.util.concurrent.Flow.
The Spring Framework uses Reactor internally for its own reactive support. Reactor is a Reactive Streams implementation that further extends the basic Reactive Streams Publisher contract with the Flux and Mono composable API types to provide declarative operations on data sequences of 0..N and 0..1.
The Spring Framework exposes Flux and Mono in many of its own reactive APIs. At the application level however, as always, Spring provides choice and fully supports the use of RxJava. For more on reactive types check the post "Understanding Reactive Types" by Sebastien Deleuze.

Annotation-based Programming Model

The same @Controller programming model and the same annotations used in Spring MVC are also supported in WebFlux. The main difference is that the underlying core, framework contracts — i.e. HandlerMappingHandlerAdapter, are non-blocking and operate on the reactive ServerHttpRequest and ServerHttpResponserather than on the HttpServletRequest and HttpServletResponse. Below is an example with a reactive controller:
public class PersonController {

 private final PersonRepository repository;

 public PersonController(PersonRepository repository) {
  this.repository = repository;

 Mono<Void> create(@RequestBody Publisher<Person> personStream) {
  return this.repository.save(personStream).then();

 Flux<Person> list() {
  return this.repository.findAll();

 Mono<Person> findById(@PathVariable String id) {
  return this.repository.findOne(id);


Beliebte Posts