An introduction to Spring WebFlux

Reactive or Reactive Streams is a hot topic in these years, you can see it in blog entries, presentations, or some online course.

What is Reactive Streams?

The following is extracted from the official Reactive Streams website:

Reactive Streams is an initiative to provide a standard for asynchronous stream processing with non-blocking back pressure. This encompasses efforts aimed at runtime environments (JVM and JavaScript) as well as network protocols.

Currently, the JVM specification is completed, it includes a Java API(four simple interfaces), a textual Specification, a TCK and implementation examples.

There are 4 core components provided in ReactiveStreams JVM specification.

  • Publisher<T> - A Publisher is a provider of a potentially unbounded number of sequenced elements, publishing them according to the demand received from its Subscriber(s).
  • Subscriber<T> - Will receive call to Subscriber.onSubscribe(Subscription) once after passing an instance of Subscriber to Publisher.subscribe(Subscriber).
  • Subscription - A Subscription represents a one-to-one lifecycle of a Subscriber subscribing to a Publisher.
  • Processor<T,R> - A Processor represents a processing stage—which is both a Subscriber and a Publisher and obeys the contracts of both.

More info, please check Reactive Streams for JVM project and API docs.

Reactor and RxJava 2/3 have implemented this specification, and Java 9 has already adopted it in the new Flow API.

Reactor

The Reactor project team works close to the Spring Team, and Reactor is the default ReactiveStreams implementation supported in Spring WebFlux.

In Reactor, there are two reactive types implements Publisher interface.

  • Flux - A Publisher with rx operators that emits 0 to N elements, and then completes (successfully or with an error).
  • Mono - A Publisher with basic rx operators that emits at most one item via the onNext signal then terminates with an onComplete signal (successful Mono, with or without value), or only emits a single onError signal (failed Mono).

RxJava 2 and RxJava 3

RxJava 2 and 3 APIs are similar, the difference is RxJava 2 targets Java 6 and RxJava 3 is rewritten in Java 8. RxJava 2 is end of life, and for new projects RxJava 3 is preferred.

In RxJava, there are some terminologies.

  • Observable - The Observable class is the non-backpressured, optionally multi-valued base reactive class that offers factory methods, intermediate operators and the ability to consume synchronous and/or asynchronous reactive dataflows.
  • Flowable - The Flowable class that implements the Reactive Streams Publisher Pattern and offers factory methods, intermediate operators and the ability to consume reactive dataflows. Flowables support backpressure and require Subscribers to signal demand via Subscription.request(long).
  • Single - The Single class implements the Reactive Pattern for a single value response. Single behaves similarly to Observable except that it can only emit either a single successful value or an error (there is no onComplete notification as there is for an Observable).
  • Maybe - The Maybe class represents a deferred computation and emission of a single value, no value at all or an exception.
  • Completable - The Completable class represents a deferred computation without any value but only indication for completion or exception. Completable behaves similarly to Observable except that it can only emit either a completion or error signal (there is no onNext or onSuccess as with the other reactive types).

Flow API in Java 9+

Since Java 9, Java platform adds an java.util.concurrent.Flow interface which repackage all interfaces in Reactive Streams for JVM and a Flow.Publisher implementation class - SubmissionPublisher .

Java 11 adds a new reactive Http Client API which is based on the Flow APIs. It frees you from using other 3rd party HttpClient libraries, such as Apache HttpClients, OkHttp, etc. when interacts with remote HTTP APIs.

SmallRye Mutiny

Since Spring Boot 2.5.5/Spring Framework 5.3.10, SmallRye Mutiny is suppported.

SmallyRye Mutiny is an Reactive Streams implementation from RedHat.

There are two core types in the SmallRye Mutiny.

  • Multi - Similar to the Reactor Flux, it means there are 0 to many items in the stream. The Multi implements Publisher interface.
  • Uni - Similar to the Reactor Mono, but Uni accepts null. NOTE: Uni does not implements Publisher.

Spring WebFlux

The Spring embraces Reactive Streams in the new 5.x era

For Spring developers, it brings a complete new programming model.

  • Spring added a new spring-webflux module in it is core framework, and provided built-in reactive programming support via Reactor and RxJava 2/3(RxJava 1 support is removed in the latest Spring 5.3).
  • Spring Security 5 also added reactive feature.
  • Spring supports RSocket which a new bi-direction messaging protocol.
  • In Spring Data umbrella projects, a new ReactiveSortingRepository interface is added in Spring Data Commons. Redis, Mongo, Cassandra subprojects firstly got reactive supports. For RDBMS, Spring created R2dbc sepc and R2dbc is part of Spring since 5.3.
  • Spring Session also began to add reactive features, an reactive variant for its SessionRepository is included since 2.0.
  • Spring Integration added flux message channel and reactive programming APIs.