Springboot 3.x - Reactive programming
一、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.
Core Concepts
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 inherits from both the Subscriber and Publisher interfaces, indicating that it can act as both a consumer and producer of data. public interface Processor<T, R> extends Subscriber<T>, Publisher<R> { }
Backpressure Mechanism and How It Works
Backpressure is an important concept in Reactive Streams, allowing subscribers to control the rate at which they receive data from publishers to prevent publishers from producing data too quickly for subscribers to handle. The backpressure mechanism is implemented through the request
method in the Subscription interface, where subscribers can call this method to specify the number of data items they are able to handle.
The key to the backpressure mechanism lies in the Subscription
interface, which provides two methods: request(long n)
and cancel()
. The main workflow of backpressure is as follows:
- Subscribers Request Data: When a subscriber subscribes to a publisher, it receives a
Subscription
object. The subscriber calls therequest
method of theSubscription
to request the number of data items it can handle. - Publishers Respond to Requests: Based on the subscriber’s request, the publisher sends the specified number of data items to the subscriber. If the subscriber does not request new data items, the publisher will not send data.
- Dynamically Adjusting the Requested Quantity: Subscribers can dynamically adjust the number of requested data items based on their processing capabilities to avoid data accumulation.
Backpressure Strategies
In practice, different backpressure strategies can be adopted based on specific needs:
- Directly Requesting All Data: If the consumer can handle all the data, it can request all the data at once, but this may lead to high memory usage.
- Batch Requesting Data: Request data in batches, each time requesting a batch of data items that can be processed.
- Requesting Data on Demand: Dynamically adjust the number of requested data items based on the real-time processing capability of the consumer.
Common Implementations of Backpressure Strategies
Different implementations of Reactive Streams provide a variety of built-in backpressure strategies. For example, Project Reactor and RxJava both offer several backpressure strategies:
- Buffer: Buffer all data items until the consumer processes them.
- Drop: Discard new data items, retaining old ones.
- Latest: Only keep the latest data item, discarding old ones.
- Error: Throw an error when exceeding the buffer limit.
原文地址:https://blog.csdn.net/Josh_scott/article/details/140248800
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!