Class RetryPolicyBuilder<R>
- Type Parameters:
R- result type
- All Implemented Interfaces:
PolicyListeners<RetryPolicyBuilder<R>,R>
RetryPolicy instances.
- 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:
-
Field Summary
FieldsFields inherited from class dev.failsafe.PolicyBuilder
config -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabortIf(CheckedBiPredicate<R, ? extends Throwable> completionPredicate) Specifies that retries should be aborted if thecompletionPredicatematches the completion result.abortIf(CheckedPredicate<R> resultPredicate) Specifies that retries should be aborted if theresultPredicatematches the result.abortOn(CheckedPredicate<? extends Throwable> abortPredicate) Specifies that retries should be aborted if theabortPredicatematches the exception.Specifies when retries should be aborted.final RetryPolicyBuilder<R> Specifies when retries should be aborted.Specifies when retries should be aborted.Specifies that retries should be aborted if the execution result matches theresult.build()Builds a newRetryPolicyusing the builder's configuration.onAbort(EventListener<ExecutionCompletedEvent<R>> listener) Registers thelistenerto be called when an execution is aborted.onFailedAttempt(EventListener<ExecutionAttemptedEvent<R>> listener) Registers thelistenerto be called when an execution attempt fails.onRetriesExceeded(EventListener<ExecutionCompletedEvent<R>> listener) Registers thelistenerto be called when an execution fails and themax retry attemptsormax durationare exceeded.onRetry(EventListener<ExecutionAttemptedEvent<R>> listener) Registers thelistenerto be called when a retry is about to be attempted.onRetryScheduled(EventListener<ExecutionScheduledEvent<R>> listener) Registers thelistenerto be called when a retry for an async call is about to be scheduled.withBackoff(long delay, long maxDelay, ChronoUnit chronoUnit) Sets thedelaybetween retries, exponentially backing off to themaxDelayand multiplying consecutive delays by a factor of 2.withBackoff(long delay, long maxDelay, ChronoUnit chronoUnit, double delayFactor) Sets thedelaybetween retries, exponentially backing off to themaxDelayand multiplying consecutive delays by thedelayFactor.withBackoff(Duration delay, Duration maxDelay) Sets thedelaybetween retries, exponentially backing off to themaxDelayand multiplying consecutive delays by a factor of 2.withBackoff(Duration delay, Duration maxDelay, double delayFactor) Sets thedelaybetween retries, exponentially backing off to themaxDelayand multiplying consecutive delays by thedelayFactor.withDelay(long delayMin, long delayMax, ChronoUnit chronoUnit) Sets a random delay between thedelayMinanddelayMax(inclusive) to occur between retries.Sets the fixeddelayto occur between retries.Sets a random delay between thedelayMinanddelayMax(inclusive) to occur between retries.withJitter(double jitterFactor) Sets thejitterFactorto randomly vary retry delays by.withJitter(Duration jitter) Sets thejitterto randomly vary retry delays by.withMaxAttempts(int maxAttempts) Sets the max number of execution attempts to perform.withMaxDuration(Duration maxDuration) Sets the max duration to perform retries for, else the execution will be failed.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, withDelayFnWhenMethods inherited from class dev.failsafe.FailurePolicyBuilder
failurePredicateFor, failurePredicateFor, handle, handle, handle, handleIf, handleIf, handleResult, handleResultIf, resultPredicateFor, resultPredicateForMethods inherited from class dev.failsafe.PolicyBuilder
onFailure, onSuccessMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface dev.failsafe.PolicyListeners
onFailure, onSuccess
-
Field Details
-
DEFAULT_MAX_RETRIES
private static final int DEFAULT_MAX_RETRIES- See Also:
-
-
Constructor Details
-
RetryPolicyBuilder
RetryPolicyBuilder() -
RetryPolicyBuilder
RetryPolicyBuilder(RetryPolicyConfig<R> config)
-
-
Method Details
-
build
Builds a newRetryPolicyusing the builder's configuration. -
abortIf
Specifies that retries should be aborted if thecompletionPredicatematches the completion result. Any exception thrown from thecompletionPredicateis treated as afalseresult.- Throws:
NullPointerException- ifcompletionPredicateis null
-
abortIf
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:
NullPointerException- ifresultPredicateis null
-
abortOn
Specifies when retries should be aborted. Any exception that is assignable from theexceptionwill be result in retries being aborted.- Throws:
NullPointerException- ifexceptionis null
-
abortOn
Specifies when retries should be aborted. Any exception that is assignable from theexceptionswill be result in retries being aborted.- Throws:
NullPointerException- ifexceptionsis nullIllegalArgumentException- if exceptions is empty
-
abortOn
Specifies when retries should be aborted. Any exception that is assignable from theexceptionswill be result in retries being aborted.- Throws:
NullPointerException- ifexceptionsis nullIllegalArgumentException- if exceptions is null or empty
-
abortOn
Specifies that retries should be aborted if theabortPredicatematches the exception. Any exception thrown from theabortPredicateis treated as afalseresult.- Throws:
NullPointerException- ifabortPredicateis null
-
abortWhen
Specifies that retries should be aborted if the execution result matches theresult. -
onAbort
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:
NullPointerException- iflisteneris null
-
onFailedAttempt
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:
NullPointerException- iflisteneris null
-
onRetriesExceeded
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:
NullPointerException- iflisteneris null
-
onRetry
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:
NullPointerException- iflisteneris null- See Also:
-
onRetryScheduled
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:
NullPointerException- iflisteneris null- See Also:
-
withBackoff
Sets thedelaybetween retries, exponentially backing off to themaxDelayand multiplying consecutive delays by a factor of 2. Replaces any previously configuredfixedorrandomdelays.- Throws:
NullPointerException- ifdelayormaxDelayare nullIllegalArgumentException- ifdelayis invalid input: '<'= 0 ordelayis >=maxDelayIllegalStateException- ifdelayis >= themaxDurationordelayis invalid input: '<' a configuredjitter duration
-
withBackoff
Sets thedelaybetween retries, exponentially backing off to themaxDelayand multiplying consecutive delays by a factor of 2. Replaces any previously configuredfixedorrandomdelays.- Throws:
NullPointerException- ifchronoUnitis nullIllegalArgumentException- ifdelayis invalid input: '<'= 0 ordelayis >=maxDelayIllegalStateException- ifdelayis >= themaxDurationordelayis invalid input: '<' a configuredjitter duration
-
withBackoff
public RetryPolicyBuilder<R> withBackoff(long delay, long maxDelay, ChronoUnit chronoUnit, double delayFactor) Sets thedelaybetween retries, exponentially backing off to themaxDelayand multiplying consecutive delays by thedelayFactor. Replaces any previously configuredfixedorrandomdelays.- Throws:
NullPointerException- ifchronoUnitis nullIllegalArgumentException- ifdelayinvalid input: '<'= 0,delayis >=maxDelay, or thedelayFactoris invalid input: '<'= 1IllegalStateException- ifdelayis >= themaxDurationordelayis invalid input: '<' a configuredjitter duration
-
withBackoff
Sets thedelaybetween retries, exponentially backing off to themaxDelayand multiplying consecutive delays by thedelayFactor. Replaces any previously configuredfixedorrandomdelays.- Throws:
NullPointerException- ifdelayormaxDelayare nullIllegalArgumentException- ifdelayinvalid input: '<'= 0,delayis >=maxDelay, or thedelayFactoris invalid input: '<'= 1IllegalStateException- ifdelayis >= themaxDurationordelayis invalid input: '<' a configuredjitter duration
-
withDelay
Sets the fixeddelayto occur between retries. Replaces any previously configuredbackofforrandomdelays.- Overrides:
withDelayin classDelayablePolicyBuilder<RetryPolicyBuilder<R>,RetryPolicyConfig<R>, R> - Throws:
NullPointerException- ifdelayis nullIllegalArgumentException- ifdelayinvalid input: '<'= 0IllegalStateException- ifdelayis >= themaxDurationordelayis invalid input: '<' a configuredjitter duration
-
withDelay
Sets a random delay between thedelayMinanddelayMax(inclusive) to occur between retries. Replaces any previously configuredfixedorbackoffdelays.- Throws:
NullPointerException- ifchronoUnitis nullIllegalArgumentException- ifdelayMinordelayMaxare invalid input: '<'= 0, ordelayMin>=delayMaxIllegalStateException- ifdelayMaxis >= themaxDurationordelayMinis invalid input: '<' a configuredjitter duration
-
withDelay
Sets a random delay between thedelayMinanddelayMax(inclusive) to occur between retries. Replaces any previously configuredfixedorbackoffdelays.- Throws:
NullPointerException- ifdelayMinordelayMaxare nullIllegalArgumentException- ifdelayMinordelayMaxare invalid input: '<'= 0, ordelayMin>=delayMaxIllegalStateException- ifdelayMaxis >= themaxDurationordelayis invalid input: '<' a configuredjitter duration
-
withJitter
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:
IllegalArgumentException- ifjitterFactoris invalid input: '<' 0 or > 1
-
withJitter
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:
NullPointerException- ifjitteris nullIllegalArgumentException- ifjitteris invalid input: '<'= 0IllegalStateException- if the jitter is greater than the min configured delay
-
withMaxAttempts
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:
IllegalArgumentException- ifmaxAttemptsis 0 or less than -1- See Also:
-
withMaxDuration
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:
NullPointerException- ifmaxDurationis nullIllegalStateException- ifmaxDurationis invalid input: '<'= thedelayormaxDurationis invalid input: '<'= themax random delay.
- This setting will not cause long running executions to be interrupted. For that capability, use a
-
withMaxRetries
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:
IllegalArgumentException- ifmaxRetriesinvalid input: '<' -1- See Also:
-