现在我的代码如下所示:
List<Mono<ResponseEntity<String>>> response = queue.stream()
.map(req-> webClient
.post()
.bodyValue(req)
.retrieve()
.toEntity(String.class)
)
.collect(Collectors.toList());
我如何才能等待所有回复都被接受的那一刻?
如果某些请求失败,我只想重试它们。
我怎样才能实现它?
最佳答案
我建议使用 Mono
和 Flux
的功能,而不是使用另一个答案建议的 ExecutorService
,它提供了更惯用的方式解决办法:
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
}
关于java - 如何并行调用多个http请求(具有重试功能)并等待所有请求完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58913196/