How to wait for multiple Flux and Mono publishers to complete concurrently

Viewed 1158

How to chain Flux & Mono Publishers?

I want to chain them both and send response. Basically, execute all asynchronously.

Here, I use zip for Mono & Flux for executing all Mono publishers and Flux publishers.

List<Mono<?>> monoPubs = new ArrayList<>();
Mono.zip(monoPubs, mred -> result);

List<Flux<?>> fluxPubs = new ArrayList<>();
Flux.zip(fluxPubs, fres -> result);
1 Answers

If you don't need the result from the individual publishers, then you can use Mono.when to wait for all publishers to complete:

List<Publisher<?>> publishers = new ArrayList<>(monoPubs);
publishers.addAll(fluxPubs);

Mono.when(publishers)
    .thenReturn(result);

In case you need the results from the publishers, then you can use Flux.merge:

List<Publisher<?>> publishers = new ArrayList<>(monoPubs);
publishers.addAll(fluxPubs);

Flux.merge(publishers);
Related