I want to:
- subscribe to multiple endpoints returning Flux and output the messages I receive.
- wait until all messages have been output from all endpoints before continuing.
- avoid processing messages from multiple endpoints "together" (e.g. Flux.zip) because the endpoints will return an uneven number of messages and are not logically connected to each other
- block forever if one or more endpoints generate an infinite number of messages
The following code satisfies 1 and 3 but not 2 and 4:
Stream.of("http://service1.com", "http://service2.com", "http://service3.com")
.forEach(service -> {
webClient.get()
.retrieve()
.bodyToFlux(String.class)
.map(message -> service + ": " + message)
.subscribe(System.out::println);
});
System.out.println("Received all messages");
The line "Received all messages" should not be printed until all endpoints have finished. However, because subscribe
is asynchronous, that line is instead printed almost immediately and my application continues instead of waiting.
What should I do differently?