Class Retryer
Backoff intervals are configured through chaining upon()
calls. It's critical
to use the new Retryer
instances returned by upon()
. Just remember
Retryer
is immutable.
If the retried operation still fails after retry, the previous exceptions can be accessed
through Throwable.getSuppressed()
.
- Since:
- 2.0
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Represents a delay upon an event of typeE
prior to the retry attempt.static final class
Retries based on return values. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescription<T> Retryer.ForReturnValue
<T> ifReturns
(Predicate<T> condition, List<? extends Retryer.Delay<? super T>> delays) Returns a new object that retries if the return value satisfiescondition
.<T> Retryer.ForReturnValue
<T> ifReturns
(Predicate<T> condition, Stream<? extends Retryer.Delay<? super T>> delays) Returns a new object that retries if the return value satisfiescondition
.<T> CompletionStage
<T> retry
(CheckedSupplier<T, ?> supplier, ScheduledExecutorService executor) Invokes and possibly retriessupplier
upon exceptions, according to the retry strategies specified withupon()
.<T> CompletionStage
<T> retryAsync
(CheckedSupplier<? extends CompletionStage<T>, ?> asyncSupplier, ScheduledExecutorService executor) Invokes and possibly retriesasyncSupplier
upon exceptions, according to the retry strategies specified withupon()
.<T,
E extends Throwable>
TretryBlockingly
(CheckedSupplier<T, E> supplier) Invokes and possibly retriessupplier
upon exceptions, according to the retry strategies specified withupon()
.upon
(Class<E> exceptionType, Predicate<? super E> condition, List<? extends Retryer.Delay<? super E>> delays) Returns a newRetryer
that usesdelays
when an exception is instance ofexceptionType
and satisfiescondition
.upon
(Class<E> exceptionType, Predicate<? super E> condition, Stream<? extends Retryer.Delay<? super E>> delays) Returns a newRetryer
that usesdelays
when an exception is instance ofexceptionType
and satisfiescondition
.upon
(Class<E> exceptionType, List<? extends Retryer.Delay<? super E>> delays) Returns a newRetryer
that usesdelays
when an exception is instance ofexceptionType
.upon
(Class<E> exceptionType, Stream<? extends Retryer.Delay<? super E>> delays) Returns a newRetryer
that usesdelays
when an exception is instance ofexceptionType
.<T> Retryer.ForReturnValue
<T> uponReturn
(T returnValue, List<? extends Retryer.Delay<? super T>> delays) Returns a new object that retries if the function returnsreturnValue
.<T> Retryer.ForReturnValue
<T> uponReturn
(T returnValue, Stream<? extends Retryer.Delay<? super T>> delays) Returns a new object that retries if the function returnsreturnValue
.
-
Constructor Details
-
Retryer
public Retryer()Constructs an emptyRetryer
.
-
-
Method Details
-
upon
public final <E extends Throwable> Retryer upon(Class<E> exceptionType, List<? extends Retryer.Delay<? super E>> delays) Returns a newRetryer
that usesdelays
when an exception is instance ofexceptionType
.InterruptedException
is always considered a request to stop retrying. Callingupon(InterruptedException.class, ...)
is illegal. -
upon
public final <E extends Throwable> Retryer upon(Class<E> exceptionType, Stream<? extends Retryer.Delay<? super E>> delays) Returns a newRetryer
that usesdelays
when an exception is instance ofexceptionType
.InterruptedException
is always considered a request to stop retrying. Callingupon(InterruptedException.class, ...)
is illegal. -
upon
public <E extends Throwable> Retryer upon(Class<E> exceptionType, Predicate<? super E> condition, List<? extends Retryer.Delay<? super E>> delays) Returns a newRetryer
that usesdelays
when an exception is instance ofexceptionType
and satisfiescondition
.InterruptedException
is always considered a request to stop retrying. Callingupon(InterruptedException.class, ...)
is illegal. -
upon
public <E extends Throwable> Retryer upon(Class<E> exceptionType, Predicate<? super E> condition, Stream<? extends Retryer.Delay<? super E>> delays) Returns a newRetryer
that usesdelays
when an exception is instance ofexceptionType
and satisfiescondition
.InterruptedException
is always considered a request to stop retrying. Callingupon(InterruptedException.class, ...)
is illegal. -
retryBlockingly
Invokes and possibly retriessupplier
upon exceptions, according to the retry strategies specified withupon()
.This method blocks while waiting to retry. If interrupted, retry is canceled.
If
supplier
fails despite retrying, the exception from the most recent invocation is propagated.- Throws:
E
-
retry
public <T> CompletionStage<T> retry(CheckedSupplier<T, ?> supplier, ScheduledExecutorService executor) Invokes and possibly retriessupplier
upon exceptions, according to the retry strategies specified withupon()
.The first invocation is done in the current thread. Unchecked exceptions thrown by
supplier
directly are propagated unless explicitly configured to retry. This is to avoid hiding programming errors. Checked exceptions are reported through the returnedCompletionStage
so callers only need to deal with them in one place.Retries are scheduled and performed by
executor
.Canceling the returned future object will cancel currently pending retry attempts. Same if
supplier
throwsInterruptedException
.NOTE that if
executor.shutdownNow()
is called, the returnedCompletionStage
will never be done. -
retryAsync
public <T> CompletionStage<T> retryAsync(CheckedSupplier<? extends CompletionStage<T>, ?> asyncSupplier, ScheduledExecutorService executor) Invokes and possibly retriesasyncSupplier
upon exceptions, according to the retry strategies specified withupon()
.The first invocation is done in the current thread. Unchecked exceptions thrown by
asyncSupplier
directly are propagated unless explicitly configured to retry. This is to avoid hiding programming errors. Checked exceptions are reported through the returnedCompletionStage
so callers only need to deal with them in one place.Retries are scheduled and performed by
executor
.Canceling the returned future object will cancel currently pending retry attempts. Same if
supplier
throwsInterruptedException
.NOTE that if
executor.shutdownNow()
is called, the returnedCompletionStage
will never be done. -
ifReturns
public <T> Retryer.ForReturnValue<T> ifReturns(Predicate<T> condition, List<? extends Retryer.Delay<? super T>> delays) Returns a new object that retries if the return value satisfiescondition
.delays
specify the backoffs between retries. -
ifReturns
public <T> Retryer.ForReturnValue<T> ifReturns(Predicate<T> condition, Stream<? extends Retryer.Delay<? super T>> delays) Returns a new object that retries if the return value satisfiescondition
.delays
specify the backoffs between retries. -
uponReturn
public <T> Retryer.ForReturnValue<T> uponReturn(T returnValue, Stream<? extends Retryer.Delay<? super T>> delays) Returns a new object that retries if the function returnsreturnValue
.- Parameters:
returnValue
- The nullable return value that triggers retrydelays
- specify the backoffs between retries
-
uponReturn
public <T> Retryer.ForReturnValue<T> uponReturn(T returnValue, List<? extends Retryer.Delay<? super T>> delays) Returns a new object that retries if the function returnsreturnValue
.- Parameters:
returnValue
- The nullable return value that triggers retrydelays
- specify the backoffs between retries
-