2

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.

membersound
  • 81,582
  • 193
  • 585
  • 1,120

1 Answers1

1

You need to apply error handling on the inner Monos:

public Mono<List<Response>> aggregate(List<Object> bodies)
{
    return Flux.fromIterable(bodies)
               .flatMap(body -> send(body).onErrorResume(e -> Mono.empty()))
               .collectList();
}

private Mono<Response> send(Object body)
{
    return WEB_CLIENT.post().syncBody(body).retrieve().bodyToMono(Response.class);
}
Martin Tarjányi
  • 8,863
  • 2
  • 31
  • 49