Search code examples
javaspring-bootmonospring-webfluxflux

Cascade Flux results


I have two actions in Mono, both returning similar object - Mono action1() Mono action2()

I tried doing - Flux1.blockFirst(); Flux2.blockFirst();

both of the flux are performing the action I want. But one of the requirement is if any action fails then both flux should fail or call action to revert the previous action.


Solution

  • Looks like Flux.zip(flux1, flux2).... is what you're looking for:

    public static <T1,T2> Flux<Tuple2<T1,T2>> zip(Publisher<? extends T1> source1,
                                                  Publisher<? extends T2> source2)
    

    Zip two sources together, that is to say wait for all the sources to emit one element and combine these elements once into a Tuple2. The operator will continue doing so until any of the sources completes. Errors will immediately be forwarded. This "Step-Merge" processing is especially useful in Scatter-Gather scenarios.

    https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#zip-org.reactivestreams.Publisher-org.reactivestreams.Publisher-

    There are various overloads with different number of publishers to combine and possible specification of a combinator function.

    Also, some other ways to combine publishers are described here: https://www.baeldung.com/reactor-combine-streams