I'm implementing a retry policy. Basically what I want to do is retry a POST request on a separate thread. I'm using Failsafe (https://failsafe.dev/async-execution/#async-integration) Here is my code
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));
I don't want to catch the IOException here. It is handled by the retry policy. Currently retrying won't happen since I'm catching the exception here. Is there a way to throw an exception from 'supplyAsync' so it will be handled by the retry policy? Thanks. Thanks
CompletionStage API gives several different ways of handling and dealing with unchecked exceptions. But in your case what you get is a Checked exception and you are out of luck. You either have to handle it or throw it outward towards your caller. Here's a one way of doing it, if you prefer the latter approach.
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));