public class CompletableFutureRetryer extends Object
CompletableFuture
Constructor and Description |
---|
CompletableFutureRetryer(ThreadPoolExecutor executor,
Duration waitBetween)
Util to help run retry
Currently, it only supports fixed-wait-time retry strategy
todo: need to enhance to support other retry strategy in the future.
|
Modifier and Type | Method and Description |
---|---|
<T> CompletableFuture<T> |
withRetries(Supplier<CompletableFuture<T>> attempter,
Predicate<Throwable> shouldRetry,
int attempts)
Compose a
CompletableFuture using the attempter to create the first
attempt and any retries permitted by the shouldRetry predicate. |
public CompletableFutureRetryer(ThreadPoolExecutor executor, Duration waitBetween)
executor
- the excutor to run the taskswaitBetween
- the duration of waiting between attemptspublic <T> CompletableFuture<T> withRetries(Supplier<CompletableFuture<T>> attempter, Predicate<Throwable> shouldRetry, int attempts)
CompletableFuture
using the attempter
to create the first
attempt and any retries permitted by the shouldRetry
predicate. All retries wait
for the waitBetween
before going again, up to a maximum number of attemptsT
- the type of value the future will returnattempter
- produce an attempt as a CompletableFuture
shouldRetry
- determines whether a Throwable
is retryableattempts
- the number of attempts to make before allowing failureCompletableFuture
that runs until success or total failure