I want to send parallel web requests and collect the results also in parallel. Then my method should return the aggregated results.
Sounds simple using spring-webflux
with WebClient
. But how can I actually aggregate the results?
public List<Response> aggregate(List<Object> bodys) {
List<Mono> monos = bodys.stream(body -> send(body)).collect(Collectors.toList());
//TODO how can I get all results in parallel and collect it to a response list?
List<Response> = Flux.merge(monos)...???...collectList(); //pseudeocode
}
private Mono<Response> send(Object body) {
return webClient.post().syncBode(body).bodyToMono(type);
}
What is important: if one of the monos finish with an exception, still the others should be executed and not be aborted.