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

JavaObject Oriented ProgrammingProgramming

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
Published on 09-Mar-2020 11:06:32
Advertisements