R - result typepublic class RetryPolicy<R> extends FailurePolicy<RetryPolicy<R>,R>
The handle methods describe when a retry should be performed for a particular failure. The handleResult methods describe when a retry should be performed for a particular result. If multiple handle
or handleResult 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 allows 3 execution attempts max with no delay.
|
| Modifier and Type | Method and Description |
|---|---|
RetryPolicy<R> |
abortIf(BiPredicate<R,? extends Throwable> completionPredicate)
Specifies that retries should be aborted if the
completionPredicate matches the completion result. |
RetryPolicy<R> |
abortIf(Predicate<R> resultPredicate)
Specifies that retries should be aborted if the
resultPredicate matches the result. |
RetryPolicy<R> |
abortOn(Class<? extends Throwable>... failures)
Specifies when retries should be aborted.
|
RetryPolicy<R> |
abortOn(Class<? extends Throwable> failure)
Specifies when retries should be aborted.
|
RetryPolicy<R> |
abortOn(List<Class<? extends Throwable>> failures)
Specifies when retries should be aborted.
|
RetryPolicy<R> |
abortOn(Predicate<? extends Throwable> failurePredicate)
Specifies that retries should be aborted if the
failurePredicate matches the failure. |
RetryPolicy<R> |
abortWhen(R 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 |
canApplyDelayFn(R result,
Throwable failure)
Returns whether any configured delay function can be applied for an execution result.
|
RetryPolicy<R> |
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<R,? 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. |
int |
getMaxAttempts()
Returns the max number of execution attempts to perform.
|
Duration |
getMaxDelay()
Returns the max delay between backoff retries.
|
Duration |
getMaxDuration()
Returns the max duration to perform retries for.
|
int |
getMaxRetries()
Returns the max number of retries to perform when an execution attempt fails.
|
boolean |
isAbortable(R result,
Throwable failure)
Returns whether an execution result can be aborted given the configured abort conditions.
|
RetryPolicy<R> |
onAbort(CheckedConsumer<? extends ExecutionCompletedEvent<R>> listener)
Registers the
listener to be called when an execution is aborted. |
RetryPolicy<R> |
onFailedAttempt(CheckedConsumer<? extends ExecutionAttemptedEvent<R>> listener)
Registers the
listener to be called when an execution attempt fails. |
RetryPolicy<R> |
onRetriesExceeded(CheckedConsumer<? extends ExecutionCompletedEvent<R>> listener)
Registers the
listener to be called when an execution fails and the max retry attempts or max duration are exceeded. |
RetryPolicy<R> |
onRetry(CheckedConsumer<? extends ExecutionAttemptedEvent<R>> listener)
Registers the
listener to be called before an execution is retried. |
PolicyExecutor |
toExecutor(AbstractExecution execution)
Returns an
PolicyExecutor capable of performing an execution in the context of a Policy and handling
results according to the Policy. |
RetryPolicy<R> |
withBackoff(long delay,
long maxDelay,
ChronoUnit chronoUnit)
Sets the
delay between retries, exponentially backing off to the maxDelay and multiplying
successive delays by a factor of 2. |
RetryPolicy<R> |
withBackoff(long delay,
long maxDelay,
ChronoUnit chronoUnit,
double delayFactor)
Sets the
delay between retries, exponentially backing off to the maxDelay and multiplying
successive delays by the delayFactor. |
RetryPolicy<R> |
withDelay(Duration delay)
Sets the
delay to occur between retries. |
RetryPolicy<R> |
withDelay(long delayMin,
long delayMax,
ChronoUnit chronoUnit)
Sets a random delay between the
delayMin and delayMax (inclusive) to occur between retries. |
RetryPolicy<R> |
withDelay(RetryPolicy.DelayFunction<R,? extends Throwable> delayFunction)
Sets the
delayFunction that computes the next delay before retrying. |
<F extends Throwable> |
withDelayOn(RetryPolicy.DelayFunction<R,F> delayFunction,
Class<F> failure)
Sets the
delayFunction that computes the next delay before retrying. |
RetryPolicy<R> |
withDelayWhen(RetryPolicy.DelayFunction<R,? extends Throwable> delayFunction,
R result)
Sets the
delayFunction that computes the next delay before retrying. |
RetryPolicy<R> |
withJitter(double jitterFactor)
Sets the
jitterFactor to randomly vary retry delays by. |
RetryPolicy<R> |
withJitter(Duration jitter)
Sets the
jitter to randomly vary retry delays by. |
RetryPolicy<R> |
withMaxAttempts(int maxAttempts)
Sets the max number of execution attempts to perform.
|
RetryPolicy<R> |
withMaxDuration(Duration maxDuration)
Sets the max duration to perform retries for, else the execution will be failed.
|
RetryPolicy<R> |
withMaxRetries(int maxRetries)
Sets the max number of retries to perform when an execution attempt fails.
|
handle, handle, handle, handleIf, handleIf, handleResult, handleResultIf, isFailureonFailure, onSuccesspublic RetryPolicy()
public RetryPolicy<R> abortIf(BiPredicate<R,? extends Throwable> completionPredicate)
completionPredicate matches the completion result.NullPointerException - if completionPredicate is nullpublic RetryPolicy<R> abortIf(Predicate<R> resultPredicate)
resultPredicate matches the result. Predicate is not
invoked when the operation fails.NullPointerException - if resultPredicate is nullpublic RetryPolicy<R> abortOn(Class<? extends Throwable> failure)
failure will be result in
retries being aborted.NullPointerException - if failure is nullpublic RetryPolicy<R> abortOn(Class<? extends Throwable>... failures)
failures will be result
in retries being aborted.NullPointerException - if failures is nullIllegalArgumentException - if failures is emptypublic RetryPolicy<R> 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<R> abortOn(Predicate<? extends Throwable> failurePredicate)
failurePredicate matches the failure.NullPointerException - if failurePredicate is nullpublic RetryPolicy<R> abortWhen(R result)
result.public boolean allowsRetries()
maxRetries and
maxDuration.withMaxRetries(int),
withMaxDuration(Duration)public boolean isAbortable(R result, Throwable failure)
abortOn(Class...),
abortOn(List),
abortOn(Predicate),
abortIf(BiPredicate),
abortIf(Predicate),
#abortWhen(R)public RetryPolicy<R> onAbort(CheckedConsumer<? extends ExecutionCompletedEvent<R>> listener)
listener to be called when an execution is aborted.public RetryPolicy<R> onFailedAttempt(CheckedConsumer<? extends ExecutionAttemptedEvent<R>> listener)
listener to be called when an execution attempt fails.public RetryPolicy<R> onRetriesExceeded(CheckedConsumer<? extends ExecutionCompletedEvent<R>> listener)
listener to be called when an execution fails and the max retry attempts or max duration are exceeded.public RetryPolicy<R> onRetry(CheckedConsumer<? extends ExecutionAttemptedEvent<R>> listener)
listener to be called before an execution is retried.public boolean canApplyDelayFn(R result, Throwable failure)
public RetryPolicy<R> copy()
public Duration getDelay()
Duration.ZERO.public Duration getDelayMin()
withDelay(long, long, ChronoUnit)public Duration getDelayMax()
withDelay(long, long, ChronoUnit)public RetryPolicy.DelayFunction<R,? extends Throwable> getDelayFn()
Throwable.public double getDelayFactor()
public Duration getJitter()
null if none has been configured.withJitter(Duration)public double getJitterFactor()
0.0 if none has been configured.withJitter(double)public int getMaxAttempts()
-1 represents no limit. Defaults to
3.withMaxAttempts(int),
getMaxRetries()public Duration getMaxDelay()
withBackoff(long, long, ChronoUnit)public Duration getMaxDuration()
withMaxDuration(Duration)public int getMaxRetries()
-1 represents no
limit. Defaults to 2.withMaxRetries(int),
getMaxAttempts()public RetryPolicy<R> withBackoff(long delay, long maxDelay, ChronoUnit chronoUnit)
delay between retries, exponentially backing off to the maxDelay and multiplying
successive delays by a factor of 2.NullPointerException - if chronoUnit 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<R> withBackoff(long delay, long maxDelay, ChronoUnit chronoUnit, double delayFactor)
delay between retries, exponentially backing off to the maxDelay and multiplying
successive delays by the delayFactor.NullPointerException - if chronoUnit 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<R> withDelay(Duration delay)
delay to occur between retries.NullPointerException - if chronoUnit 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<R> withDelay(long delayMin, long delayMax, ChronoUnit chronoUnit)
delayMin and delayMax (inclusive) to occur between retries.NullPointerException - if chronoUnit 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<R> withDelay(RetryPolicy.DelayFunction<R,? 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<R> withDelayOn(RetryPolicy.DelayFunction<R,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 RetryPolicy<R> 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.delayFunction - 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<R> 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(Duration) has already been
calledpublic RetryPolicy<R> withJitter(Duration jitter)
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 jitter is nullIllegalArgumentException - if jitter is <= 0IllegalStateException - if no delay has been configured or withJitter(double) has already been
calledpublic RetryPolicy<R> withMaxAttempts(int maxAttempts)
-1 indicates no limit. This method has the same
effect as setting 1 more than withMaxRetries(int). For example, 2 retries equal 3 attempts.IllegalArgumentException - if maxAttempts is 0 or less than -1withMaxRetries(int)public RetryPolicy<R> withMaxDuration(Duration maxDuration)
NullPointerException - if maxDuration is nullIllegalStateException - if maxDuration is <= the delaypublic RetryPolicy<R> withMaxRetries(int maxRetries)
-1 indicates no limit. This
method has the same effect as setting 1 less than withMaxAttempts(int). For example, 2 retries equal 3
attempts.IllegalArgumentException - if maxRetries < -1withMaxAttempts(int)public PolicyExecutor toExecutor(AbstractExecution execution)
PolicyPolicyExecutor capable of performing an execution in the context of a Policy and handling
results according to the Policy.Copyright © 2019. All rights reserved.