Detailed explanation of reactive stream protocol

background

Everyone should be familiar with stream. Java8 introduces the concept of stream for all collection classes. Elegant chain operation and flow processing logic, I believe people who have used it will love it.

Each data stream has a producer and a consumer. Producers are responsible for generating data, while consumers are responsible for consumption data. If it is a synchronous system, there is no problem with producing one and consuming the other. However, in an asynchronous system, problems will arise.

Because producers cannot perceive the state of consumers, they do not know whether consumers are busy or idle, and whether they have the ability to consume more data.

Generally speaking, the length of data queue is limited. Even if there is no limit, the memory of the system is limited. When too much data is not consumed, it will lead to memory overflow or data can not be processed.

Back pressure is needed at this time.

If the message receiver cannot handle the message, it can notify the message sender that it is under pressure and needs to reduce the load. Back pressure is a message feedback mechanism that enables the system to respond gracefully to the load rather than crash under the load.

The purpose of reactive stream is to manage the stream data exchange of asynchronous services, and enable the receiver to decide the frequency of receiving data independently. Back pressure is an indispensable part of reactive stream.

What is reactive stream

We talked about the role of reactive stream above. We should have a basic understanding of reactive stream. Here we define reactive stream:

Reactive stream is a standard for asynchronous stream processing, which is characterized by non blocking back pressure.

Reactive stream is just a standard. It defines the interfaces, methods and protocols for realizing the minimum interval of non blocking back pressure.

Therefore, reactive stream can be implemented in many ways, not only in Java, but also in other programming languages.

Reactive stream only defines the most basic functions, and each major implementation can expand freely while realizing the basic functions.

At present, the latest java version of reactive stream is 1.0 3. It was released on August 23, 2019. It contains Java API, protocol definition file, test tool set and specific implementation examples.

Learn more about the Java version of reactive stream

Before introducing the Java version of reactive stream, let's review what reactive stream needs to do:

In order to realize these four functions, reactive stream defines four interfaces: publisher, subscriber, subscription and processor. These four interfaces are actually an implementation of the observer pattern. Next, let's analyze the functions and conventions of each interface in detail.

Publisher

First look at the definition of Publisher:

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

Publisher is used to generate messages. It defines a subscribe method and passes in a subscriber. This method is used to connect publisher and subscriber.

A publisher can connect multiple subscribers.

Every time you call subscribe to establish a connection, a new subscription will be created. The subscription and subscriber correspond to each other one by one.

A subscriber can subscribe to publisher only once.

If the subscription fails or is rejected, it will start the subscriber Onerror (throwable) method.

Subscriber

Let's first look at the definition of subscriber:

public interface Subscriber<T> {
    public void onSubscribe(Subscription s);
    public void onNext(T t);
    public void onError(Throwable t);
    public void onComplete();
}

The subscriber is the recipient of the message.

The onsubscribe (subscription s) method is triggered when publisher and subscriber establish a connection.

When calling subscription When using the request (long) method, onnext (T) will be triggered. According to the size of the request parameter, onnext will be triggered one or more times.

The onerror (throwable T) or oncomplete () method will be triggered when an exception or end occurs.

Subscription

Let's first look at the definition of subscription:

public interface Subscription {
    public void request(long n);
    public void cancel();
}

Subscription represents the subscribe relationship between one-to-one subscriber and publisher.

Request (long n) means how many events are requested from publisher, which will trigger subscriber Onnext method.

Cancel () requests publisher to stop sending information and clear resources.

Processor

Let's first look at the definition of processor:

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

Processor is both subscriber and publisher, which represents a processing state.

Implementation of reactive stream in JDK

Java. Java in JDK util. concurrent. Flow is an implementation of reactive stream semantics.

Flow has existed since jdk9. Let's look at its structure:

From the above figure, we can see that flow is a final class in JDK, and subscriber, publisher, subscription and processor are its internal classes.

We will continue to explain the use of flow in JDK in later articles. Coming soon.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>