我在执行重试政策。基本上,我想做的是在一个单独的线程上重试一个POST请求。我正在使用故障安全(https://failsafe.dev/async-execution/#async-integration) --这是我的代码
Failsafe.with(retryPolicy).with(executor).future(() -> CompletableFuture.supplyAsync(() -> {
try {
CloseableHttpResponse response = client.execute(httpPost);
httpPost.releaseConnection();
client.close();
return response;
} catch (IOException e) {
return null;
}
}).thenApplyAsync(response -> "Response: " + response)
.thenAccept(System.out::println));我不想在这里赶上IOException。它由重试策略处理。目前,重新尝试不会发生,因为我在这里捕捉到的例外。是否有方法从“supplyAsync”抛出异常,以便由重试策略处理?谢谢。谢谢
发布于 2018-12-02 14:19:06
CompletionStage API提供了几种处理和处理未经检查的异常的不同方法。但在你的情况下,你得到的是一个检查的例外,你是不走运的。你要么处理它,要么把它向外扔给你的来电者。如果你喜欢后一种方法,这里有一种方法。
Failsafe.with(retryPolicy).with(executor).future(() -> CompletableFuture.supplyAsync(() -> {
try {
// Remainder omitted
return response;
} catch (IOException e) {
throw new CompletionException(e);
}
}).thenApplyAsync(response -> "Response: " + response)
.thenAccept(System.out::println));https://stackoverflow.com/questions/53580754
复制相似问题