Java 9 Flow API example – Publisher and Subscriber

In previous post, we have general knowledge about Reactive Streams and Java 9 Flow API Components and Behaviour. In this tutorial, we’re gonna look at an example that implements Publisher and Subscriber for reactive programming.

Related Articles:
Java 9 Flow API – Reactive Streams
Java 9 Flow API example – Processor
Java 9 FLow SubmissionPublisher – A Concrete Publisher

I. Technologies

– Java 9
– Eclipse with Java 9 Support for Oxygen (4.7)

II. Project Overview


We will create a Publisher that is subscribed by two Subscribers.
Publisher maintains a list of Subscriptions, each Subscription is correlative to each Subscriber above.
Publisher uses one Subscription to push items to correlative Subscriber by Subscriber::onNext() method.
Subscriber uses Subscription to request items from Publisher by Subscription::request() method.
Publisher defines an Executor for multi-threading. Then request() and onNext() method work asynchronously, producing data to each Subscriber by Subscription is also asynchronous.
– After receiving all items successfully, Subscriber can request new data or cancel Subscription (random).

III. Practice

To understand how Publisher, Subscriber and Subscription behave and way to implementing them, please visit: Java 9 Flow API – Reactive Streams

1. Create implementation of Publisher

2. Create implementation of Subscriber

3. Create Test Class

4. Check Result


Related Posts



Got Something To Say:

Your email address will not be published. Required fields are marked *

*