public class RetryTemplate extends java.lang.Object implements RetryOperations
RetryCallback
interface and are executed using one of the supplied execute methods. Exception or subclass of
Exception. This behaviour can be changed by using the
setRetryPolicy(RetryPolicy) method. setRetryPolicy(RetryPolicy) and setBackOffPolicy(BackOffPolicy)
properties. The BackOffPolicy controls how
long the pause is between each individual retry attempt. BackOffPolicy used and no in progress
retryable operations will be affected.| Modifier and Type | Field and Description |
|---|---|
private BackOffPolicy |
backOffPolicy |
private RetryListener[] |
listeners |
protected org.apache.commons.logging.Log |
logger |
private RetryContextCache |
retryContextCache |
private RetryPolicy |
retryPolicy |
private boolean |
throwLastExceptionOnExhausted |
| 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).
|
private <T,E extends java.lang.Throwable> |
doCloseInterceptors(RetryCallback<T,E> callback,
RetryContext context,
java.lang.Throwable lastException) |
protected <T,E extends java.lang.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.
|
private <T,E extends java.lang.Throwable> |
doOnErrorInterceptors(RetryCallback<T,E> callback,
RetryContext context,
java.lang.Throwable throwable) |
private <T,E extends java.lang.Throwable> |
doOpenInterceptors(RetryCallback<T,E> callback,
RetryContext context) |
private RetryContext |
doOpenInternal(RetryPolicy retryPolicy) |
<T,E extends java.lang.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 java.lang.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 java.lang.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 java.lang.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,
java.lang.Throwable e) |
protected <E extends java.lang.Throwable> |
rethrow(RetryContext context,
java.lang.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. |
private static <E extends java.lang.Throwable> |
wrapIfNecessary(java.lang.Throwable throwable)
Re-throws the original throwable if it is an Exception, and wraps non-exceptions into
RetryException. |
protected final org.apache.commons.logging.Log logger
private volatile BackOffPolicy backOffPolicy
private volatile RetryPolicy retryPolicy
private volatile RetryListener[] listeners
private RetryContextCache retryContextCache
private boolean throwLastExceptionOnExhausted
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 - RetryListenerpublic void registerListener(RetryListener listener)
listener - setListeners(RetryListener[])public void setBackOffPolicy(BackOffPolicy backOffPolicy)
BackOffPolicy.backOffPolicy - public void setRetryPolicy(RetryPolicy retryPolicy)
RetryPolicy.retryPolicy - public final <T,E extends java.lang.Throwable> T execute(RetryCallback<T,E> retryCallback) throws E extends java.lang.Throwable
execute in interface RetryOperationsRetryCallback upon successful
invocation.TerminatedRetryException - if the retry has been manually terminated by a
listener.E extends java.lang.ThrowableRetryOperations.execute(RetryCallback)public final <T,E extends java.lang.Throwable> T execute(RetryCallback<T,E> retryCallback, RecoveryCallback<T> recoveryCallback) throws E extends java.lang.Throwable
execute in interface RetryOperationsRetryCallback upon successful
invocation, and that returned by the RecoveryCallback otherwise.TerminatedRetryException - if the retry has been manually terminated by a
listener.E extends java.lang.ThrowableRetryOperations.execute(RetryCallback, RecoveryCallback)public final <T,E extends java.lang.Throwable> T execute(RetryCallback<T,E> retryCallback, RetryState retryState) throws E extends java.lang.Throwable, ExhaustedRetryException
execute in interface RetryOperationsRetryCallback upon successful
invocation, and that returned by the RecoveryCallback otherwise.ExhaustedRetryException - if the retry has been exhausted.E extends java.lang.ThrowableRetryOperations.execute(RetryCallback, RetryState)public final <T,E extends java.lang.Throwable> T execute(RetryCallback<T,E> retryCallback, RecoveryCallback<T> recoveryCallback, RetryState retryState) throws E extends java.lang.Throwable, ExhaustedRetryException
execute in interface RetryOperationsRetryCallback upon successful
invocation, and that returned by the RecoveryCallback otherwise.E extends java.lang.ThrowableExhaustedRetryExceptionRetryOperations.execute(RetryCallback, RetryState)protected <T,E extends java.lang.Throwable> T doExecute(RetryCallback<T,E> retryCallback, RecoveryCallback<T> recoveryCallback, RetryState state) throws E extends java.lang.Throwable, ExhaustedRetryException
ExhaustedRetryException - if the retry has been exhausted. finally {E extends java.lang.ThrowableRetryOperations.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)
context - state - succeeded - protected void registerThrowable(RetryPolicy retryPolicy, RetryState state, RetryContext context, java.lang.Throwable e)
retryPolicy - state - context - e - protected RetryContext open(RetryPolicy retryPolicy, RetryState state)
RetryPolicy having checked in the cache for an existing
value if the state is not null.retryPolicy - a RetryPolicy to delegate the context creationprivate RetryContext doOpenInternal(RetryPolicy retryPolicy)
retryPolicy - protected <T> T handleRetryExhausted(RecoveryCallback<T> recoveryCallback, RetryContext context, RetryState state) throws java.lang.Throwable
recoveryCallback - the callback for recovery (might be null)context - the current retry contextjava.lang.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
callbackjava.lang.Throwableprotected <E extends java.lang.Throwable> void rethrow(RetryContext context, java.lang.String message) throws E extends java.lang.Throwable
E extends java.lang.Throwableprotected boolean shouldRethrow(RetryPolicy retryPolicy, RetryContext context, RetryState state)
RetryCallback. Normal stateless behaviour is not to rethrow, and if
there is state we rethrow.retryPolicy - context - the current contextprivate <T,E extends java.lang.Throwable> boolean doOpenInterceptors(RetryCallback<T,E> callback, RetryContext context)
private <T,E extends java.lang.Throwable> void doCloseInterceptors(RetryCallback<T,E> callback, RetryContext context, java.lang.Throwable lastException)
private <T,E extends java.lang.Throwable> void doOnErrorInterceptors(RetryCallback<T,E> callback, RetryContext context, java.lang.Throwable throwable)
private static <E extends java.lang.Throwable> E wrapIfNecessary(java.lang.Throwable throwable)
throws RetryException
RetryException.RetryException