R
- result typepublic class RetryPolicy<R> extends DelayablePolicy<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.
Note: RetryPolicy extends DelayablePolicy
and FailurePolicy
which offer additional configuration.
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 . |
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.
|
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> |
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.
|
computeDelay, getDelayFn, withDelay, withDelayOn, withDelayWhen
handle, handle, handle, handleIf, handleIf, handleResult, handleResultIf, isFailure
onFailure, onSuccess
public 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 null@SafeVarargs public final 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.
Note: Any exceptions that are thrown from within the listener
are ignored.
public RetryPolicy<R> onFailedAttempt(CheckedConsumer<? extends ExecutionAttemptedEvent<R>> listener)
listener
to be called when an execution attempt fails.
Note: Any exceptions that are thrown from within the listener
are ignored.
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.
Note: Any exceptions that are thrown from within the listener
are ignored.
public RetryPolicy<R> onRetry(CheckedConsumer<? extends ExecutionAttemptedEvent<R>> listener)
listener
to be called before an execution is retried.
Note: Any exceptions that are thrown from within the listener
are ignored.
public RetryPolicy<R> copy()
public Duration getDelay()
Duration.ZERO
.public Duration getDelayMin()
withDelay(long, long, ChronoUnit)
public Duration getDelayMax()
withDelay(long, long, ChronoUnit)
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 >= maxDelay
IllegalStateException
- 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
>= delayMax
IllegalStateException
- if delayMax
is >= the maxDuration
, if delays have already been set, if backoff delays have already been setpublic 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 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 withJitter(double)
has already been called or the jitter is greater than
the min configured delaypublic 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 delay
public 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)
Policy
PolicyExecutor
capable of performing an execution in the context of a Policy and handling
results according to the Policy.Copyright © 2020. All rights reserved.