What are the core interfaces of Reactive Streams in Java 9?


Java 9 has introduced Reactive Streams under java.util.concurrent.Flow package that supports an interoperable publish-subscribe framework. It processes an asynchronous stream of data across the asynchronous boundary (passing elements into another thread or thread-pool), and the receiving side is not forced to buffer arbitrary amounts of data, then buffer overflow can't occur.

Flow API contains four interrelated core interfaces: Publisher, Subscriber, Subscription, and Processor.

Syntax

@FunctionalInterface
public static interface Publisher<T> {
   public void subscribe(Subscriber<? super T> subscriber)
}
public static interface Subscriber<T> {
   public void onSubscribe(Subscription subscription);
   public void onNext(T item);
   public void onError(Throwable throwable);
   public void onComplete();
}
public static interface Subscription {
   public void request(long n);
   public void cancel();
}
public static interface Processor<T, R> extends Subscriber<T>, Publisher<R> {
}

These four interfaces: Flow.Publisher, Flow.Processor, Flow.Subscriber, and Flow. Subscription related to reactive stream specifications. The Publisher interface has subscribe() method, Subscription has cancel() and request() methods and Subscriber has onSubscribe(), onNext(), onError(), and onComplete() methods. The Processor interface implements all methods of Flow. Publisher and Flow.Subscriber interfaces.

raja
raja

e

Updated on: 09-Mar-2020

442 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements