Subscription-time

The second important phase of the execution life cycle in the stream is the subscription-time. Subscription happens when we subscribe to a given Publisher. For example, the following code demonstrates how to subscribe to the aforementioned execution flow:

... 
filteredFlux.subscribe(...);

As we may remember from previous sections, in order to build an execution flow, we pass Publishers inside each other. So, we have a chain of Publishers. Once, we subscribe to the top wrapper, we start the subscription process of that chain. The following pseudocode shows how a Subscriber is propagated through the chain of Subscribers during the subscription time:

filterFlux.subscribe(Subscriber) {
mapFlux.subscribe(new FilterSubscriber(Subscriber)) {
arrayFlux.subscribe(new MapSubscriber(FilterSubscriber(Subscriber))) {
// start pushing real elements here
}
}
}

The preceding code shows what happens during the subscription-time inside the assembled Flux. As we can see, execution of the filtered Flux.subscribe method subsequently executes the subscribe method for each inner Publisher. Finally, when execution ends at the line with comment, we will have the following sequence of Subscribers wrapped inside each other:

ArraySubscriber(
MapSubscriber(
FilterSubscriber(
Subscriber
)
)
)

In contrast to the assembled Fluxs, here, we have the ArraySubscriber wrapper at the top of a Subscribers pyramid where, in the case of Flux pyramid, we have FluxArray in the middle (reversed pyramid of wrappers).

The importance of subscription-time phase is in the fact that during that phase we may do the same optimization as during assemble-time phase. Another important thing is that some of the operators that enable multi-threading in Reactor allows changing the worker on which subscription happens. We are going to cover subscription-time optimization and multi-threading in this chapter later and for now switching to explanation of the last phase of the stream execution life cycle.

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

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