How it works...

To apply more asynchronous approaches in generating Mono<T> and Flux<T>, schedulers are recommended by the Reactive Stream specification to lessen the load of the main thread. With schedulers, operators can be allowed to execute using many threads. Generally, it provides asynchronous boundaries to all threaded operators of the publisher.

There are many ways to generate and use schedulers; one is the use of timed operators like timeout(), delayElements(), and skip() which are showcased in the previous recipe. These types of methods have built-in schedulers that are run in the background once executed. Some are created just to become an argument of a publisher operation just like window() presented in selectNamesScheduler() where the two sub-Flux Streams are managed by separate threads. The use of publishOn() is one way to manage Streams using multiple threading by having different threads for the subscriptions. Subscribers observe all the publisher Streams through the scheduler specified by publishOn(). The Scheduler.newSingle() factory method is used to generate a single thread that will process the needed computations. The location where publishOn() is called matters because it is the point where the rest of the operations will shift to a new scheduler from the main thread.

On the other hand, assigning another thread to the Subscriber's onComplete(), onError(), and onNext() is another popular technique for multithreading which is done through subscribeOn().

Since dispatchers and executors are almost obsolete in this latest version of Reactor Core 3.x, the Scheduler will stand as a dispatcher with an embedded executor that will truly solve major synchronization problems and thread management on the Mono<T> and Flux<T> Streams.

There are other commands from schedulers that can be useful in initiating thread execution such as Schedulers.immediate() which triggers work immediately on the current thread and Schedulers.elastic() which is suited for I/O related tasks which can contain a pool of threads.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.164.141