Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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
<strong>@FunctionalInterface
</strong>public static interface <strong>Publisher<T></strong> {
public void <strong>subscribe</strong>(<strong>Subscriber</strong><? super T><!--? super T--> subscriber)
}
public static interface <strong>Subscriber<T></strong> {
public void <strong>onSubscribe</strong>(Subscription subscription);
public void <strong>onNext</strong>(T item);
public void <strong>onError</strong>(Throwable throwable);
public void <strong>onComplete</strong>();
}
public static interface <strong>Subscription </strong>{
public void <strong>request</strong>(long n);
public void <strong>cancel</strong>();
}
public static interface <strong>Processor<T, R> </strong>extends <strong>Subscriber<T></strong>, <strong>Publisher<R></strong> {
}
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.
