自学内容网 自学内容网

Springboot 3.x - Reactive programming

一、Preliminary Knowledge

  1. Functional Interface
  2. Lambda expression
  3. Stream API
    1. Intermediate operation
      1. filter:Used to filter elements in a stream
      2. map:One-to-one conversion
      3. flatMap:One-to-many conversion
      4. distinct、sorted、peek、limit、skip、takeWhile…
    2. Terminal operation
      1. collect:toList/toMap/groupingBy

二、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:

  1. 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);
    }
    
  2. 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();
    }
    
  3. 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();
    }
    
  4. 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:

  1. Subscribers Request Data: When a subscriber subscribes to a publisher, it receives a Subscription object. The subscriber calls the request method of the Subscription to request the number of data items it can handle.
  2. 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.
  3. 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:

  1. 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.
  2. Batch Requesting Data: Request data in batches, each time requesting a batch of data items that can be processed.
  3. 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)!