Spring5 already supports the Reactor model, mainly providing developers with Mono and Flux. While Reator follows the standard API of ReactiveStreams,Rxjava implemented the reactive stream library before Reactor, and Spring5 implemented it The framework that implements SpringMVC based on reactor-Netty is named Spring Webflux, which is an asynchronous framework that replaces the traditional Servlet API.

Next, take a look at the standards of Reactive Streams.

  1. Publisher Data provider, available to subscribers

  2. Subscription Data provided by publishers to which a subscriber subscribes

  3. Subscriber Subscribes to the publisher’s data

  4. This interface integrates publisher and subscriber interfaces, so that both publisher and subscriber can use the Processor.

    In the simple test case above, when the subscribe method is executed, the subscriber is actually called to subscribe to the publisher’s element.

    As you can see from Mono’s Subscribe method, the default is to create a LambdaMonoSubscriber, which is actually a wrapper class for a functional interface such as consumer, and then execute the SUBSCRIBE method to pass in the subscriber.

    If there is an optimized action in the publisher, the optimized action is executed, and then finally the publisher subscribe method is executed to initiate a subscription.

    The MonoJust subscribe method is then called, and the onSubscribe method is actually executed by the actual subscriber, which is the ScalarSubscription scalar.

    The Subscription object calls the Request method to send the subscriber the number of elements requested by the Subscription element, the default being the maximum of type Long.

    The element is then subscribed by executing the onNext of the LambdaMonoSubscriber subscriber.

    LambdaMonoSubscriber’s onNext will perform the callback of the lambda function performed by the consumer on the subscription element and print out the corresponding element data. And call onComplete,onError.

    Github has found a sequence diagram of the entire process for you to understand

Today’s summary is mainly about the implementation process of Reactor’s Mono, and the principle behind it will be further understood.