Reactive streams:

In the image above, we can see that if Destination can not deal with incoming data from Source, all future data could be blocked until the existing ones are processed.

Reactive Streams processes an asynchronous stream data across an asynchronous boundary (passing elements on to another thread or thread-pool), and receiving side (Destination) is not forced to buffer arbitrary amounts of data, then buffer overflow will not occur.

Reactive programming:

“Reactive programming is about processing an asynchronous stream of data items, where applications react to the data items as they occur. A stream of data is essentially a sequence of data items occurring over time. This model is more memory efficient because the data is processed as streams, as compared to iterating over the in-memory data.”

The main problem is Handling streams of data—especially “live” data whose volume is not predetermined—requires special care in an asynchronous system. The most prominent issue is that resource consumption needs to be controlled such that a fast data source does not overwhelm the stream destination.

The Flow Api:

Java 9 comes with one handy features called Flow api or reactive api. It has four basic components as below.

Subscription:

Processor:

A component that acts as both a Subscriber and Publisher.

The processor sits between the Publisher and Subscriber, and transforms one stream to another. There could be one or more processor chained together, and the result of the final processor in the chain, is processed by the Subscriber.

We can create one or more Processors in chain which link a Publisher to a Subscriber.

General Flow:

Now look at the diagram below:

Publisher uses its subscribe() method with Subscriber object as input parameter. That Subscriber now subscribes the Publisher.Publisher defines its own Subscription implementation and produces data elements for that Subscription.
When a Subscriber subscribes a Publisher, onSubscribe() method is invoked. Then Subscriber now can use Subscription to link to the Publisher by request(numberItems) or cancel() method.Publisher uses Subscription to invoke Subscriber‘s methods:onNext() if publishing items to the Subscriber asynchronously, normally using an Executor.onComplete() when no more elements are available,onError() if there is a failure.

JavaDeveloperZone is the group of innovative software developers. We are expert in Java JEE and BigData application development. Our contributions will help Java developers and make development journey easy. Feel free to ask any question and suggestion. Always have space for improvement !
We are also providing software application development as service. Contact us