Introduction to Reactive Streams in Java

Reactive Streams specification

Reactive Streams specification is the foundation for Reactive programming.  It is created by engineers from multiple organizations such as Lightburn, Netflix, Pivotal, etc.

Reactive Streams specification aims to provide a standard for asynchronous stream processing with non-blocking backpressure.

The Reactive Streams specification consists of the following:

  • Publisher
  • Subscriber
  • Subscription
  • Processor


Reactive Streams in Java

We have four Reactive Streams interfaces in Java:

Publisher interface

public interface Publisher<T> {
  void subscribe(Subscriber<? super T> var1);

The Publisher publishes the data to the Subscriber.
It is usually a data source. It can be a database, a remote service, or anything which holds some data.

Subscriber interface

The Subsciber interface has four methods:

public interface Subscriber<T> {

  void onSubscribe(Subscription var1);

  void onNext(T var1);

  void onError(Throwable var1);

  void onComplete();

Subscription interface

The Subscription interface has two methods:

public interface Subscription {

  void request(long var1);

  void cancel();

This interface is the one which connects the Publisher and a Subscriber.

Processor interface

The Processor interface extends the Publisher and a Subscriber.

public interface Processor<T, R> extends Subscriber<T>, Publisher<R> {

The idea behind the Processor is to behave as a Publisher and also as a Subscriber.

Working with the Reative Streams interfaces in Java

To use the Reactive Streams interfaces in Java you need the reactive-streams library.

Let’s see how these components work together:

reactive streams in java

Here, the flow starts with the Subscriber initiating the request by invoking the subscribe() method of the Publisher. (1) Then, the Publisher sends out the Subscription object by invoking the onSubscribe() method of the Subscriber (2). When the Subscriber receives the Subscription object, it will invoke the request() function of the Publisher requesting the Publisher to send the data (3). As the parameter to the method, we specify how much data we want to receive.

The Publisher starts to send data concurrently, as soon as it is available, in the form of a stream of events by invoking the onNext() function of the Subscriber interface. (4, 5, 6).

Once all data is sent, the Publisher invokes the onComplete() method (7) of the Subscriber, and the whole process ends.

In this case, the Publisher invokes all methods from the Subscriber except the onError().  The onError() function will be called in case of an error.


reactive streams - onError()

Here, the Publisher started sending data, but an error occurred, and the exception got sent using the onError() method of the Subscriber.

In Reactive Programming, exceptions are treated the same way as the data, which means that the exception will be sent as an event to the Subscriber.

That’s it!

Proceed to the next lesson.

Happy coding!

Leave a Reply

Your email address will not be published.