Class RetryTemplateBuilder
RetryTemplate.
By default, the builder configures a BinaryExceptionClassifier that acts upon
Exception and its subclasses without traversing causes, a
NoBackOffPolicy and a MaxAttemptsRetryPolicy that attempts actions
MaxAttemptsRetryPolicy.DEFAULT_MAX_ATTEMPTS times.
The builder is not thread-safe.
Example usage:
RetryTemplate.builder()
.maxAttempts(10)
.exponentialBackoff(100, 2, 10000)
.retryOn(IOException.class)
.traversingCauses()
.build();
RetryTemplate.builder()
.fixedBackoff(10)
.withinMillis(3000)
.build();
RetryTemplate.builder()
.infiniteRetry()
.retryOn(IOException.class)
.uniformRandomBackoff(1000, 3000)
.build();
- Since:
- 1.3
- Author:
- Aleksandr Shamukov, Artem Bilan, Kim In Hoi, Andreas Ahlenstorf, Morulai Planinski, Tobias Soloschenko
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()Build a newRetryTemplate.customBackoff(BackOffPolicy backOffPolicy) Use the providedBackOffPolicy.customPolicy(RetryPolicy policy) Use the providedRetryPolicy.exponentialBackoff(long initialInterval, double multiplier, long maxInterval) Use an exponential backoff policy.exponentialBackoff(long initialInterval, double multiplier, long maxInterval, boolean withRandom) Use an exponential backoff policy.exponentialBackoff(Duration initialInterval, double multiplier, Duration maxInterval) Use an exponential backoff policy.exponentialBackoff(Duration initialInterval, double multiplier, Duration maxInterval, boolean withRandom) Use an exponential backoff policy.fixedBackoff(long interval) Perform each retry after a fixed amount of time.fixedBackoff(Duration interval) Perform each retry after fixed amount of time.Retry actions infinitely.maxAttempts(int maxAttempts) Attempt an action no more thanmaxAttemptstimes.Retry immediately without pausing between attempts.notRetryOn(Class<? extends Throwable> throwable) Add theThrowableand its subclasses to the list of exceptions that do not cause a retry.notRetryOn(List<Class<? extends Throwable>> throwables) Add the list ofThrowableclasses and their subclasses to the list of exceptions that do not cause a retry.Add theThrowableand its subclasses to the list of exceptions that cause a retry.Add the list ofThrowableclasses and their subclasses to the list of exceptions that cause a retry.Enable examining exception causes forThrowableinstances that cause a retry.uniformRandomBackoff(long minInterval, long maxInterval) uniformRandomBackoff(Duration minInterval, Duration maxInterval) withinMillis(long timeout) Deprecated, for removal: This API element is subject to removal in a future version.withListener(RetryListener listener) Append the providedlistenertoRetryTemplate's list of listeners.withListeners(List<RetryListener> listeners) Append all providedlistenerstoRetryTemplate's list of listeners.withLogger(org.apache.commons.logging.Log logger) Applies a dedicated logger to theRetryTemplate.withTimeout(long timeoutMillis) Retry untiltimeoutMillishas passed since the initial attempt.withTimeout(Duration timeout) Retry untiltimeouthas passed since the initial attempt.
-
Constructor Details
-
RetryTemplateBuilder
public RetryTemplateBuilder()
-
-
Method Details
-
maxAttempts
Attempt an action no more thanmaxAttemptstimes.- Parameters:
maxAttempts- how many times an action should be attempted. A value of 3 would result in an initial attempt and two retries.- Returns:
- this
- Throws:
IllegalArgumentException- ifmaxAttemptsis 0 or less, or if another retry policy has already been selected.- See Also:
-
withinMillis
Deprecated, for removal: This API element is subject to removal in a future version.UsewithTimeout(long)instead.Retry untiltimeouthas passed since the initial attempt.- Parameters:
timeout- timeout in milliseconds- Returns:
- this
- Throws:
IllegalArgumentException- if timeout is <= 0 or if another retry policy has already been selected.- See Also:
-
withTimeout
Retry untiltimeoutMillishas passed since the initial attempt.- Parameters:
timeoutMillis- timeout in milliseconds- Returns:
- this
- Throws:
IllegalArgumentException- if timeout is <= 0 or if another retry policy has already been selected.- Since:
- 2.0.2
- See Also:
-
withTimeout
Retry untiltimeouthas passed since the initial attempt.- Parameters:
timeout- duration for how long retries should be attempted- Returns:
- this
- Throws:
IllegalArgumentException- if timeout isnullor 0, or if another retry policy has already been selected.- Since:
- 2.0.2
- See Also:
-
infiniteRetry
Retry actions infinitely.- Returns:
- this
- Throws:
IllegalArgumentException- if another retry policy has already been selected.- See Also:
-
customPolicy
Use the providedRetryPolicy.- Parameters:
policy-RetryPolicyto use- Returns:
- this
- Throws:
IllegalArgumentException- if another backoff policy has already been selected, if any argument isnull, or if another retry policy has already been selected.
-
exponentialBackoff
public RetryTemplateBuilder exponentialBackoff(long initialInterval, double multiplier, long maxInterval) Use an exponential backoff policy. The formula for the backoff period is:currentInterval = Math.min(initialInterval * Math.pow(multiplier, retryNum), maxInterval)For the first attempt,
retryNum = 0.- Parameters:
initialInterval- initial sleep duration in millisecondsmultiplier- backoff interval multipliermaxInterval- maximum backoff duration in milliseconds- Returns:
- this
- Throws:
IllegalArgumentException- if another backoff policy has already been selected, ifinitialIntervalis < 1, ifmultiplieris <= 1, or ifmaxInterval<=initialInterval.- See Also:
-
exponentialBackoff
public RetryTemplateBuilder exponentialBackoff(Duration initialInterval, double multiplier, Duration maxInterval) Use an exponential backoff policy. The formula for the backoff period is:currentInterval = Math.min(initialInterval * Math.pow(multiplier, retryNum), maxInterval)For the first attempt,
retryNum = 0.- Parameters:
initialInterval- initial sleep durationmultiplier- backoff interval multipliermaxInterval- maximum backoff duration- Returns:
- this
- Throws:
IllegalArgumentException- if another backoff policy has already been selected, ifinitialIntervalisnullor less than 1 millisecond, multiplier is <= 1, or ifmaxIntervalisnullor <=initialInterval.- Since:
- 2.0.2
- See Also:
-
exponentialBackoff
public RetryTemplateBuilder exponentialBackoff(long initialInterval, double multiplier, long maxInterval, boolean withRandom) Use an exponential backoff policy. The formula for the backoff period is (without randomness):currentInterval = Math.min(initialInterval * Math.pow(multiplier, retryNum), maxInterval)For the first attempt,
retryNum = 0.- Parameters:
initialInterval- initial sleep duration in millisecondsmultiplier- backoff interval multipliermaxInterval- maximum backoff duration in millisecondswithRandom- whether to use aExponentialRandomBackOffPolicy(iftrue) or not- Returns:
- this
- Throws:
IllegalArgumentException- if another backoff policy has already been selected, ifinitialIntervalis < 1, ifmultiplieris <= 1, or ifmaxInterval<=initialInterval.- See Also:
-
exponentialBackoff
public RetryTemplateBuilder exponentialBackoff(Duration initialInterval, double multiplier, Duration maxInterval, boolean withRandom) Use an exponential backoff policy. The formula for the backoff period is (without randomness):currentInterval = Math.min(initialInterval * Math.pow(multiplier, retryNum), maxInterval)For the first attempt,
retryNum = 0.- Parameters:
initialInterval- initial sleep durationmultiplier- backoff interval multipliermaxInterval- maximum backoff durationwithRandom- whether to use aExponentialRandomBackOffPolicy(iftrue) or not- Returns:
- this
- Throws:
IllegalArgumentException- if another backoff policy has already been selected, ifinitialIntervalisnullor less than 1 millisecond, ifmultiplieris <= 1, or ifmaxIntervalisnullor <=initialInterval.- Since:
- 2.0.2
- See Also:
-
withLogger
Applies a dedicated logger to theRetryTemplate. If not applied the following is used:LogFactory.getLog(getClass())- Parameters:
logger- the logger which should be used for logging- Returns:
- this
- Since:
- 2.0.10
-
fixedBackoff
Perform each retry after a fixed amount of time.- Parameters:
interval- fixed interval in milliseconds- Returns:
- this
- Throws:
IllegalArgumentException- if another backoff policy has already been selected, or ifintervalis < 1.- See Also:
-
fixedBackoff
Perform each retry after fixed amount of time.- Parameters:
interval- fixed backoff duration- Returns:
- this
- Throws:
IllegalArgumentException- if another backoff policy has already been selected, or ifintervalisnullor less than 1 millisecond- Since:
- 2.0.2
- See Also:
-
uniformRandomBackoff
- Parameters:
minInterval- minimal interval in millisecondsmaxInterval- maximal interval in milliseconds- Returns:
- this
- Throws:
IllegalArgumentException- if another backoff policy has already been selected, ifminIntervalis < 1,maxIntervalis < 1, or ifmaxIntervalis <=minInterval.- See Also:
-
uniformRandomBackoff
- Parameters:
minInterval- minimum backoff durationmaxInterval- maximum backoff duration- Returns:
- this
- Throws:
IllegalArgumentException- if another backoff policy has already been selected, ifminIntervalisnullor < 1,maxIntervalisnullor less than 1 millisecond, or ifmaxInterval<=minInterval.- Since:
- 2.0.2
- See Also:
-
noBackoff
Retry immediately without pausing between attempts.- Returns:
- this
- Throws:
IllegalArgumentException- if another backoff policy has already been selected.- See Also:
-
customBackoff
Use the providedBackOffPolicy.- Parameters:
backOffPolicy-BackOffPolicyto use- Returns:
- this
- Throws:
IllegalArgumentException- ifbackOffPolicyis null or if another backoff policy has already been selected.
-
retryOn
Add theThrowableand its subclasses to the list of exceptions that cause a retry.retryOn()andnotRetryOn()are mutually exclusive. Trying to use both causes anIllegalArgumentException.- Parameters:
throwable- that causes a retry- Returns:
- this
- Throws:
IllegalArgumentException- ifthrowableisnull, or ifnotRetryOn(java.lang.Class<? extends java.lang.Throwable>)has already been used.- See Also:
-
notRetryOn
Add theThrowableand its subclasses to the list of exceptions that do not cause a retry.retryOn()andnotRetryOn()are mutually exclusive. Trying to use both causes anIllegalArgumentException.- Parameters:
throwable- that does not cause a retry- Returns:
- this
- Throws:
IllegalArgumentException- ifthrowableisnull, or ifretryOn(java.lang.Class<? extends java.lang.Throwable>)has already been used.- See Also:
-
retryOn
Add the list ofThrowableclasses and their subclasses to the list of exceptions that cause a retry.retryOn()andnotRetryOn()are mutually exclusive. Trying to use both causes anIllegalArgumentException.- Parameters:
throwables- that cause a retry- Returns:
- this
- Throws:
IllegalArgumentException- ifnotRetryOn(java.lang.Class<? extends java.lang.Throwable>)has already been used.- Since:
- 1.3.2
- See Also:
-
notRetryOn
Add the list ofThrowableclasses and their subclasses to the list of exceptions that do not cause a retry.retryOn()andnotRetryOn()are mutually exclusive. Trying to use both causes anIllegalArgumentException.- Parameters:
throwables- that do not cause a retry- Returns:
- this
- Throws:
IllegalArgumentException- ifretryOn(java.lang.Class<? extends java.lang.Throwable>)has already been used.- Since:
- 1.3.2
- See Also:
-
retryOn
Set aPredicate<Throwable>that decides if the exception causes a retry.retryOn(Predicate<Throwable>)cannot be mixed with otherretryOn()ornoRetryOn(). Attempting to do so will result in aIllegalArgumentException.- Parameters:
predicate- if the exception causes a retry.- Returns:
- this
- Throws:
IllegalArgumentException- ifretryOn(java.lang.Class<? extends java.lang.Throwable>)ornotRetryOn(java.lang.Class<? extends java.lang.Throwable>)has already been used.- Since:
- 2.0.7
- See Also:
-
traversingCauses
Enable examining exception causes forThrowableinstances that cause a retry.Suppose the following
RetryTemplate:RetryTemplate.builder() .retryOn(IOException.class) .build()It will act on code that throws an
IOException, for example,throw new IOException(). But it will not retry the action if theIOExceptionis wrapped in another exception, for example,throw new MyException(new IOException()). However, thisRetryTemplatewill:RetryTemplate.builder() .retryOn(IOException.class) .traversingCauses() .build()- Returns:
- this
- See Also:
-
withListener
Append the providedlistenertoRetryTemplate's list of listeners.- Parameters:
listener- to be appended- Returns:
- this
- Throws:
IllegalArgumentException- iflistenerisnull.- See Also:
-
withListeners
Append all providedlistenerstoRetryTemplate's list of listeners.- Parameters:
listeners- to be appended- Returns:
- this
- Throws:
IllegalArgumentException- if any of thelistenersisnull.- See Also:
-
build
Build a newRetryTemplate.Supports building multiple instances. However, it is not possible to change the configuration between multiple
build()calls.The
retryPolicyof the returnedRetryTemplateis always aCompositeRetryPolicythat consists of one base policy and ofBinaryExceptionClassifierRetryPolicyto enable exception classification regardless of the base policy.- Returns:
- new instance of
RetryTemplate
-
withTimeout(long)instead.