Reactive streams

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.

A few important things to note are as follows:

  • Reactive streams aim to define a minimal set of interfaces, methods, and protocols to enable reactive programming
  • Reactive streams aim to be a language-neutral approach with implementation in the Java (JVM-based) and JavaScript languages
  • Multiple transport streams (TCP, UDP, HTTP, and WebSockets) are supported

Maven dependencies for Reactive Streams are shown as follows:

    <dependency>
<groupId>org.reactivestreams</groupId>
<artifactId>reactive-streams</artifactId>
<version>1.0.0</version>
</dependency>

<dependency>
<groupId>org.reactivestreams</groupId>
<artifactId>reactive-streams-tck</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>

A few of the important interfaces defined in Reactive Streams are shown as follows:

    public interface Subscriber<T> {
public void onSubscribe(Subscription s);
public void onNext(T t);
public void onError(Throwable t);
public void onComplete();
}
public interface Publisher<T> {
public void subscribe(Subscriber<? super T> s);
}
public interface Subscription {
public void request(long n);
public void cancel();
}

A few important things to note are as follows:

  • Interface Publisher: Publisher provides a stream of elements in response to the demand received from its Subscriber(s). A Publisher can serve any number of subscribers. The subscriber count might vary with time.
  • Interface Subscriber : Subscriber registers to listen to the stream of events. Subscribing is a two-step process. The first step is calling Publisher.subscribe(Subscriber). The second step involves making a call to Subscription.request(long). Once these steps are completed, the subscriber can start processing notifications using the onNext(T t) method. The onComplete() method signals the end of notifications. Demand can be signaled via Subscription.request(long) whenever the Subscriber instance is capable of handling more.
  • Interface Subscription : Subscription represents the link between one Subscriber and its Publisher. A subscriber can request more data using request(long n). It can cancel the subscription to notifications using the cancel() method.

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset