类 RetryTemplate
- 所有已实现的接口:
RetryOperations
Retryable operations are encapsulated in implementations of the RetryCallback
interface and are executed using one of the supplied execute methods.
By default, an operation is retried if is throws any Exception
or subclass of
Exception
. This behaviour can be changed by using the
setRetryPolicy(RetryPolicy)
method.
Also by default, each operation is retried for a maximum of three attempts with no back
off in between. This behaviour can be configured using the
setRetryPolicy(RetryPolicy)
and setBackOffPolicy(BackOffPolicy)
properties. The BackOffPolicy
controls how
long the pause is between each individual retry attempt.
A new instance can be fluently configured via builder()
, e.g:
RetryTemplate.builder()
.maxAttempts(10)
.fixedBackoff(1000)
.build();
See RetryTemplateBuilder
for more examples and details.
This class is thread-safe and suitable for concurrent access when executing operations
and when performing configuration changes. As such, it is possible to change the number
of retries on the fly, as well as the BackOffPolicy
used and no in progress
retryable operations will be affected.
- 从以下版本开始:
- 4.0
- 作者:
- Rob Harrop, Dave Syer, Gary Russell, Artem Bilan, Josh Long, Aleksandr Shamukov, Harry Yang
-
字段概要
字段修饰符和类型字段说明private BackOffPolicy
private static final String
Retry context attribute name that indicates the context should be considered global state (never closed).private RetryListener[]
protected final infra.logging.Logger
private RetryContextCache
private RetryPolicy
private boolean
-
构造器概要
构造器 -
方法概要
修饰符和类型方法说明static RetryTemplateBuilder
builder()
Main entry point to configure RetryTemplate using fluent API.protected boolean
canRetry
(RetryPolicy retryPolicy, RetryContext context) Decide whether to proceed with the ongoing retry attempt.protected void
close
(RetryPolicy retryPolicy, RetryContext context, RetryState state, boolean succeeded) Clean up the cache if necessary and close the context provided (if the flag indicates that processing was successful).static RetryTemplate
Creates a new default instance.private <T,
E extends Throwable>
voiddoCloseInterceptors
(RetryCallback<T, E> callback, RetryContext context, Throwable lastException) protected <T,
E extends Throwable>
TdoExecute
(RetryCallback<T, E> retryCallback, RecoveryCallback<T> recoveryCallback, RetryState state) Execute the callback once if the policy dictates that we can, otherwise execute the recovery callback.private <T,
E extends Throwable>
voiddoOnErrorInterceptors
(RetryCallback<T, E> callback, RetryContext context, Throwable throwable) private <T,
E extends Throwable>
voiddoOnSuccessInterceptors
(RetryCallback<T, E> callback, RetryContext context, T result) private <T,
E extends Throwable>
booleandoOpenInterceptors
(RetryCallback<T, E> callback, RetryContext context) private RetryContext
doOpenInternal
(RetryPolicy retryPolicy) private RetryContext
doOpenInternal
(RetryPolicy retryPolicy, RetryState state) final <T,
E extends Throwable>
Texecute
(RetryCallback<T, E> retryCallback) Keep executing the callback until it either succeeds or the policy dictates that we stop, in which case the most recent exception thrown by the callback will be rethrown.final <T,
E extends Throwable>
Texecute
(RetryCallback<T, E> retryCallback, RecoveryCallback<T> recoveryCallback) Keep executing the callback until it either succeeds or the policy dictates that we stop, in which case the recovery callback will be executed.final <T,
E extends Throwable>
Texecute
(RetryCallback<T, E> retryCallback, RecoveryCallback<T> recoveryCallback, RetryState retryState) Execute the callback once if the policy dictates that we can, re-throwing any exception encountered so that clients can re-present the same task later.final <T,
E extends Throwable>
Texecute
(RetryCallback<T, E> retryCallback, RetryState retryState) Execute the callback once if the policy dictates that we can, re-throwing any exception encountered so that clients can re-present the same task later.protected <T> T
handleRetryExhausted
(RecoveryCallback<T> recovery, RetryContext context, RetryState state) Actions to take after final attempt has failed.boolean
Return true if at least one listener is registered.protected RetryContext
open
(RetryPolicy retryPolicy, RetryState state) Delegate to theRetryPolicy
having checked in the cache for an existing value if the state is not null.private void
registerContext
(RetryContext context, RetryState state) void
registerListener
(RetryListener listener) Register an additional listener at the end of the list.void
registerListener
(RetryListener listener, int index) Register an additional listener at the specified index.protected void
registerThrowable
(RetryPolicy retryPolicy, RetryState state, RetryContext context, Throwable e) protected <E extends Throwable>
voidrethrow
(RetryContext context, String message, boolean wrap) void
setBackOffPolicy
(BackOffPolicy backOffPolicy) Setter forBackOffPolicy
.void
setListeners
(RetryListener[] listeners) Setter for listeners.void
setRetryContextCache
(RetryContextCache retryContextCache) Public setter for theRetryContextCache
.void
setRetryPolicy
(RetryPolicy retryPolicy) Setter forRetryPolicy
.void
setThrowLastExceptionOnExhausted
(boolean throwLastExceptionOnExhausted) protected boolean
shouldRethrow
(RetryPolicy retryPolicy, RetryContext context, RetryState state) Extension point for subclasses to decide on behaviour after catching an exception in aRetryCallback
.private static <E extends Throwable>
EwrapIfNecessary
(Throwable throwable) Re-throws the original throwable if it is an Exception, and wraps non-exceptions intoRetryException
.
-
字段详细资料
-
GLOBAL_STATE
Retry context attribute name that indicates the context should be considered global state (never closed). TODO: convert this to a flag in the RetryState.- 另请参阅:
-
logger
protected final infra.logging.Logger logger -
backOffPolicy
-
retryPolicy
-
listeners
-
retryContextCache
-
throwLastExceptionOnExhausted
private boolean throwLastExceptionOnExhausted
-
-
构造器详细资料
-
RetryTemplate
public RetryTemplate()
-
-
方法详细资料
-
builder
Main entry point to configure RetryTemplate using fluent API. SeeRetryTemplateBuilder
for usage examples and details.- 返回:
- a new instance of RetryTemplateBuilder with preset default behaviour, that can be overwritten during manual configuration
-
defaultInstance
Creates a new default instance. The properties of default instance are described inRetryTemplateBuilder
documentation.- 返回:
- a new instance of RetryTemplate with default behaviour
-
setThrowLastExceptionOnExhausted
public void setThrowLastExceptionOnExhausted(boolean throwLastExceptionOnExhausted) - 参数:
throwLastExceptionOnExhausted
- the throwLastExceptionOnExhausted to set
-
setRetryContextCache
Public setter for theRetryContextCache
.- 参数:
retryContextCache
- theRetryContextCache
to set.
-
setListeners
Setter for listeners. The listeners are executed before and after a retry block (i.e. before and after all the attempts), and on an error (every attempt).- 参数:
listeners
- theRetryListener
s- 另请参阅:
-
registerListener
Register an additional listener at the end of the list.- 参数:
listener
- theRetryListener
- 另请参阅:
-
registerListener
Register an additional listener at the specified index.- 参数:
listener
- theRetryListener
index
- the position in the list.- 另请参阅:
-
hasListeners
public boolean hasListeners()Return true if at least one listener is registered.- 返回:
- true if listeners present.
-
setBackOffPolicy
Setter forBackOffPolicy
.- 参数:
backOffPolicy
- theBackOffPolicy
-
setRetryPolicy
Setter forRetryPolicy
.- 参数:
retryPolicy
- theRetryPolicy
-
execute
Keep executing the callback until it either succeeds or the policy dictates that we stop, in which case the most recent exception thrown by the callback will be rethrown.- 指定者:
execute
在接口中RetryOperations
- 类型参数:
T
- the return valueE
- the exception to throw- 参数:
retryCallback
- theRetryCallback
- 返回:
- the value returned by the
RetryCallback
upon successful invocation. - 抛出:
TerminatedRetryException
- if the retry has been manually terminated by a listener.E
- anyException
raised by theRetryCallback
upon unsuccessful retry.- 另请参阅:
-
execute
public final <T,E extends Throwable> T execute(RetryCallback<T, E> retryCallback, RecoveryCallback<T> recoveryCallback) throws EKeep executing the callback until it either succeeds or the policy dictates that we stop, in which case the recovery callback will be executed.- 指定者:
execute
在接口中RetryOperations
- 类型参数:
T
- the type to returnE
- the type of the exception- 参数:
retryCallback
- theRetryCallback
recoveryCallback
- theRecoveryCallback
- 返回:
- the value returned by the
RetryCallback
upon successful invocation, and that returned by theRecoveryCallback
otherwise. - 抛出:
TerminatedRetryException
- if the retry has been manually terminated by a listener.E
- anyException
raised by the unsuccessful retry.- 另请参阅:
-
execute
public final <T,E extends Throwable> T execute(RetryCallback<T, E> retryCallback, RetryState retryState) throws E, ExhaustedRetryExceptionExecute the callback once if the policy dictates that we can, re-throwing any exception encountered so that clients can re-present the same task later.- 指定者:
execute
在接口中RetryOperations
- 类型参数:
T
- the type of the return valueE
- the type of the exception to return- 参数:
retryCallback
- theRetryCallback
retryState
- theRetryState
- 返回:
- the value returned by the
RetryCallback
upon successful invocation, and that returned by theRecoveryCallback
otherwise. - 抛出:
ExhaustedRetryException
- if the retry has been exhausted.E
- anyException
raised by theRecoveryCallback
.- 另请参阅:
-
execute
public final <T,E extends Throwable> T execute(RetryCallback<T, E> retryCallback, RecoveryCallback<T> recoveryCallback, RetryState retryState) throws E, ExhaustedRetryExceptionExecute the callback once if the policy dictates that we can, re-throwing any exception encountered so that clients can re-present the same task later.- 指定者:
execute
在接口中RetryOperations
- 类型参数:
T
- the return value typeE
- the exception type- 参数:
retryCallback
- theRetryCallback
recoveryCallback
- theRecoveryCallback
retryState
- theRetryState
- 返回:
- the value returned by the
RetryCallback
upon successful invocation, and that returned by theRecoveryCallback
otherwise. - 抛出:
E
- anyException
raised by theRecoveryCallback
upon unsuccessful retry.ExhaustedRetryException
- 另请参阅:
-
doExecute
protected <T,E extends Throwable> T doExecute(RetryCallback<T, E> retryCallback, RecoveryCallback<T> recoveryCallback, RetryState state) throws E, ExhaustedRetryExceptionExecute the callback once if the policy dictates that we can, otherwise execute the recovery callback.- 类型参数:
T
- the type of the return valueE
- the exception type to throw- 参数:
recoveryCallback
- theRecoveryCallback
retryCallback
- theRetryCallback
state
- theRetryState
- 返回:
- T the retried value
- 抛出:
ExhaustedRetryException
- if the retry has been exhausted.E
- an exception if the retry operation fails- 另请参阅:
-
canRetry
Decide whether to proceed with the ongoing retry attempt. This method is called before theRetryCallback
is executed, but after the backoff and open interceptors.- 参数:
retryPolicy
- the policy to applycontext
- the current retry context- 返回:
- true if we can continue with the attempt
-
close
protected void close(RetryPolicy retryPolicy, RetryContext context, RetryState state, boolean succeeded) Clean up the cache if necessary and close the context provided (if the flag indicates that processing was successful).- 参数:
retryPolicy
- theRetryPolicy
context
- theRetryContext
state
- theRetryState
succeeded
- whether the close succeeded
-
registerThrowable
protected void registerThrowable(RetryPolicy retryPolicy, RetryState state, RetryContext context, Throwable e) -
registerContext
-
open
Delegate to theRetryPolicy
having checked in the cache for an existing value if the state is not null.- 参数:
state
- aRetryState
retryPolicy
- aRetryPolicy
to delegate the context creation- 返回:
- a retry context, either a new one or the one used last time the same state was encountered
-
doOpenInternal
-
doOpenInternal
-
handleRetryExhausted
protected <T> T handleRetryExhausted(RecoveryCallback<T> recovery, RetryContext context, RetryState state) throws Throwable Actions to take after final attempt has failed. If there is state clean up the cache. If there is a recovery callback, execute that and return its result. Otherwise throw an exception.- 类型参数:
T
- the type to classify- 参数:
recovery
- the callback for recovery (might be null)context
- the current retry contextstate
- theRetryState
- 返回:
- T the payload to return
- 抛出:
Exception
- if the callback does, and if there is no callback and the state is null then the last exception from the contextExhaustedRetryException
- if the state is not null and there is no recovery callbackThrowable
- if there is an error
-
rethrow
protected <E extends Throwable> void rethrow(RetryContext context, String message, boolean wrap) throws E - 抛出:
E extends Throwable
-
shouldRethrow
Extension point for subclasses to decide on behaviour after catching an exception in aRetryCallback
. Normal stateless behaviour is not to rethrow, and if there is state we rethrow.- 参数:
retryPolicy
- the retry policycontext
- the current contextstate
- the current retryState- 返回:
- true if the state is not null but subclasses might choose otherwise
-
doOpenInterceptors
private <T,E extends Throwable> boolean doOpenInterceptors(RetryCallback<T, E> callback, RetryContext context) -
doCloseInterceptors
private <T,E extends Throwable> void doCloseInterceptors(RetryCallback<T, E> callback, RetryContext context, Throwable lastException) -
doOnSuccessInterceptors
private <T,E extends Throwable> void doOnSuccessInterceptors(RetryCallback<T, E> callback, RetryContext context, T result) -
doOnErrorInterceptors
private <T,E extends Throwable> void doOnErrorInterceptors(RetryCallback<T, E> callback, RetryContext context, Throwable throwable) -
wrapIfNecessary
Re-throws the original throwable if it is an Exception, and wraps non-exceptions intoRetryException
.- 抛出:
RetryException
-