What is the difference between transform
& transformDeferred
in project reactor flux.
Good example will be helpful.
https://projectreactor.io/docs/core/release/reference/index.html#advanced-mutualizing-operator-usage
What is the difference between transform
& transformDeferred
in project reactor flux.
Good example will be helpful.
https://projectreactor.io/docs/core/release/reference/index.html#advanced-mutualizing-operator-usage
Most of the time, a
Flux
is "lazy": you declare a processing pipeline, but data only starts flowing once you subscribe to it. You can subscribe multiple times.This is called a cold
Flux
(and each time you subscribe to a cold source, the source generates its data anew for the benefit of the new subscriber).So we can distinguish:
Flux
instance, returning a newFlux
instancetransform
is a convenience method to apply a set of operators to a givenFlux
. For instance, you want all yourFlux
returned by methods of a service to use.log("serviceName")
, so you externalize this trait in astatic Function<Flux, Flux>
:loggingTrait = f -> f.log("serviceName");`
Now you can apply this trait in all Flux-returning methods of the service via
transform
.It is applied immediately, right at assembly time. Since subscribers come after, they all "share" the same result of the function.
Now imagine you'd like the logging to eg. include the time of subscription, or another piece of data that is more dependent on each individual subscriber.
That's where
transformDeferred
comes in: it defers the application of theFunction
to the moment where the subscription occurs. Plus, it applies theFunction
for EACH subscription.So you could do something like:
And the logs category would be different for each subscriber.