Search code examples
javaspringspring-bootspring-webfluxspring-webclient

How to call several http requests in parallel(with retry functionality) and await when all of them are finish?


For now my code looks like this:

   List<Mono<ResponseEntity<String>>> response = queue.stream()
            .map(req-> webClient
                    .post()
                    .bodyValue(req)
                    .retrieve()
                    .toEntity(String.class)
            )
            .collect(Collectors.toList());

How could I await the moment when all responses will be accepted ?

If some of requests are failed I would like to retry only them.

How could I achieve it?


Solution

  • Rather than going with ExecutorService suggested by another answer, I'd recommend using the capabilities of Mono and Flux which provides a more idiomatic solution:

    Mono<List<String>> response = Flux.fromIterable(queue)
                                      .flatMap(this::callHttp)
                                      .collectList();
    
    private Mono<String> callHttp(String req)
    {
        return webClient
                .post()
                .syncBody(req)
                .retrieve()
                .bodyToMono(String.class)
                .retry(3); // retries failed requests at most 3 times
    }