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
-
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 thanmaxAttempts
times.Retry immediately without pausing between attempts.notRetryOn
(Class<? extends Throwable> throwable) Add theThrowable
and its subclasses to the list of exceptions that do not cause a retry.notRetryOn
(List<Class<? extends Throwable>> throwables) Add the list ofThrowable
classes and their subclasses to the list of exceptions that do not cause a retry.Add theThrowable
and its subclasses to the list of exceptions that cause a retry.Add the list ofThrowable
classes and their subclasses to the list of exceptions that cause a retry.Enable examining exception causes forThrowable
instances 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 providedlistener
toRetryTemplate
's list of listeners.withListeners
(List<RetryListener> listeners) Append all providedlisteners
toRetryTemplate
's list of listeners.withLogger
(org.apache.commons.logging.Log logger) Applies a dedicated logger to theRetryTemplate
.withTimeout
(long timeoutMillis) Retry untiltimeoutMillis
has passed since the initial attempt.withTimeout
(Duration timeout) Retry untiltimeout
has passed since the initial attempt.
-
Constructor Details
-
RetryTemplateBuilder
public RetryTemplateBuilder()
-
-
Method Details
-
maxAttempts
Attempt an action no more thanmaxAttempts
times.- 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
- ifmaxAttempts
is 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 untiltimeout
has 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 untiltimeoutMillis
has 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 untiltimeout
has passed since the initial attempt.- Parameters:
timeout
- duration for how long retries should be attempted- Returns:
- this
- Throws:
IllegalArgumentException
- if timeout isnull
or 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
-RetryPolicy
to 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, ifinitialInterval
is < 1, ifmultiplier
is <= 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, ifinitialInterval
isnull
or less than 1 millisecond, multiplier is <= 1, or ifmaxInterval
isnull
or <=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, ifinitialInterval
is < 1, ifmultiplier
is <= 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, ifinitialInterval
isnull
or less than 1 millisecond, ifmultiplier
is <= 1, or ifmaxInterval
isnull
or <=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 ifinterval
is < 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 ifinterval
isnull
or 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, ifminInterval
is < 1,maxInterval
is < 1, or ifmaxInterval
is <=minInterval
.- See Also:
-
uniformRandomBackoff
- Parameters:
minInterval
- minimum backoff durationmaxInterval
- maximum backoff duration- Returns:
- this
- Throws:
IllegalArgumentException
- if another backoff policy has already been selected, ifminInterval
isnull
or < 1,maxInterval
isnull
or 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
-BackOffPolicy
to use- Returns:
- this
- Throws:
IllegalArgumentException
- ifbackOffPolicy
is null or if another backoff policy has already been selected.
-
retryOn
Add theThrowable
and 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
- ifthrowable
isnull
, or ifnotRetryOn(java.lang.Class<? extends java.lang.Throwable>)
has already been used.- See Also:
-
notRetryOn
Add theThrowable
and 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
- ifthrowable
isnull
, or ifretryOn(java.lang.Class<? extends java.lang.Throwable>)
has already been used.- See Also:
-
retryOn
Add the list ofThrowable
classes 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 ofThrowable
classes 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 forThrowable
instances 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 theIOException
is wrapped in another exception, for example,throw new MyException(new IOException())
. However, thisRetryTemplate
will:RetryTemplate.builder() .retryOn(IOException.class) .traversingCauses() .build()
- Returns:
- this
- See Also:
-
withListener
Append the providedlistener
toRetryTemplate
's list of listeners.- Parameters:
listener
- to be appended- Returns:
- this
- Throws:
IllegalArgumentException
- iflistener
isnull
.- See Also:
-
withListeners
Append all providedlisteners
toRetryTemplate
's list of listeners.- Parameters:
listeners
- to be appended- Returns:
- this
- Throws:
IllegalArgumentException
- if any of thelisteners
isnull
.- See Also:
-
build
Build a newRetryTemplate
.Supports building multiple instances. However, it is not possible to change the configuration between multiple
build()
calls.The
retryPolicy
of the returnedRetryTemplate
is always aCompositeRetryPolicy
that consists of one base policy and ofBinaryExceptionClassifierRetryPolicy
to enable exception classification regardless of the base policy.- Returns:
- new instance of
RetryTemplate
-
withTimeout(long)
instead.