Building RESTful Web Services with Spring 5(Second Edition)
上QQ阅读APP看书,第一时间看更新

Reactive Core and Streams

Java 8 introduced Reactive Core, which implements the Reactive programming model and is built on top of the Reactive Streams specification, a standard for building Reactive applications. As the lambda syntax gives more flexibility to go for the event-driven approach Java 8 provides the best way to support Reactive. Also, Java's lambda syntax gives us the ability to create and spawn up small and independent asynchronous tasks. One of the main goals of Reactive Streams is to address the problem of back pressure. We will talk more about back pressure in a later section of this chapter.

The main difference between Java 8 Streams and Reactive Streams is that Reactive is a push model, whereas Java 8 Streams focuses on pulling. In Reactive Streams, based on consumer needs and numbers, all events will be pushed to consumers.

Reactive programming model support is Spring 5's best feature since the last release. Also, with the support of the Akka and Play framework, Java 8 provides a better platform for Reactive applications.

Reactor is built on top of the Reactive Streams specification. Reactive Streams is a bundle of four Java interfaces:

  • Publisher
  • Subscriber
  • Subscription
  • Processor

Publisher will publish a stream of data items to the subscribers that registered with the Publisher. Using an executor, the Publisher publishes the items to the Subscriber. Also, Publisher makes sure that the Subscriber method invocations for each subscription are strictly ordered.

Subscriber consumes items only when requested. You can cancel the receiving process any time by using Subscription.

Subscription behaves as a message mediator between the Publisher and the Subscriber.

Processor represents a processing stage, which can include both Subscriber and a Publisher. Processor can initiate back pressure and cancel the subscription, as well.


Reactive Streams is a specification for asynchronous stream processing, which means all events can be produced and consumed asynchronously.