Class RetryTemplateBuilder
Examples:
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();
The builder provides the following defaults:
- retry policy: max attempts = 3 (initial + 2 retries)
- backoff policy: no backoff (retry immediately)
- exception classification: retry only on
Exception
and it's subclasses, without traversing of causes
The builder supports only widely used properties of RetryTemplate
. More
specific properties can be configured directly (after building).
Not thread safe. Building should be performed in a single thread. Also, there is no guarantee that all constructors of all fields are thread safe in-depth (means employing only volatile and final writes), so, in concurrent environment, it is recommended to ensure presence of happens-before between publication and any usage. (e.g. publication via volatile write, or other safe publication technique)
- Since:
- 1.3
- Author:
- Aleksandr Shamukov, Artem Bilan, Kim In Hoi
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()
Finish configuration and build resultingRetryTemplate
.customBackoff
(BackOffPolicy backOffPolicy) You can provide your ownBackOffPolicy
via this method.customPolicy
(RetryPolicy policy) If flexibility of this builder is not enough for you, you can provide your ownRetryPolicy
via this method.exponentialBackoff
(long initialInterval, double multiplier, long maxInterval) Use exponential backoff policy.exponentialBackoff
(long initialInterval, double multiplier, long maxInterval, boolean withRandom) Use exponential backoff policy.fixedBackoff
(long interval) Perform each retry after fixed amount of time.Allows infinite retry, do not limit attempts by number or time.maxAttempts
(int maxAttempts) Limits maximum number of attempts to provided value.Do not pause between attempts, retry immediately.notRetryOn
(Class<? extends Throwable> throwable) Add a throwable to the black list of retryable exceptions.notRetryOn
(List<Class<? extends Throwable>> throwables) Add all throwables to the black list of retryable exceptions.Add a throwable to the while list of retryable exceptions.Add all throwables to the while list of retryable exceptions.Suppose throwing anew MyLogicException(new IOException())
.uniformRandomBackoff
(long minInterval, long maxInterval) UseUniformRandomBackOffPolicy
, see it's doc for details.withinMillis
(long timeout) Allows retry if there is no more thantimeout
millis since first attempt.withListener
(RetryListener listener) Appends providedlistener
toRetryTemplate
's listener list.withListeners
(List<RetryListener> listeners) Appends all providedlisteners
toRetryTemplate
's listener list.
-
Constructor Details
-
RetryTemplateBuilder
public RetryTemplateBuilder()
-
-
Method Details
-
maxAttempts
Limits maximum number of attempts to provided value.Invocation of this method does not discard default exception classification rule, that is "retry only on
Exception
and it's subclasses".- Parameters:
maxAttempts
- includes initial attempt and all retries. E.g: maxAttempts = 3 means one initial attempt and two retries.- Returns:
- this
- See Also:
-
withinMillis
Allows retry if there is no more thantimeout
millis since first attempt.Invocation of this method does not discard default exception classification rule, that is "retry only on
Exception
and it's subclasses".- Parameters:
timeout
- whole execution timeout in milliseconds- Returns:
- this
- See Also:
-
infiniteRetry
Allows infinite retry, do not limit attempts by number or time.Invocation of this method does not discard default exception classification rule, that is "retry only on
Exception
and it's subclasses".- Returns:
- this
- See Also:
-
customPolicy
If flexibility of this builder is not enough for you, you can provide your ownRetryPolicy
via this method.Invocation of this method does not discard default exception classification rule, that is "retry only on
Exception
and it's subclasses".- Parameters:
policy
- will be directly set to resultingRetryTemplate
- Returns:
- this
-
exponentialBackoff
public RetryTemplateBuilder exponentialBackoff(long initialInterval, double multiplier, long maxInterval) Use exponential backoff policy. The formula of backoff period:currentInterval = Math.min(initialInterval * Math.pow(multiplier, retryNum), maxInterval)
(for first attempt retryNum = 0)
- Parameters:
initialInterval
- in millisecondsmultiplier
- see the formula abovemaxInterval
- in milliseconds- Returns:
- this
- See Also:
-
exponentialBackoff
public RetryTemplateBuilder exponentialBackoff(long initialInterval, double multiplier, long maxInterval, boolean withRandom) Use exponential backoff policy. The formula of backoff period (without randomness):currentInterval = Math.min(initialInterval * Math.pow(multiplier, retryNum), maxInterval)
(for first attempt retryNum = 0)
- Parameters:
initialInterval
- in millisecondsmultiplier
- see the formula abovemaxInterval
- in millisecondswithRandom
- adds some randomness to backoff intervals. For details, seeExponentialRandomBackOffPolicy
- Returns:
- this
- See Also:
-
fixedBackoff
Perform each retry after fixed amount of time.- Parameters:
interval
- fixed interval in milliseconds- Returns:
- this
- See Also:
-
uniformRandomBackoff
UseUniformRandomBackOffPolicy
, see it's doc for details.- Parameters:
minInterval
- in millisecondsmaxInterval
- in milliseconds- Returns:
- this
- See Also:
-
noBackoff
Do not pause between attempts, retry immediately.- Returns:
- this
- See Also:
-
customBackoff
You can provide your ownBackOffPolicy
via this method.- Parameters:
backOffPolicy
- will be directly set to resultingRetryTemplate
- Returns:
- this
-
retryOn
Add a throwable to the while list of retryable exceptions.Warn: touching this method drops default
retryOn(Exception.class)
and you should configure whole classifier from scratch.You should select the way you want to configure exception classifier: white list or black list. If you choose white list - use this method, if black - use
notRetryOn(Class)
ornotRetryOn(List)
- Parameters:
throwable
- to be retryable (with it's subclasses)- Returns:
- this
- See Also:
-
notRetryOn
Add a throwable to the black list of retryable exceptions.Warn: touching this method drops default
retryOn(Exception.class)
and you should configure whole classifier from scratch.You should select the way you want to configure exception classifier: white list or black list. If you choose black list - use this method, if white - use
retryOn(Class)
orretryOn(List)
- Parameters:
throwable
- to be not retryable (with it's subclasses)- Returns:
- this
- See Also:
-
retryOn
Add all throwables to the while list of retryable exceptions.Warn: touching this method drops default
retryOn(Exception.class)
and you should configure whole classifier from scratch.You should select the way you want to configure exception classifier: white list or black list. If you choose white list - use this method, if black - use
notRetryOn(Class)
ornotRetryOn(List)
- Parameters:
throwables
- to be retryable (with it's subclasses)- Returns:
- this
- Since:
- 1.3.2
- See Also:
-
notRetryOn
Add all throwables to the black list of retryable exceptions.Warn: touching this method drops default
retryOn(Exception.class)
and you should configure whole classifier from scratch.You should select the way you want to configure exception classifier: white list or black list. If you choose black list - use this method, if white - use
retryOn(Class)
orretryOn(List)
- Parameters:
throwables
- to be not retryable (with it's subclasses)- Returns:
- this
- Since:
- 1.3.2
- See Also:
-
traversingCauses
Suppose throwing anew MyLogicException(new IOException())
. This template will not retry on it:
but this will retry:RetryTemplate.builder() .retryOn(IOException.class) .build()
RetryTemplate.builder() .retryOn(IOException.class) .traversingCauses() .build()
- Returns:
- this
- See Also:
-
withListener
Appends providedlistener
toRetryTemplate
's listener list.- Parameters:
listener
- to be appended- Returns:
- this
- See Also:
-
withListeners
Appends all providedlisteners
toRetryTemplate
's listener list.- Parameters:
listeners
- to be appended- Returns:
- this
- See Also:
-
build
Finish configuration and build resultingRetryTemplate
. For default behaviour and concurrency note see class-level doc ofRetryTemplateBuilder
. TheretryPolicy
of the returnedRetryTemplate
is always an instance ofCompositeRetryPolicy
, that consists of one base policy, and ofBinaryExceptionClassifierRetryPolicy
. The motivation is: whatever base policy we use, exception classification is extremely recommended.- Returns:
- new instance of
RetryTemplate
-