public class RetryTemplateBuilder extends Object
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:
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)
Constructor and Description |
---|
RetryTemplateBuilder() |
Modifier and Type | Method and Description |
---|---|
RetryTemplate |
build()
Finish configuration and build resulting
RetryTemplate . |
RetryTemplateBuilder |
customBackoff(BackOffPolicy backOffPolicy)
You can provide your own
BackOffPolicy via this method. |
RetryTemplateBuilder |
customPolicy(RetryPolicy policy)
If flexibility of this builder is not enough for you, you can provide your own
RetryPolicy via this method. |
RetryTemplateBuilder |
exponentialBackoff(long initialInterval,
double multiplier,
long maxInterval)
Use exponential backoff policy.
|
RetryTemplateBuilder |
exponentialBackoff(long initialInterval,
double multiplier,
long maxInterval,
boolean withRandom)
Use exponential backoff policy.
|
RetryTemplateBuilder |
fixedBackoff(long interval)
Perform each retry after fixed amount of time.
|
RetryTemplateBuilder |
infiniteRetry()
Allows infinite retry, do not limit attempts by number or time.
|
RetryTemplateBuilder |
maxAttempts(int maxAttempts)
Limits maximum number of attempts to provided value.
|
RetryTemplateBuilder |
noBackoff()
Do not pause between attempts, retry immediately.
|
RetryTemplateBuilder |
notRetryOn(Class<? extends Throwable> throwable)
Add a throwable to the black list of retryable exceptions.
|
RetryTemplateBuilder |
notRetryOn(List<Class<? extends Throwable>> throwables)
Add all throwables to the black list of retryable exceptions.
|
RetryTemplateBuilder |
retryOn(Class<? extends Throwable> throwable)
Add a throwable to the while list of retryable exceptions.
|
RetryTemplateBuilder |
retryOn(List<Class<? extends Throwable>> throwables)
Add all throwables to the while list of retryable exceptions.
|
RetryTemplateBuilder |
traversingCauses()
Suppose throwing a
new MyLogicException(new IOException()) . |
RetryTemplateBuilder |
uniformRandomBackoff(long minInterval,
long maxInterval)
Use
UniformRandomBackOffPolicy , see it's doc for details. |
RetryTemplateBuilder |
withinMillis(long timeout)
Allows retry if there is no more than
timeout millis since first attempt. |
RetryTemplateBuilder |
withListener(RetryListener listener)
Appends provided
listener to RetryTemplate 's listener list. |
RetryTemplateBuilder |
withListeners(List<RetryListener> listeners)
Appends all provided
listeners to RetryTemplate 's listener list. |
public RetryTemplateBuilder maxAttempts(int maxAttempts)
Invocation of this method does not discard default exception classification rule,
that is "retry only on Exception
and it's subclasses".
maxAttempts
- includes initial attempt and all retries. E.g: maxAttempts = 3
means one initial attempt and two retries.MaxAttemptsRetryPolicy
public RetryTemplateBuilder withinMillis(long timeout)
timeout
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".
timeout
- whole execution timeout in millisecondsTimeoutRetryPolicy
public RetryTemplateBuilder infiniteRetry()
Invocation of this method does not discard default exception classification rule,
that is "retry only on Exception
and it's subclasses".
TimeoutRetryPolicy
public RetryTemplateBuilder customPolicy(RetryPolicy policy)
RetryPolicy
via this method.
Invocation of this method does not discard default exception classification rule,
that is "retry only on Exception
and it's subclasses".
policy
- will be directly set to resulting RetryTemplate
public RetryTemplateBuilder exponentialBackoff(long initialInterval, double multiplier, long maxInterval)
currentInterval = Math.min(initialInterval * Math.pow(multiplier, retryNum), maxInterval)
(for first attempt retryNum = 0)
initialInterval
- in millisecondsmultiplier
- see the formula abovemaxInterval
- in millisecondsExponentialBackOffPolicy
public RetryTemplateBuilder exponentialBackoff(long initialInterval, double multiplier, long maxInterval, boolean withRandom)
currentInterval = Math.min(initialInterval * Math.pow(multiplier, retryNum), maxInterval)
(for first attempt retryNum = 0)
initialInterval
- in millisecondsmultiplier
- see the formula abovemaxInterval
- in millisecondswithRandom
- adds some randomness to backoff intervals. For details, see
ExponentialRandomBackOffPolicy
ExponentialBackOffPolicy
,
ExponentialRandomBackOffPolicy
public RetryTemplateBuilder fixedBackoff(long interval)
interval
- fixed interval in millisecondsFixedBackOffPolicy
public RetryTemplateBuilder uniformRandomBackoff(long minInterval, long maxInterval)
UniformRandomBackOffPolicy
, see it's doc for details.minInterval
- in millisecondsmaxInterval
- in millisecondsUniformRandomBackOffPolicy
public RetryTemplateBuilder noBackoff()
NoBackOffPolicy
public RetryTemplateBuilder customBackoff(BackOffPolicy backOffPolicy)
BackOffPolicy
via this method.backOffPolicy
- will be directly set to resulting RetryTemplate
public RetryTemplateBuilder retryOn(Class<? extends Throwable> throwable)
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)
or notRetryOn(List)
throwable
- to be retryable (with it's subclasses)BinaryExceptionClassifierBuilder.retryOn(java.lang.Class<? extends java.lang.Throwable>)
,
BinaryExceptionClassifier
public RetryTemplateBuilder notRetryOn(Class<? extends Throwable> throwable)
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)
or retryOn(List)
throwable
- to be not retryable (with it's subclasses)BinaryExceptionClassifierBuilder.notRetryOn(java.lang.Class<? extends java.lang.Throwable>)
,
BinaryExceptionClassifier
public RetryTemplateBuilder retryOn(List<Class<? extends Throwable>> throwables)
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)
or notRetryOn(List)
throwables
- to be retryable (with it's subclasses)BinaryExceptionClassifierBuilder.retryOn(java.lang.Class<? extends java.lang.Throwable>)
,
BinaryExceptionClassifier
public RetryTemplateBuilder notRetryOn(List<Class<? extends Throwable>> throwables)
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)
or retryOn(List)
throwables
- to be not retryable (with it's subclasses)BinaryExceptionClassifierBuilder.notRetryOn(java.lang.Class<? extends java.lang.Throwable>)
,
BinaryExceptionClassifier
public RetryTemplateBuilder traversingCauses()
new MyLogicException(new IOException())
. This template
will not retry on it:
RetryTemplate.builder()
.retryOn(IOException.class)
.build()
but this will retry:
RetryTemplate.builder()
.retryOn(IOException.class)
.traversingCauses()
.build()
BinaryExceptionClassifier
public RetryTemplateBuilder withListener(RetryListener listener)
listener
to RetryTemplate
's listener list.listener
- to be appendedRetryTemplate
,
RetryListener
public RetryTemplateBuilder withListeners(List<RetryListener> listeners)
listeners
to RetryTemplate
's listener list.listeners
- to be appendedRetryTemplate
,
RetryListener
public RetryTemplate build()
RetryTemplate
. For default
behaviour and concurrency note see class-level doc of RetryTemplateBuilder
.
The retryPolicy
of the returned RetryTemplate
is always an instance
of CompositeRetryPolicy
, that consists of one base policy, and of
BinaryExceptionClassifierRetryPolicy
. The motivation is: whatever base
policy we use, exception classification is extremely recommended.RetryTemplate
Copyright © 2022 SpringSource. All rights reserved.