We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Here is a Spring Retry example:
RetryTemplate template = RetryTemplate.builder() .maxAttempts(3) .fixedBackoff(1000) .retryOn(RemoteAccessException.class) .build(); template.execute(ctx -> { // ... do something });
It is easy to implement retry for CompletableFuture.
CompletableFuture
@Slf4j class RetryNAttemptsDemo { public static void main(String[] args) { // return succeed result by 4th try var times = new AtomicInteger(); Supplier<Integer> task = () -> { if (times.getAndIncrement() < 3) { throw new RuntimeException("error result"); } else { return 42; } }; // using retry method retry(4, () -> supplyAsync(task)) .thenAcceptAsync(r -> log.info("success result: {}", r)) .exceptionallyAsync(throwable -> { log.error("final result", throwable); return null; }) .join(); } public static <T> CompletableFuture<T> retry(int attempts, Supplier<CompletionStage<T>> supplier) { var result = new CompletableFuture<T>(); retryNAttempts(result, attempts, supplier); return result; } private static <T> void retryNAttempts(CompletableFuture<T> result, int attempts, Supplier<CompletionStage<T>> supplier) { supplier.get() .thenAccept(result::complete) .exceptionally(throwable -> { if (attempts > 0L) { log.warn("retrying"); retryNAttempts(result, attempts - 1, supplier); } else { log.error("retry failed", throwable); result.completeExceptionally(throwable); } return null; }); } }
but it is better to add static utility method for CompletableFuture with less bug and multiple retry strategy support.
There are 3 types of strategies for retrying:
Goal:
retry(Retry retry, Supplier(CompletionStage<T>) supplier) : CompetionStage<T>
The text was updated successfully, but these errors were encountered:
@linzee1 Thanks for your professional issue 👍
It'll take some time to understand and feedback. 💕
more info about retry see the author's article 重试机制与CompletableFuture拓展. 🆙
Sorry, something went wrong.
Mature resilience4j fault tolerance library provides retry with first-class support of CompletionStage/CompletableFuture.
resilience4j
retry
CompletionStage
It's recommended to use resilience4j retry rather than re-implementing in cffu.
resilience4j retry
cffu
oldratlee
Successfully merging a pull request may close this issue.
Here is a Spring Retry example:
It is easy to implement retry for
CompletableFuture
.but it is better to add static utility method for CompletableFuture with less bug and multiple retry strategy support.
There are 3 types of strategies for retrying:
Goal:
retry(Retry retry, Supplier(CompletionStage<T>) supplier) : CompetionStage<T>
The text was updated successfully, but these errors were encountered: