Recently, I decided to try spring 5 with projectreactor.io (io.projectreactor:reactor-test:jar:3.1.1).
Does anyone know what the best case of using this functions? What cons and pros of using each of them and where they should be used?
Good examples will be helpful.
I found the example in reference documentation little difficult to follow
So made the below programs to wrap my head around the tranform vs compose concept.
Transform
The operator is applied at the time of instantiation of the flux.
fnstatefull will behave same way for both subscribers below.
Output
Compose
The operator is applied at the time of subscription to the flux.
fnstatefull will behave differently for each subscriber below.
Output
You have two broadly different categories of operators here:
Operators that work on the
Flux
itselftransform
andcompose
are for code mutualizationWhen you compose chains of operators regularly and you have common operator usage patterns in your application, you can mutualize this code or give it a more descriptive name by using
compose
andtransform
.The difference between the two is when the mutualized operators are applied:
transform
applies them at instantiation, whilecompose
applies them at subscription (allowing for dynamic choice of the added operators).Have a look at the reference documentation for more details and examples.
as
This is a convenience shortcut to apply a
Function
to the wholeFlux
while keeping the whole code in a fluent style. An example would be to convert to aMono
(as shown in the javadoc), but it can also helps with external operators that are implemented in a factory method style.Take
reactor-addons
MathFlux
for example, and compare:To:
(this can help you deal with the fact that, unlike Kotlin, Java doesn't have extension methods :) )
Operator that works on the data that goes through the
Flux
map
is all about the data. It applies a 1-1 transformation function to each element in the source, as they become available.In the MathFlux example above,
map
is successively used to add 2 to each original integer, then again to multiply each number in the sequence by 10, then a third time at the end to produce aString
out of each sum.