public class RetryTemplate extends 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 |
---|---|
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> T |
doExecute(RetryCallback<T> retryCallback,
RecoveryCallback<T> recoveryCallback,
RetryState state)
Execute the callback once if the policy dictates that we can, otherwise
execute the recovery callback.
|
<T> T |
execute(RetryCallback<T> 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> T |
execute(RetryCallback<T> 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> T |
execute(RetryCallback<T> 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> T |
execute(RetryCallback<T> 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) |
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 . |
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 setRetryContextCache(RetryContextCache retryContextCache)
RetryContextCache
.retryContextCache
- the RetryContextCache
to set.public void setListeners(RetryListener[] listeners)
listeners
- RetryListener
public void registerListener(RetryListener listener)
listener
- setListeners(RetryListener[])
public void setBackOffPolicy(BackOffPolicy backOffPolicy)
BackOffPolicy
.backOffPolicy
- public void setRetryPolicy(RetryPolicy retryPolicy)
RetryPolicy
.retryPolicy
- public final <T> T execute(RetryCallback<T> retryCallback) throws Exception
execute
in interface RetryOperations
RetryCallback
upon successful
invocation.TerminatedRetryException
- if the retry has been manually
terminated by a listener.Exception
- any Exception
raised by the
RetryCallback
upon unsuccessful retry.RetryOperations.execute(RetryCallback)
public final <T> T execute(RetryCallback<T> retryCallback, RecoveryCallback<T> recoveryCallback) throws Exception
execute
in interface RetryOperations
RetryCallback
upon successful
invocation, and that returned by the RecoveryCallback
otherwise.TerminatedRetryException
- if the retry has been manually
terminated by a listener.Exception
- any Exception
raised by the
RecoveryCallback
upon unsuccessful retry.RetryOperations.execute(RetryCallback, RecoveryCallback)
public final <T> T execute(RetryCallback<T> retryCallback, RetryState retryState) throws Exception, ExhaustedRetryException
execute
in interface RetryOperations
RetryCallback
upon successful
invocation, and that returned by the RecoveryCallback
otherwise.ExhaustedRetryException
- if the retry has been exhausted.Exception
- any Exception
raised by the
RecoveryCallback
.RetryOperations.execute(RetryCallback, RetryState)
public final <T> T execute(RetryCallback<T> retryCallback, RecoveryCallback<T> recoveryCallback, RetryState retryState) throws Exception, ExhaustedRetryException
execute
in interface RetryOperations
RetryCallback
upon successful
invocation, and that returned by the RecoveryCallback
otherwise.Exception
- any Exception
raised by the
RecoveryCallback
upon unsuccessful retry.ExhaustedRetryException
RetryOperations.execute(RetryCallback, RetryState)
protected <T> T doExecute(RetryCallback<T> retryCallback, RecoveryCallback<T> recoveryCallback, RetryState state) throws Exception, ExhaustedRetryException
ExhaustedRetryException
- if the retry has been exhausted.Exception
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)
context
- state
- succeeded
- protected void registerThrowable(RetryPolicy retryPolicy, RetryState state, RetryContext context, 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 creationprotected <T> T handleRetryExhausted(RecoveryCallback<T> recoveryCallback, RetryContext context, RetryState state) throws Exception
recoveryCallback
- the callback for recovery (might be null)context
- the current retry contextException
- 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 callbackprotected 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 contextCopyright © 2013 SpringSource. All rights reserved.