public class RetryPolicy extends Object
The retryOn methods describe when a retry should be performed for a particular failure. The retryWhen
method describes when a retry should be performed for a particular result. If multiple retryOn or
retryWhen conditions are specified, any matching condition can allow a retry. The abortOn,
abortWhen and abortIf methods describe when retries should be aborted.
| Modifier and Type | Class and Description |
|---|---|
static interface |
RetryPolicy.DelayFunction<R,F extends Throwable>
A functional interface for computing delays between retries in conjunction with
withDelay(DelayFunction). |
| Constructor and Description |
|---|
RetryPolicy()
Creates a retry policy that always retries with no delay.
|
RetryPolicy(RetryPolicy rp)
Copy constructor.
|
| Modifier and Type | Method and Description |
|---|---|
<T> RetryPolicy |
abortIf(BiPredicate<T,? extends Throwable> completionPredicate)
Specifies that retries should be aborted if the
completionPredicate matches the completion result. |
<T> RetryPolicy |
abortIf(Predicate<T> resultPredicate)
Specifies that retries should be aborted if the
resultPredicate matches the result. |
RetryPolicy |
abortOn(Class<? extends Throwable>... failures)
Specifies when retries should be aborted.
|
RetryPolicy |
abortOn(Class<? extends Throwable> failure)
Specifies when retries should be aborted.
|
RetryPolicy |
abortOn(List<Class<? extends Throwable>> failures)
Specifies when retries should be aborted.
|
RetryPolicy |
abortOn(Predicate<? extends Throwable> failurePredicate)
Specifies that retries should be aborted if the
failurePredicate matches the failure. |
RetryPolicy |
abortWhen(Object result)
Specifies that retries should be aborted if the execution result matches the
result. |
boolean |
allowsRetries()
Returns whether the policy allows retries according to the configured
maxRetries and
maxDuration. |
boolean |
canAbortFor(Object result,
Throwable failure)
Returns whether an execution result can be aborted given the configured abort conditions.
|
boolean |
canApplyDelayFn(Object result,
Throwable failure)
Returns whether any configured delay function can be applied for an execution result.
|
boolean |
canRetryFor(Object result,
Throwable failure)
Returns whether an execution result can be retried given the configured abort conditions.
|
RetryPolicy |
copy()
Returns a copy of this RetryPolicy.
|
Duration |
getDelay()
Returns the delay between retries.
|
double |
getDelayFactor()
Returns the delay factor for backoff retries.
|
RetryPolicy.DelayFunction<?,? extends Throwable> |
getDelayFn()
Returns the function that determines the next delay given a failed attempt with the given
Throwable. |
Duration |
getDelayMax()
Returns the max delay between retries.
|
Duration |
getDelayMin()
Returns the min delay between retries.
|
Duration |
getJitter()
Returns the jitter, else
null if none has been configured. |
double |
getJitterFactor()
Returns the jitter factor, else
0.0 if none has been configured. |
Duration |
getMaxDelay()
Returns the max delay between backoff retries.
|
Duration |
getMaxDuration()
Returns the max duration to perform retries for.
|
int |
getMaxRetries()
Returns the max retries.
|
<T> RetryPolicy |
retryIf(BiPredicate<T,? extends Throwable> completionPredicate)
Specifies that a retry should occur if the
completionPredicate matches the completion result and the retry
policy is not exceeded. |
<T> RetryPolicy |
retryIf(Predicate<T> resultPredicate)
Specifies that a retry should occur if the
resultPredicate matches the result and the retry policy is not
exceeded. |
RetryPolicy |
retryOn(Class<? extends Throwable>... failures)
Specifies the failures to retry on.
|
RetryPolicy |
retryOn(Class<? extends Throwable> failure)
Specifies the failure to retry on.
|
RetryPolicy |
retryOn(List<Class<? extends Throwable>> failures)
Specifies the failures to retry on.
|
RetryPolicy |
retryOn(Predicate<? extends Throwable> failurePredicate)
Specifies that a retry should occur if the
failurePredicate matches the failure and the retry policy is not
exceeded. |
RetryPolicy |
retryWhen(Object result)
Specifies that a retry should occur if the execution result equals the
result and the retry policy is not
exceeded. |
RetryPolicy |
withBackoff(long delay,
long maxDelay,
TimeUnit timeUnit)
Sets the
delay between retries, exponentially backing off to the maxDelay and multiplying
successive delays by a factor of 2. |
RetryPolicy |
withBackoff(long delay,
long maxDelay,
TimeUnit timeUnit,
double delayFactor)
Sets the
delay between retries, exponentially backing off to the maxDelay and multiplying
successive delays by the delayFactor. |
RetryPolicy |
withDelay(long delayMin,
long delayMax,
TimeUnit timeUnit)
Sets a random delay between the
delayMin and delayMax (inclusive) to occur between retries. |
RetryPolicy |
withDelay(long delay,
TimeUnit timeUnit)
Sets the
delay to occur between retries. |
RetryPolicy |
withDelay(RetryPolicy.DelayFunction<?,? extends Throwable> delayFunction)
Sets the
delayFunction that computes the next delay before retrying. |
<F extends Throwable> |
withDelayOn(RetryPolicy.DelayFunction<Object,F> delayFunction,
Class<F> failure)
Sets the
delayFunction that computes the next delay before retrying. |
<R> RetryPolicy |
withDelayWhen(RetryPolicy.DelayFunction<R,? extends Throwable> delayFunction,
R result)
Sets the
delayFunction that computes the next delay before retrying. |
RetryPolicy |
withJitter(double jitterFactor)
Sets the
jitterFactor to randomly vary retry delays by. |
RetryPolicy |
withJitter(long jitter,
TimeUnit timeUnit)
Sets the
jitter to randomly vary retry delays by. |
RetryPolicy |
withMaxDuration(long maxDuration,
TimeUnit timeUnit)
Sets the max duration to perform retries for, else the execution will be failed.
|
RetryPolicy |
withMaxRetries(int maxRetries)
Sets the max number of retries to perform.
|
public RetryPolicy()
public RetryPolicy(RetryPolicy rp)
public <T> RetryPolicy abortIf(BiPredicate<T,? extends Throwable> completionPredicate)
completionPredicate matches the completion result.NullPointerException - if completionPredicate is nullpublic <T> RetryPolicy abortIf(Predicate<T> resultPredicate)
resultPredicate matches the result.NullPointerException - if resultPredicate is nullpublic RetryPolicy abortOn(Class<? extends Throwable> failure)
failure will be result in
retries being aborted.NullPointerException - if failure is nullpublic RetryPolicy abortOn(Class<? extends Throwable>... failures)
failures will be result
in retries being aborted.NullPointerException - if failures is nullIllegalArgumentException - if failures is emptypublic RetryPolicy abortOn(List<Class<? extends Throwable>> failures)
failures will be result
in retries being aborted.NullPointerException - if failures is nullIllegalArgumentException - if failures is null or emptypublic RetryPolicy abortOn(Predicate<? extends Throwable> failurePredicate)
failurePredicate matches the failure.NullPointerException - if failurePredicate is nullpublic RetryPolicy abortWhen(Object result)
result.public boolean allowsRetries()
maxRetries and
maxDuration.public boolean canAbortFor(Object result, Throwable failure)
public boolean canRetryFor(Object result, Throwable failure)
public boolean canApplyDelayFn(Object result, Throwable failure)
public RetryPolicy copy()
public Duration getDelay()
Duration.NONE.public Duration getDelayMin()
withDelay(long, long, TimeUnit)public Duration getDelayMax()
withDelay(long, long, TimeUnit)public RetryPolicy.DelayFunction<?,? extends Throwable> getDelayFn()
Throwable.public double getDelayFactor()
public Duration getJitter()
null if none has been configured.withJitter(long, TimeUnit)public double getJitterFactor()
0.0 if none has been configured.withJitter(double)public Duration getMaxDelay()
withBackoff(long, long, TimeUnit)public Duration getMaxDuration()
withMaxDuration(long, TimeUnit)public int getMaxRetries()
100, which retries forever.withMaxRetries(int)public <T> RetryPolicy retryIf(BiPredicate<T,? extends Throwable> completionPredicate)
completionPredicate matches the completion result and the retry
policy is not exceeded.NullPointerException - if completionPredicate is nullpublic <T> RetryPolicy retryIf(Predicate<T> resultPredicate)
resultPredicate matches the result and the retry policy is not
exceeded.NullPointerException - if resultPredicate is nullpublic RetryPolicy retryOn(Class<? extends Throwable> failure)
failure will be retried.NullPointerException - if failure is nullpublic RetryPolicy retryOn(Class<? extends Throwable>... failures)
failures will be retried.NullPointerException - if failures is nullIllegalArgumentException - if failures is emptypublic RetryPolicy retryOn(List<Class<? extends Throwable>> failures)
failures will be retried.NullPointerException - if failures is nullIllegalArgumentException - if failures is null or emptypublic RetryPolicy retryOn(Predicate<? extends Throwable> failurePredicate)
failurePredicate matches the failure and the retry policy is not
exceeded.NullPointerException - if failurePredicate is nullpublic RetryPolicy retryWhen(Object result)
result and the retry policy is not
exceeded.public RetryPolicy withBackoff(long delay, long maxDelay, TimeUnit timeUnit)
delay between retries, exponentially backing off to the maxDelay and multiplying
successive delays by a factor of 2.NullPointerException - if timeUnit is nullIllegalArgumentException - if delay is <= 0 or delay is >= maxDelayIllegalStateException - if delay is >= the maxDuration, if delays have already been set, or if random delays have already been setpublic RetryPolicy withBackoff(long delay, long maxDelay, TimeUnit timeUnit, double delayFactor)
delay between retries, exponentially backing off to the maxDelay and multiplying
successive delays by the delayFactor.NullPointerException - if timeUnit is nullIllegalArgumentException - if delay <= 0, delay is >= maxDelay, or the
delayFactor is <= 1IllegalStateException - if delay is >= the maxDuration, if delays have already been set, or if random delays have already been setpublic RetryPolicy withDelay(long delay, TimeUnit timeUnit)
delay to occur between retries.NullPointerException - if timeUnit is nullIllegalArgumentException - if delay <= 0IllegalStateException - if delay is >= the maxDuration, if random delays have already been set, or if backoff delays have already been setpublic RetryPolicy withDelay(long delayMin, long delayMax, TimeUnit timeUnit)
delayMin and delayMax (inclusive) to occur between retries.NullPointerException - if timeUnit is nullIllegalArgumentException - if delayMin or delayMax are <= 0, or delayMin >=
delayMaxIllegalStateException - if delayMax is >= the maxDuration, if delays have already been set, if backoff delays have already been setpublic RetryPolicy withDelay(RetryPolicy.DelayFunction<?,? extends Throwable> delayFunction)
delayFunction that computes the next delay before retrying.delayFunction - the function to use to compute the delay before a next attemptNullPointerException - if delayFunction is nullRetryPolicy.DelayFunctionpublic <F extends Throwable> RetryPolicy withDelayOn(RetryPolicy.DelayFunction<Object,F> delayFunction, Class<F> failure)
delayFunction that computes the next delay before retrying. Delays will only occur for failures
that are assignable from the failure.F - failure typedelayFunction - the function to use to compute the delay before a next attemptfailure - the execution failure that is expected in order to trigger the delayNullPointerException - if delayFunction or failure are nullRetryPolicy.DelayFunctionpublic <R> RetryPolicy withDelayWhen(RetryPolicy.DelayFunction<R,? extends Throwable> delayFunction, R result)
delayFunction that computes the next delay before retrying. Delays will only occur for results
that equal the result.R - result typedelayFunction - the function to use to compute the delay before a next attemptresult - the execution result that is expected in order to trigger the delayNullPointerException - if delayFunction or result are nullRetryPolicy.DelayFunctionpublic RetryPolicy withJitter(double jitterFactor)
jitterFactor to randomly vary retry delays by. For each retry delay, a random portion of the delay
multiplied by the jitterFactor will be added or subtracted to the delay. For example: a retry delay of
100 milliseconds and a jitterFactor of .25 will result in a random retry delay between
75 and 125 milliseconds.
Jitter should be combined with fixed, random or exponential backoff delays.
IllegalArgumentException - if jitterFactor is < 0 or > 1IllegalStateException - if no delay has been configured or withJitter(long, TimeUnit) has already
been calledpublic RetryPolicy withJitter(long jitter, TimeUnit timeUnit)
jitter to randomly vary retry delays by. For each retry delay, a random portion of the
jitter will be added or subtracted to the delay. For example: a jitter of 100 milliseconds
will randomly add between -100 and 100 milliseconds to each retry delay.
Jitter should be combined with fixed, random or exponential backoff delays.
NullPointerException - if timeUnit is nullIllegalArgumentException - if jitter is <= 0IllegalStateException - if no delay has been configured or withJitter(double) has already been
calledpublic RetryPolicy withMaxDuration(long maxDuration, TimeUnit timeUnit)
NullPointerException - if timeUnit is nullIllegalStateException - if maxDuration is <= the delaypublic RetryPolicy withMaxRetries(int maxRetries)
-1 indicates to retry forever.IllegalArgumentException - if maxRetries < -1Copyright © 2018. All rights reserved.