一、Preliminary Knowledge
- Functional Interface
- Lambda expression
- Stream API
- Intermediate operation
- filter:Used to filter elements in a stream
- map:One-to-one conversion
- flatMap:One-to-many conversion
- distinct、sorted、peek、limit、skip、takeWhile…
- Terminal operation
- collect:toList/toMap/groupingBy
- Intermediate operation
二、Reactor Core
1、Reactive Stream
https://www.reactive-streams.org
Java’s Reactive Streams is a standardized API for asynchronously processing data streams, designed to support backpressure mechanisms to ensure that system resources are not over-consumed due to mismatches in the speeds of producers and consumers during asynchronous data processing. The main goal of Reactive Streams is to provide a compatible and unified API that allows different libraries and frameworks to work seamlessly together, especially when dealing with large-scale data streams.
For more information about the Backpressure mechanism, please visit the article: https://blog.youkuaiyun.com/Josh_scott/article/details/140398863
The Reactive Streams API is primarily composed of the following four core interfaces:
-
Publisher:A publisher, the source that produces the data stream.
// The Publisher interface has only one method, subscribe, which is used to subscribe to the data stream. public interface Publisher<T> { void subscribe(Subscriber<? super T> s); }
-
Subscriber:A subscriber consumes the data stream at the terminal.
public interface Subscriber<T> { // Called when a subscriber subscribes, passing a Subscription object. void onSubscribe(Subscription s); // Called when a new data item is produced. void onNext(T t); // Called when an error occurs. void onError(Throwable t); // Called when the data stream ends. void onComplete(); }
-
Subscription:A subscription manages the relationship between the publisher and the subscriber, including operations for requesting and canceling data.
public interface Subscription { // Requesting the number of data items. void request(long n); // Canceling the subscription. void cancel(); }
-
Processor<T, R>:A processor is both a publisher and a subscriber, used to process data within the data stream.
// The Processor interface