public class RetryTemplate extends Object implements 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.
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.
Modifier and Type | Field and Description |
---|---|
protected org.apache.commons.logging.Log |
logger |
Constructor and Description |
---|
RetryTemplate() |
Modifier and Type | Method and Description |
---|---|
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).
|
protected <T,E extends Throwable> |
doExecute(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.
|
<T,E extends Throwable> |
execute(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.
|
<T,E extends Throwable> |
execute(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.
|
<T,E extends Throwable> |
execute(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.
|
<T,E extends Throwable> |
execute(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> recoveryCallback,
RetryContext context,
RetryState state)
Actions to take after final attempt has failed.
|
protected RetryContext |
open(RetryPolicy retryPolicy,
RetryState state)
Delegate to the
RetryPolicy having checked in the cache for an existing
value if the state is not null. |
void |
registerListener(RetryListener listener)
Register an additional listener.
|
protected void |
registerThrowable(RetryPolicy retryPolicy,
RetryState state,
RetryContext context,
Throwable e) |
protected <E extends Throwable> |
rethrow(RetryContext context,
String message) |
void |
setBackOffPolicy(BackOffPolicy backOffPolicy)
Setter for
BackOffPolicy . |
void |
setListeners(RetryListener[] listeners)
Setter for listeners.
|
void |
setRetryContextCache(RetryContextCache retryContextCache)
Public setter for the
RetryContextCache . |
void |
setRetryPolicy(RetryPolicy retryPolicy)
Setter for
RetryPolicy . |
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 a
RetryCallback . |
public void setThrowLastExceptionOnExhausted(boolean throwLastExceptionOnExhausted)
throwLastExceptionOnExhausted
- the throwLastExceptionOnExhausted to setpublic void setRetryContextCache(RetryContextCache retryContextCache)
RetryContextCache
.retryContextCache
- the RetryContextCache
to set.public void setListeners(RetryListener[] listeners)
listeners
- the RetryListener
sRetryListener
public void registerListener(RetryListener listener)
listener
- the RetryListener
setListeners(RetryListener[])
public void setBackOffPolicy(BackOffPolicy backOffPolicy)
BackOffPolicy
.backOffPolicy
- the BackOffPolicy
public void setRetryPolicy(RetryPolicy retryPolicy)
RetryPolicy
.retryPolicy
- the RetryPolicy
public final <T,E extends Throwable> T execute(RetryCallback<T,E> retryCallback) throws E extends Throwable
execute
in interface RetryOperations
T
- the return valueE
- the exception to throwretryCallback
- the RetryCallback
RetryCallback
upon successful
invocation.TerminatedRetryException
- if the retry has been manually terminated by a
listener.E
- any Exception
raised by the
RetryCallback
upon unsuccessful retry.E extends Throwable
RetryOperations.execute(RetryCallback)
public final <T,E extends Throwable> T execute(RetryCallback<T,E> retryCallback, RecoveryCallback<T> recoveryCallback) throws E extends Throwable
execute
in interface RetryOperations
T
- the type to returnE
- the type of the exceptionretryCallback
- the RetryCallback
recoveryCallback
- the RecoveryCallback
RetryCallback
upon successful
invocation, and that returned by the RecoveryCallback
otherwise.TerminatedRetryException
- if the retry has been manually terminated by a
listener.E
- any Exception
raised by theE extends Throwable
RetryOperations.execute(RetryCallback, RecoveryCallback)
public final <T,E extends Throwable> T execute(RetryCallback<T,E> retryCallback, RetryState retryState) throws E extends Throwable, ExhaustedRetryException
execute
in interface RetryOperations
T
- the type of the return valueE
- the type of the exception to returnretryCallback
- the RetryCallback
retryState
- the RetryState
RetryCallback
upon successful
invocation, and that returned by the RecoveryCallback
otherwise.ExhaustedRetryException
- if the retry has been exhausted.E
- any Exception
raised by the RecoveryCallback
.E extends Throwable
RetryOperations.execute(RetryCallback, RetryState)
public final <T,E extends Throwable> T execute(RetryCallback<T,E> retryCallback, RecoveryCallback<T> recoveryCallback, RetryState retryState) throws E extends Throwable, ExhaustedRetryException
execute
in interface RetryOperations
T
- the return value typeE
- the exception typeretryCallback
- the RetryCallback
recoveryCallback
- the RecoveryCallback
retryState
- the RetryState
RetryCallback
upon successful
invocation, and that returned by the RecoveryCallback
otherwise.E
- any Exception
raised by the RecoveryCallback
upon unsuccessful retry.E extends Throwable
ExhaustedRetryException
RetryOperations.execute(RetryCallback, RetryState)
protected <T,E extends Throwable> T doExecute(RetryCallback<T,E> retryCallback, RecoveryCallback<T> recoveryCallback, RetryState state) throws E extends Throwable, ExhaustedRetryException
T
- the type of the return valueE
- the exception type to throwrecoveryCallback
- the RecoveryCallback
retryCallback
- the RetryCallback
state
- the RetryState
ExhaustedRetryException
- if the retry has been exhausted.E
- an exception if the retry operation failsE extends Throwable
RetryOperations.execute(RetryCallback, RecoveryCallback, RetryState)
protected boolean canRetry(RetryPolicy retryPolicy, RetryContext context)
RetryCallback
is executed, but after the backoff and open
interceptors.retryPolicy
- the policy to applycontext
- the current retry contextprotected void close(RetryPolicy retryPolicy, RetryContext context, RetryState state, boolean succeeded)
retryPolicy
- the RetryPolicy
context
- the RetryContext
state
- the RetryState
succeeded
- whether the close succeededprotected void registerThrowable(RetryPolicy retryPolicy, RetryState state, RetryContext context, Throwable e)
protected RetryContext open(RetryPolicy retryPolicy, RetryState state)
RetryPolicy
having checked in the cache for an existing
value if the state is not null.state
- a RetryState
retryPolicy
- a RetryPolicy
to delegate the context creationprotected <T> T handleRetryExhausted(RecoveryCallback<T> recoveryCallback, RetryContext context, RetryState state) throws Throwable
T
- the type to classifyrecoveryCallback
- the callback for recovery (might be null)context
- the current retry contextstate
- the RetryState
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
protected <E extends Throwable> void rethrow(RetryContext context, String message) throws E extends Throwable
E extends Throwable
protected boolean shouldRethrow(RetryPolicy retryPolicy, RetryContext context, RetryState state)
RetryCallback
. Normal stateless behaviour is not to rethrow, and if
there is state we rethrow.retryPolicy
- the retry policycontext
- the current contextstate
- the current retryStateCopyright © 2018 SpringSource. All rights reserved.