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>
- APublisher
is a provider of a potentially unbounded number of sequenced elements, publishing them according to the demand received from itsSubscriber
(s).Subscriber<T>
- Will receive call toSubscriber.onSubscribe(Subscription)
once after passing an instance ofSubscriber
toPublisher.subscribe(Subscriber)
.Subscription
- ASubscription
represents a one-to-one lifecycle of aSubscriber
subscribing to aPublisher
.Processor<T,R>
- A Processor represents a processing stage—which is both aSubscriber
and aPublisher
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
- APublisher
with rx operators that emits 0 to N elements, and then completes (successfully or with an error).Mono
- APublisher
with basic rx operators that emits at most one item via theonNext
signal then terminates with anonComplete
signal (successful Mono, with or without value), or only emits a singleonError
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
- TheObservable
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
- TheFlowable
class that implements the Reactive StreamsPublisher
Pattern and offers factory methods, intermediate operators and the ability to consume reactive dataflows.Flowable
s support backpressure and requireSubscriber
s to signal demand viaSubscription.request(long)
.Single
- TheSingle
class implements the Reactive Pattern for a single value response.Single
behaves similarly toObservable
except that it can only emit either a single successful value or an error (there is noonComplete
notification as there is for anObservable
).Maybe
- TheMaybe
class represents a deferred computation and emission of a single value, no value at all or an exception.Completable
- TheCompletable
class represents a deferred computation without any value but only indication for completion or exception.Completable
behaves similarly toObservable
except that it can only emit either a completion or error signal (there is noonNext
oronSuccess
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 ReactorFlux
, it means there are 0 to many items in the stream. TheMulti
implementsPublisher
interface.Uni
- Similar to the ReactorMono
, but Uni acceptsnull
. 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.