membersound
membersound

Reputation: 86747

How to collect results from parallel web requests with Spring WebClient?

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.

Upvotes: 2

Views: 1611

Answers (1)

Martin Tarj&#225;nyi
Martin Tarj&#225;nyi

Reputation: 9947

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);
}

Upvotes: 1

Related Questions