Reactive Programming with Reactor 3
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Other Operations
Description
In this section, we'll have a look at a few more useful operators that don't fall into the broad categories we explored earlier. Reactor 3 contains a lot of operators, so don't hesitate to have a look at the Flux and Mono javadocs as well as the reference guide to learn about more of them.
Practice
In the first exercise we'll receive 3 Flux<String>. Their elements could arrive with
latency, yet each time the three sequences have all emitted an element, we want to combine
these 3 elements and create a new User. This concatenate-and-transform operation is
called zip:
If you have 3 possible Mono sources and you only want to keep the one that emits its value the fastest,
you can use the firstWithValue static method:
Flux also has the firstWithValue static method. Only the first element emitted by each Flux
is considered to select the fastest Flux (which is then mirrored in the output):
Sometimes you're not interested in elements of a Flux<T>. If you want to still keep a
Flux<T> type, you can use ignoreElements(). But if you really just want the completion,
represented as a Mono<Void>, you can use then() instead:
Reactive Streams does not allow null values in onNext. There's an operator that allow to
just emit one value, unless it is null in which case it will revert to an empty Mono.
Can you find it?
Similarly, if you want to prevent the empty Mono case by falling back to a different one,
you can find an operator that does this switch:
Sometimes you want to capture all values emitted by Flux into separate List.
In this case you can use collectList operator that would return Mono containing that List.
There are more operators belonging to the collect family. You can check them out in Flux documentation.