R
- result typepublic class RetryPolicyBuilder<R> extends DelayablePolicyBuilder<RetryPolicyBuilder<R>,RetryPolicyConfig<R>,R> implements PolicyListeners<RetryPolicyBuilder<R>,R>
RetryPolicy
instances.
2
times when any Exception
is thrown, with no delay
between retry attempts.with
configuration methods.handle
conditions. The default exception handling condition will only be overridden by
another condition that handles exceptions such as FailurePolicyBuilder.handle(Class)
or FailurePolicyBuilder.handleIf(CheckedBiPredicate)
.
Specifying a condition that only handles results, such as FailurePolicyBuilder.handleResult(Object)
or
FailurePolicyBuilder.handleResultIf(CheckedPredicate)
will not replace the default exception handling condition.handle
conditions are specified, any condition that matches an execution result or exception
will trigger policy handling.abortOn
, abortWhen
and abortIf
methods describe when retries should be aborted.Note:
DelayablePolicyBuilder
and FailurePolicyBuilder
which offer additional configuration.RetryPolicyConfig
config
Modifier and Type | Method and Description |
---|---|
RetryPolicyBuilder<R> |
abortIf(CheckedBiPredicate<R,? extends Throwable> completionPredicate)
Specifies that retries should be aborted if the
completionPredicate matches the completion result. |
RetryPolicyBuilder<R> |
abortIf(CheckedPredicate<R> resultPredicate)
Specifies that retries should be aborted if the
resultPredicate matches the result. |
RetryPolicyBuilder<R> |
abortOn(CheckedPredicate<? extends Throwable> abortPredicate)
Specifies that retries should be aborted if the
abortPredicate matches the exception. |
RetryPolicyBuilder<R> |
abortOn(Class<? extends Throwable>... exceptions)
Specifies when retries should be aborted.
|
RetryPolicyBuilder<R> |
abortOn(Class<? extends Throwable> exception)
Specifies when retries should be aborted.
|
RetryPolicyBuilder<R> |
abortOn(List<Class<? extends Throwable>> exceptions)
Specifies when retries should be aborted.
|
RetryPolicyBuilder<R> |
abortWhen(R result)
Specifies that retries should be aborted if the execution result matches the
result . |
RetryPolicy<R> |
build()
Builds a new
RetryPolicy using the builder's configuration. |
RetryPolicyBuilder<R> |
onAbort(EventListener<ExecutionCompletedEvent<R>> listener)
Registers the
listener to be called when an execution is aborted. |
RetryPolicyBuilder<R> |
onFailedAttempt(EventListener<ExecutionAttemptedEvent<R>> listener)
Registers the
listener to be called when an execution attempt fails. |
RetryPolicyBuilder<R> |
onRetriesExceeded(EventListener<ExecutionCompletedEvent<R>> listener)
Registers the
listener to be called when an execution fails and the max retry attempts or max duration
are exceeded. |
RetryPolicyBuilder<R> |
onRetry(EventListener<ExecutionAttemptedEvent<R>> listener)
Registers the
listener to be called when a retry is about to be attempted. |
RetryPolicyBuilder<R> |
onRetryScheduled(EventListener<ExecutionScheduledEvent<R>> listener)
Registers the
listener to be called when a retry for an async call is about to be scheduled. |
RetryPolicyBuilder<R> |
withBackoff(Duration delay,
Duration maxDelay)
Sets the
delay between retries, exponentially backing off to the maxDelay and multiplying
consecutive delays by a factor of 2. |
RetryPolicyBuilder<R> |
withBackoff(Duration delay,
Duration maxDelay,
double delayFactor)
Sets the
delay between retries, exponentially backing off to the maxDelay and multiplying
consecutive delays by the delayFactor . |
RetryPolicyBuilder<R> |
withBackoff(long delay,
long maxDelay,
ChronoUnit chronoUnit)
Sets the
delay between retries, exponentially backing off to the maxDelay and multiplying
consecutive delays by a factor of 2. |
RetryPolicyBuilder<R> |
withBackoff(long delay,
long maxDelay,
ChronoUnit chronoUnit,
double delayFactor)
Sets the
delay between retries, exponentially backing off to the maxDelay and multiplying
consecutive delays by the delayFactor . |
RetryPolicyBuilder<R> |
withDelay(Duration delay)
Sets the fixed
delay to occur between retries. |
RetryPolicyBuilder<R> |
withDelay(Duration delayMin,
Duration delayMax)
Sets a random delay between the
delayMin and delayMax (inclusive) to occur between retries. |
RetryPolicyBuilder<R> |
withDelay(long delayMin,
long delayMax,
ChronoUnit chronoUnit)
Sets a random delay between the
delayMin and delayMax (inclusive) to occur between retries. |
RetryPolicyBuilder<R> |
withJitter(double jitterFactor)
Sets the
jitterFactor to randomly vary retry delays by. |
RetryPolicyBuilder<R> |
withJitter(Duration jitter)
Sets the
jitter to randomly vary retry delays by. |
RetryPolicyBuilder<R> |
withMaxAttempts(int maxAttempts)
Sets the max number of execution attempts to perform.
|
RetryPolicyBuilder<R> |
withMaxDuration(Duration maxDuration)
Sets the max duration to perform retries for, else the execution will be failed.
|
RetryPolicyBuilder<R> |
withMaxRetries(int maxRetries)
Sets the max number of retries to perform when an execution attempt fails.
|
withDelayFn, withDelayFnOn, withDelayFnWhen
handle, handle, handle, handleIf, handleIf, handleResult, handleResultIf
onFailure, onSuccess
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
onFailure, onSuccess
public RetryPolicy<R> build()
RetryPolicy
using the builder's configuration.public RetryPolicyBuilder<R> abortIf(CheckedBiPredicate<R,? extends Throwable> completionPredicate)
completionPredicate
matches the completion result. Any
exception thrown from the completionPredicate
is treated as a false
result.NullPointerException
- if completionPredicate
is nullpublic RetryPolicyBuilder<R> abortIf(CheckedPredicate<R> resultPredicate)
resultPredicate
matches the result. Predicate is not
invoked when the operation fails. Any exception thrown from the resultPredicate
is treated as a false
result.NullPointerException
- if resultPredicate
is nullpublic RetryPolicyBuilder<R> abortOn(Class<? extends Throwable> exception)
exception
will be
result in retries being aborted.NullPointerException
- if exception
is null@SafeVarargs public final RetryPolicyBuilder<R> abortOn(Class<? extends Throwable>... exceptions)
exceptions
will be
result in retries being aborted.NullPointerException
- if exceptions
is nullIllegalArgumentException
- if exceptions is emptypublic RetryPolicyBuilder<R> abortOn(List<Class<? extends Throwable>> exceptions)
exceptions
will be
result in retries being aborted.NullPointerException
- if exceptions
is nullIllegalArgumentException
- if exceptions is null or emptypublic RetryPolicyBuilder<R> abortOn(CheckedPredicate<? extends Throwable> abortPredicate)
abortPredicate
matches the exception. Any exception thrown
from the abortPredicate
is treated as a false
result.NullPointerException
- if abortPredicate
is nullpublic RetryPolicyBuilder<R> abortWhen(R result)
result
.public RetryPolicyBuilder<R> onAbort(EventListener<ExecutionCompletedEvent<R>> listener)
listener
to be called when an execution is aborted.
Note: Any exceptions that are thrown from within the listener
are ignored. To provide an alternative
result for a failed execution, use a Fallback
.
NullPointerException
- if listener
is nullpublic RetryPolicyBuilder<R> onFailedAttempt(EventListener<ExecutionAttemptedEvent<R>> listener)
listener
to be called when an execution attempt fails. You can also use onFailure
to determine when the execution attempt fails and and all retries have
failed.
Note: Any exceptions that are thrown from within the listener
are ignored. To provide an alternative
result for a failed execution, use a Fallback
.
NullPointerException
- if listener
is nullpublic RetryPolicyBuilder<R> onRetriesExceeded(EventListener<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. To provide an alternative
result for a failed execution, use a Fallback
.
NullPointerException
- if listener
is nullpublic RetryPolicyBuilder<R> onRetry(EventListener<ExecutionAttemptedEvent<R>> listener)
listener
to be called when a retry is about to be attempted.
Note: Any exceptions that are thrown from within the listener
are ignored. To provide an alternative
result for a failed execution, use a Fallback
.
NullPointerException
- if listener
is nullonRetryScheduled(EventListener)
public RetryPolicyBuilder<R> onRetryScheduled(EventListener<ExecutionScheduledEvent<R>> listener)
listener
to be called when a retry for an async call is about to be scheduled. This method
differs from onRetry(EventListener)
since it is called when a retry is initially scheduled but before any
configured delay, whereas onRetry
is called after a delay, just before the retry
attempt takes place.
Note: Any exceptions that are thrown from within the listener
are ignored. To provide an alternative
result for a failed execution, use a Fallback
.
NullPointerException
- if listener
is nullonRetry(EventListener)
public RetryPolicyBuilder<R> withBackoff(Duration delay, Duration maxDelay)
delay
between retries, exponentially backing off to the maxDelay
and multiplying
consecutive delays by a factor of 2. Replaces any previously configured fixed
or
random
delays.NullPointerException
- if delay
or maxDelay
are nullIllegalArgumentException
- if delay
is <= 0 or delay
is >= maxDelay
IllegalStateException
- if delay
is >= the maxDuration
or delay
is < a configured jitter duration
public RetryPolicyBuilder<R> withBackoff(long delay, long maxDelay, ChronoUnit chronoUnit)
delay
between retries, exponentially backing off to the maxDelay
and multiplying
consecutive delays by a factor of 2. Replaces any previously configured fixed
or
random
delays.NullPointerException
- if chronoUnit
is nullIllegalArgumentException
- if delay
is <= 0 or delay
is >= maxDelay
IllegalStateException
- if delay
is >= the maxDuration
or delay
is < a configured jitter duration
public RetryPolicyBuilder<R> withBackoff(long delay, long maxDelay, ChronoUnit chronoUnit, double delayFactor)
delay
between retries, exponentially backing off to the maxDelay
and multiplying
consecutive delays by the delayFactor
. Replaces any previously configured fixed
or random
delays.NullPointerException
- if chronoUnit
is nullIllegalArgumentException
- if delay
<= 0, delay
is >= maxDelay
, or the delayFactor
is <= 1IllegalStateException
- if delay
is >= the maxDuration
or delay
is < a configured jitter duration
public RetryPolicyBuilder<R> withBackoff(Duration delay, Duration maxDelay, double delayFactor)
delay
between retries, exponentially backing off to the maxDelay
and multiplying
consecutive delays by the delayFactor
. Replaces any previously configured fixed
or random
delays.NullPointerException
- if delay
or maxDelay
are nullIllegalArgumentException
- if delay
<= 0, delay
is >= maxDelay
, or the delayFactor
is <= 1IllegalStateException
- if delay
is >= the maxDuration
or delay
is < a configured jitter duration
public RetryPolicyBuilder<R> withDelay(Duration delay)
delay
to occur between retries. Replaces any previously configured backoff
or random
delays.withDelay
in class DelayablePolicyBuilder<RetryPolicyBuilder<R>,RetryPolicyConfig<R>,R>
NullPointerException
- if delay
is nullIllegalArgumentException
- if delay
<= 0IllegalStateException
- if delay
is >= the maxDuration
or delay
is < a configured jitter duration
public RetryPolicyBuilder<R> withDelay(long delayMin, long delayMax, ChronoUnit chronoUnit)
delayMin
and delayMax
(inclusive) to occur between retries.
Replaces any previously configured fixed
or backoff
delays.NullPointerException
- if chronoUnit
is nullIllegalArgumentException
- if delayMin
or delayMax
are <= 0, or delayMin
>= delayMax
IllegalStateException
- if delayMax
is >= the maxDuration
or
delayMin
is < a configured jitter duration
public RetryPolicyBuilder<R> withDelay(Duration delayMin, Duration delayMax)
delayMin
and delayMax
(inclusive) to occur between retries.
Replaces any previously configured fixed
or backoff
delays.NullPointerException
- if delayMin
or delayMax
are nullIllegalArgumentException
- if delayMin
or delayMax
are <= 0, or delayMin
>= delayMax
IllegalStateException
- if delayMax
is >= the maxDuration
or delay
is < a configured jitter duration
public RetryPolicyBuilder<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. Replaces any previously configured jitter
duration
.
Jitter should be combined with fixed
, random
or exponential backoff
delays. If no delays are configured,
this setting is ignored.
IllegalArgumentException
- if jitterFactor
is < 0 or > 1public RetryPolicyBuilder<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. Replaces any previously
configured jitter factor
.
Jitter should be combined with fixed
, random
or exponential backoff
delays. If no delays are configured,
this setting is ignored.
NullPointerException
- if jitter
is nullIllegalArgumentException
- if jitter
is <= 0IllegalStateException
- if the jitter is greater than the min configured delaypublic RetryPolicyBuilder<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 RetryPolicyBuilder<R> withMaxDuration(Duration maxDuration)
Notes:
Timeout
policy withInterrupt
set.max retries
, which are still 2
by default.
A max retries limit can be disabled via withMaxRetries(-1)
NullPointerException
- if maxDuration
is nullIllegalStateException
- if maxDuration
is <= the delay
or maxDuration
is <= the max random delay
.public RetryPolicyBuilder<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)
Copyright © 2022. All rights reserved.