0

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?

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

2 Answers2

1

The simplest and strait-forward solution would be to write a code that sends a single request with retry and returns only after it completed successfully or ran out of the max limit of reties. After that Wrap that code as an Implementation of Runnable and use ExecutorService to submit all of them. Collect the Futures into a collection and check when all of them completed.

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
1

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
}
Martin Tarjányi
  • 8,863
  • 2
  • 31
  • 49