I'm converting small project written in RxJava 1.x to Reactor 3.x. All is good, except that I could not find out how to replace flatMap(Observable::from)
with an appropriate counterpart. I have Mono<List<String>>
and I need to convert it to Flux<String>
.
2.1. To accomplish the transformation, we would use fromIterable method along with flatMapMany . The fromIterable method of Flux returns FluxIterable that can iterate each item in the provided Iterable. For example, the fromIterable takes the Mono list as input and returns a FluxIterable . Flux<String> flux = Flux.
First, we'll extract collection items from stream publisher Mono and then produce items one by one asynchronously as Flux. The Mono publisher contains a map operator, which can transform a Mono synchronously, and a flatMap operator for transforming a Mono asynchronously.
Using block() is actually the only way to get the object from a Mono when it is emitted. However, you should avoid it at all costs because it defeats the purpose of using the reactive stack given that it actually blocks the thread waiting for the object to be emitted from the Mono .
A Flux object represents a reactive sequence of 0.. N items, while a Mono object represents a single-value-or-empty (0..1) result. This distinction carries a bit of semantic information into the type, indicating the rough cardinality of the asynchronous processing.
In Reactor 3, the from
operator has been specialized into a few variants, depending on the original source (array, iterable, etc...).
Use yourMono.flatMapMany(Flux::fromIterable)
in your case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With