Class RetryPolicyBuilder<R>
- java.lang.Object
-
- dev.failsafe.PolicyBuilder<S,C,R>
-
- dev.failsafe.FailurePolicyBuilder<S,C,R>
-
- dev.failsafe.DelayablePolicyBuilder<RetryPolicyBuilder<R>,RetryPolicyConfig<R>,R>
-
- dev.failsafe.RetryPolicyBuilder<R>
-
- Type Parameters:
R- result type
- All Implemented Interfaces:
PolicyListeners<RetryPolicyBuilder<R>,R>
public class RetryPolicyBuilder<R> extends DelayablePolicyBuilder<RetryPolicyBuilder<R>,RetryPolicyConfig<R>,R> implements PolicyListeners<RetryPolicyBuilder<R>,R>
BuildsRetryPolicyinstances.- By default, a RetryPolicy will retry up to
2times when anyExceptionis thrown, with no delay between retry attempts. - You can change the default number of retry attempts and delay between retries by using the
withconfiguration methods. - By default, any exception is considered a failure and will be handled by the policy. You can override this by
specifying your own
handleconditions. The default exception handling condition will only be overridden by another condition that handles exceptions such asFailurePolicyBuilder.handle(Class)orFailurePolicyBuilder.handleIf(CheckedBiPredicate). Specifying a condition that only handles results, such asFailurePolicyBuilder.handleResult(Object)orFailurePolicyBuilder.handleResultIf(CheckedPredicate)will not replace the default exception handling condition. - If multiple
handleconditions are specified, any condition that matches an execution result or exception will trigger policy handling. - The
abortOn,abortWhenandabortIfmethods describe when retries should be aborted.
Note:
- This class extends
DelayablePolicyBuilderandFailurePolicyBuilderwhich offer additional configuration. - This class is not threadsafe.
- See Also:
RetryPolicyConfig
-
-
Field Summary
Fields Modifier and Type Field Description private static intDEFAULT_MAX_RETRIES-
Fields inherited from class dev.failsafe.PolicyBuilder
config
-
-
Constructor Summary
Constructors Constructor Description RetryPolicyBuilder()RetryPolicyBuilder(RetryPolicyConfig<R> config)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RetryPolicyBuilder<R>abortIf(CheckedBiPredicate<R,? extends java.lang.Throwable> completionPredicate)Specifies that retries should be aborted if thecompletionPredicatematches the completion result.RetryPolicyBuilder<R>abortIf(CheckedPredicate<R> resultPredicate)Specifies that retries should be aborted if theresultPredicatematches the result.RetryPolicyBuilder<R>abortOn(CheckedPredicate<? extends java.lang.Throwable> abortPredicate)Specifies that retries should be aborted if theabortPredicatematches the exception.RetryPolicyBuilder<R>abortOn(java.lang.Class<? extends java.lang.Throwable> exception)Specifies when retries should be aborted.RetryPolicyBuilder<R>abortOn(java.lang.Class<? extends java.lang.Throwable>... exceptions)Specifies when retries should be aborted.RetryPolicyBuilder<R>abortOn(java.util.List<java.lang.Class<? extends java.lang.Throwable>> exceptions)Specifies when retries should be aborted.RetryPolicyBuilder<R>abortWhen(R result)Specifies that retries should be aborted if the execution result matches theresult.RetryPolicy<R>build()Builds a newRetryPolicyusing the builder's configuration.RetryPolicyBuilder<R>onAbort(EventListener<ExecutionCompletedEvent<R>> listener)Registers thelistenerto be called when an execution is aborted.RetryPolicyBuilder<R>onFailedAttempt(EventListener<ExecutionAttemptedEvent<R>> listener)Registers thelistenerto be called when an execution attempt fails.RetryPolicyBuilder<R>onRetriesExceeded(EventListener<ExecutionCompletedEvent<R>> listener)Registers thelistenerto be called when an execution fails and themax retry attemptsormax durationare exceeded.RetryPolicyBuilder<R>onRetry(EventListener<ExecutionAttemptedEvent<R>> listener)Registers thelistenerto be called when a retry is about to be attempted.RetryPolicyBuilder<R>onRetryScheduled(EventListener<ExecutionScheduledEvent<R>> listener)Registers thelistenerto be called when a retry for an async call is about to be scheduled.RetryPolicyBuilder<R>withBackoff(long delay, long maxDelay, java.time.temporal.ChronoUnit chronoUnit)Sets thedelaybetween retries, exponentially backing off to themaxDelayand multiplying consecutive delays by a factor of 2.RetryPolicyBuilder<R>withBackoff(long delay, long maxDelay, java.time.temporal.ChronoUnit chronoUnit, double delayFactor)Sets thedelaybetween retries, exponentially backing off to themaxDelayand multiplying consecutive delays by thedelayFactor.RetryPolicyBuilder<R>withBackoff(java.time.Duration delay, java.time.Duration maxDelay)Sets thedelaybetween retries, exponentially backing off to themaxDelayand multiplying consecutive delays by a factor of 2.RetryPolicyBuilder<R>withBackoff(java.time.Duration delay, java.time.Duration maxDelay, double delayFactor)Sets thedelaybetween retries, exponentially backing off to themaxDelayand multiplying consecutive delays by thedelayFactor.RetryPolicyBuilder<R>withDelay(long delayMin, long delayMax, java.time.temporal.ChronoUnit chronoUnit)Sets a random delay between thedelayMinanddelayMax(inclusive) to occur between retries.RetryPolicyBuilder<R>withDelay(java.time.Duration delay)Sets the fixeddelayto occur between retries.RetryPolicyBuilder<R>withDelay(java.time.Duration delayMin, java.time.Duration delayMax)Sets a random delay between thedelayMinanddelayMax(inclusive) to occur between retries.RetryPolicyBuilder<R>withJitter(double jitterFactor)Sets thejitterFactorto randomly vary retry delays by.RetryPolicyBuilder<R>withJitter(java.time.Duration jitter)Sets thejitterto randomly vary retry delays by.RetryPolicyBuilder<R>withMaxAttempts(int maxAttempts)Sets the max number of execution attempts to perform.RetryPolicyBuilder<R>withMaxDuration(java.time.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.-
Methods inherited from class dev.failsafe.DelayablePolicyBuilder
withDelayFn, withDelayFnOn, withDelayFnWhen
-
Methods inherited from class dev.failsafe.FailurePolicyBuilder
failurePredicateFor, failurePredicateFor, handle, handle, handle, handleIf, handleIf, handleResult, handleResultIf, resultPredicateFor, resultPredicateFor
-
Methods inherited from class dev.failsafe.PolicyBuilder
onFailure, onSuccess
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface dev.failsafe.PolicyListeners
onFailure, onSuccess
-
-
-
-
Field Detail
-
DEFAULT_MAX_RETRIES
private static final int DEFAULT_MAX_RETRIES
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
RetryPolicyBuilder
RetryPolicyBuilder()
-
RetryPolicyBuilder
RetryPolicyBuilder(RetryPolicyConfig<R> config)
-
-
Method Detail
-
build
public RetryPolicy<R> build()
Builds a newRetryPolicyusing the builder's configuration.
-
abortIf
public RetryPolicyBuilder<R> abortIf(CheckedBiPredicate<R,? extends java.lang.Throwable> completionPredicate)
Specifies that retries should be aborted if thecompletionPredicatematches the completion result. Any exception thrown from thecompletionPredicateis treated as afalseresult.- Throws:
java.lang.NullPointerException- ifcompletionPredicateis null
-
abortIf
public RetryPolicyBuilder<R> abortIf(CheckedPredicate<R> resultPredicate)
Specifies that retries should be aborted if theresultPredicatematches the result. Predicate is not invoked when the operation fails. Any exception thrown from theresultPredicateis treated as afalseresult.- Throws:
java.lang.NullPointerException- ifresultPredicateis null
-
abortOn
public RetryPolicyBuilder<R> abortOn(java.lang.Class<? extends java.lang.Throwable> exception)
Specifies when retries should be aborted. Any exception that is assignable from theexceptionwill be result in retries being aborted.- Throws:
java.lang.NullPointerException- ifexceptionis null
-
abortOn
@SafeVarargs public final RetryPolicyBuilder<R> abortOn(java.lang.Class<? extends java.lang.Throwable>... exceptions)
Specifies when retries should be aborted. Any exception that is assignable from theexceptionswill be result in retries being aborted.- Throws:
java.lang.NullPointerException- ifexceptionsis nulljava.lang.IllegalArgumentException- if exceptions is empty
-
abortOn
public RetryPolicyBuilder<R> abortOn(java.util.List<java.lang.Class<? extends java.lang.Throwable>> exceptions)
Specifies when retries should be aborted. Any exception that is assignable from theexceptionswill be result in retries being aborted.- Throws:
java.lang.NullPointerException- ifexceptionsis nulljava.lang.IllegalArgumentException- if exceptions is null or empty
-
abortOn
public RetryPolicyBuilder<R> abortOn(CheckedPredicate<? extends java.lang.Throwable> abortPredicate)
Specifies that retries should be aborted if theabortPredicatematches the exception. Any exception thrown from theabortPredicateis treated as afalseresult.- Throws:
java.lang.NullPointerException- ifabortPredicateis null
-
abortWhen
public RetryPolicyBuilder<R> abortWhen(R result)
Specifies that retries should be aborted if the execution result matches theresult.
-
onAbort
public RetryPolicyBuilder<R> onAbort(EventListener<ExecutionCompletedEvent<R>> listener)
Registers thelistenerto be called when an execution is aborted.Note: Any exceptions that are thrown from within the
listenerare ignored. To provide an alternative result for a failed execution, use aFallback.- Throws:
java.lang.NullPointerException- iflisteneris null
-
onFailedAttempt
public RetryPolicyBuilder<R> onFailedAttempt(EventListener<ExecutionAttemptedEvent<R>> listener)
Registers thelistenerto be called when an execution attempt fails. You can also useonFailureto determine when the execution attempt fails and and all retries have failed.Note: Any exceptions that are thrown from within the
listenerare ignored. To provide an alternative result for a failed execution, use aFallback.- Throws:
java.lang.NullPointerException- iflisteneris null
-
onRetriesExceeded
public RetryPolicyBuilder<R> onRetriesExceeded(EventListener<ExecutionCompletedEvent<R>> listener)
Registers thelistenerto be called when an execution fails and themax retry attemptsormax durationare exceeded.Note: Any exceptions that are thrown from within the
listenerare ignored. To provide an alternative result for a failed execution, use aFallback.- Throws:
java.lang.NullPointerException- iflisteneris null
-
onRetry
public RetryPolicyBuilder<R> onRetry(EventListener<ExecutionAttemptedEvent<R>> listener)
Registers thelistenerto be called when a retry is about to be attempted.Note: Any exceptions that are thrown from within the
listenerare ignored. To provide an alternative result for a failed execution, use aFallback.- Throws:
java.lang.NullPointerException- iflisteneris null- See Also:
onRetryScheduled(EventListener)
-
onRetryScheduled
public RetryPolicyBuilder<R> onRetryScheduled(EventListener<ExecutionScheduledEvent<R>> listener)
Registers thelistenerto be called when a retry for an async call is about to be scheduled. This method differs fromonRetry(EventListener)since it is called when a retry is initially scheduled but before any configured delay, whereasonRetryis called after a delay, just before the retry attempt takes place.Note: Any exceptions that are thrown from within the
listenerare ignored. To provide an alternative result for a failed execution, use aFallback.- Throws:
java.lang.NullPointerException- iflisteneris null- See Also:
onRetry(EventListener)
-
withBackoff
public RetryPolicyBuilder<R> withBackoff(java.time.Duration delay, java.time.Duration maxDelay)
Sets thedelaybetween retries, exponentially backing off to themaxDelayand multiplying consecutive delays by a factor of 2. Replaces any previously configuredfixedorrandomdelays.- Throws:
java.lang.NullPointerException- ifdelayormaxDelayare nulljava.lang.IllegalArgumentException- ifdelayis <= 0 ordelayis >=maxDelayjava.lang.IllegalStateException- ifdelayis >= themaxDurationordelayis < a configuredjitter duration
-
withBackoff
public RetryPolicyBuilder<R> withBackoff(long delay, long maxDelay, java.time.temporal.ChronoUnit chronoUnit)
Sets thedelaybetween retries, exponentially backing off to themaxDelayand multiplying consecutive delays by a factor of 2. Replaces any previously configuredfixedorrandomdelays.- Throws:
java.lang.NullPointerException- ifchronoUnitis nulljava.lang.IllegalArgumentException- ifdelayis <= 0 ordelayis >=maxDelayjava.lang.IllegalStateException- ifdelayis >= themaxDurationordelayis < a configuredjitter duration
-
withBackoff
public RetryPolicyBuilder<R> withBackoff(long delay, long maxDelay, java.time.temporal.ChronoUnit chronoUnit, double delayFactor)
Sets thedelaybetween retries, exponentially backing off to themaxDelayand multiplying consecutive delays by thedelayFactor. Replaces any previously configuredfixedorrandomdelays.- Throws:
java.lang.NullPointerException- ifchronoUnitis nulljava.lang.IllegalArgumentException- ifdelay<= 0,delayis >=maxDelay, or thedelayFactoris <= 1java.lang.IllegalStateException- ifdelayis >= themaxDurationordelayis < a configuredjitter duration
-
withBackoff
public RetryPolicyBuilder<R> withBackoff(java.time.Duration delay, java.time.Duration maxDelay, double delayFactor)
Sets thedelaybetween retries, exponentially backing off to themaxDelayand multiplying consecutive delays by thedelayFactor. Replaces any previously configuredfixedorrandomdelays.- Throws:
java.lang.NullPointerException- ifdelayormaxDelayare nulljava.lang.IllegalArgumentException- ifdelay<= 0,delayis >=maxDelay, or thedelayFactoris <= 1java.lang.IllegalStateException- ifdelayis >= themaxDurationordelayis < a configuredjitter duration
-
withDelay
public RetryPolicyBuilder<R> withDelay(java.time.Duration delay)
Sets the fixeddelayto occur between retries. Replaces any previously configuredbackofforrandomdelays.- Overrides:
withDelayin classDelayablePolicyBuilder<RetryPolicyBuilder<R>,RetryPolicyConfig<R>,R>- Throws:
java.lang.NullPointerException- ifdelayis nulljava.lang.IllegalArgumentException- ifdelay<= 0java.lang.IllegalStateException- ifdelayis >= themaxDurationordelayis < a configuredjitter duration
-
withDelay
public RetryPolicyBuilder<R> withDelay(long delayMin, long delayMax, java.time.temporal.ChronoUnit chronoUnit)
Sets a random delay between thedelayMinanddelayMax(inclusive) to occur between retries. Replaces any previously configuredfixedorbackoffdelays.- Throws:
java.lang.NullPointerException- ifchronoUnitis nulljava.lang.IllegalArgumentException- ifdelayMinordelayMaxare <= 0, ordelayMin>=delayMaxjava.lang.IllegalStateException- ifdelayMaxis >= themaxDurationordelayMinis < a configuredjitter duration
-
withDelay
public RetryPolicyBuilder<R> withDelay(java.time.Duration delayMin, java.time.Duration delayMax)
Sets a random delay between thedelayMinanddelayMax(inclusive) to occur between retries. Replaces any previously configuredfixedorbackoffdelays.- Throws:
java.lang.NullPointerException- ifdelayMinordelayMaxare nulljava.lang.IllegalArgumentException- ifdelayMinordelayMaxare <= 0, ordelayMin>=delayMaxjava.lang.IllegalStateException- ifdelayMaxis >= themaxDurationordelayis < a configuredjitter duration
-
withJitter
public RetryPolicyBuilder<R> withJitter(double jitterFactor)
Sets thejitterFactorto randomly vary retry delays by. For each retry delay, a random portion of the delay multiplied by thejitterFactorwill be added or subtracted to the delay. For example: a retry delay of100milliseconds and ajitterFactorof.25will result in a random retry delay between75and125milliseconds. Replaces any previously configuredjitter duration.Jitter should be combined with
fixed,randomorexponential backoffdelays. If no delays are configured, this setting is ignored.- Throws:
java.lang.IllegalArgumentException- ifjitterFactoris < 0 or > 1
-
withJitter
public RetryPolicyBuilder<R> withJitter(java.time.Duration jitter)
Sets thejitterto randomly vary retry delays by. For each retry delay, a random portion of thejitterwill be added or subtracted to the delay. For example: ajitterof100milliseconds will randomly add between-100and100milliseconds to each retry delay. Replaces any previously configuredjitter factor.Jitter should be combined with
fixed,randomorexponential backoffdelays. If no delays are configured, this setting is ignored.- Throws:
java.lang.NullPointerException- ifjitteris nulljava.lang.IllegalArgumentException- ifjitteris <= 0java.lang.IllegalStateException- if the jitter is greater than the min configured delay
-
withMaxAttempts
public RetryPolicyBuilder<R> withMaxAttempts(int maxAttempts)
Sets the max number of execution attempts to perform.-1indicates no limit. This method has the same effect as setting 1 more thanwithMaxRetries(int). For example, 2 retries equal 3 attempts.- Throws:
java.lang.IllegalArgumentException- ifmaxAttemptsis 0 or less than -1- See Also:
withMaxRetries(int)
-
withMaxDuration
public RetryPolicyBuilder<R> withMaxDuration(java.time.Duration maxDuration)
Sets the max duration to perform retries for, else the execution will be failed.Notes:
- This setting will not cause long running executions to be interrupted. For that capability, use a
TimeoutpolicywithInterruptset. - This setting will not disable
max retries, which are still2by default. A max retries limit can be disabled viawithMaxRetries(-1)
- Throws:
java.lang.NullPointerException- ifmaxDurationis nulljava.lang.IllegalStateException- ifmaxDurationis <= thedelayormaxDurationis <= themax random delay.
- This setting will not cause long running executions to be interrupted. For that capability, use a
-
withMaxRetries
public RetryPolicyBuilder<R> withMaxRetries(int maxRetries)
Sets the max number of retries to perform when an execution attempt fails.-1indicates no limit. This method has the same effect as setting 1 less thanwithMaxAttempts(int). For example, 2 retries equal 3 attempts.- Throws:
java.lang.IllegalArgumentException- ifmaxRetries< -1- See Also:
withMaxAttempts(int)
-
-