org.springframework.batch.retry.support
Class RetryTemplate

java.lang.Object
  extended by org.springframework.batch.retry.support.RetryTemplate
All Implemented Interfaces:
RetryOperations

public class RetryTemplate
extends Object
implements RetryOperations

Template class that simplifies the execution of operations with retry semantics.
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.

Author:
Rob Harrop, Dave Syer

Field Summary
protected  Log logger
           
 
Constructor Summary
RetryTemplate()
           
 
Method Summary
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, Exception 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final Log logger
Constructor Detail

RetryTemplate

public RetryTemplate()
Method Detail

setRetryContextCache

public void setRetryContextCache(RetryContextCache retryContextCache)
Public setter for the RetryContextCache.

Parameters:
retryContextCache - the RetryContextCache to set.

setListeners

public void setListeners(RetryListener[] listeners)
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).

Parameters:
listeners -
See Also:
RetryListener

registerListener

public void registerListener(RetryListener listener)
Register an additional listener.

Parameters:
listener -
See Also:
setListeners(RetryListener[])

setBackOffPolicy

public void setBackOffPolicy(BackOffPolicy backOffPolicy)
Setter for BackOffPolicy.

Parameters:
backOffPolicy -

setRetryPolicy

public void setRetryPolicy(RetryPolicy retryPolicy)
Setter for RetryPolicy.

Parameters:
retryPolicy -

execute

public final <T> T execute(RetryCallback<T> retryCallback)
                throws Exception
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.

Specified by:
execute in interface RetryOperations
Returns:
the value returned by the RetryCallback upon successful invocation.
Throws:
TerminatedRetryException - if the retry has been manually terminated by a listener.
Exception - any Exception raised by the RetryCallback upon unsuccessful retry.
See Also:
RetryOperations.execute(RetryCallback)

execute

public final <T> T execute(RetryCallback<T> retryCallback,
                           RecoveryCallback<T> recoveryCallback)
                throws Exception
Keep executing the callback until it either succeeds or the policy dictates that we stop, in which case the recovery callback will be executed.

Specified by:
execute in interface RetryOperations
Returns:
the value returned by the RetryCallback upon successful invocation, and that returned by the RecoveryCallback otherwise.
Throws:
TerminatedRetryException - if the retry has been manually terminated by a listener.
Exception - any Exception raised by the RecoveryCallback upon unsuccessful retry.
See Also:
RetryOperations.execute(RetryCallback, RecoveryCallback)

execute

public final <T> T execute(RetryCallback<T> retryCallback,
                           RetryState retryState)
                throws Exception,
                       ExhaustedRetryException
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.

Specified by:
execute in interface RetryOperations
Returns:
the value returned by the RetryCallback upon successful invocation, and that returned by the RecoveryCallback otherwise.
Throws:
ExhaustedRetryException - if the retry has been exhausted.
Exception - any Exception raised by the RecoveryCallback.
See Also:
RetryOperations.execute(RetryCallback, RetryState)

execute

public final <T> T execute(RetryCallback<T> retryCallback,
                           RecoveryCallback<T> recoveryCallback,
                           RetryState retryState)
                throws Exception,
                       ExhaustedRetryException
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.

Specified by:
execute in interface RetryOperations
Returns:
the value returned by the RetryCallback upon successful invocation, and that returned by the RecoveryCallback otherwise.
Throws:
Exception - any Exception raised by the RecoveryCallback upon unsuccessful retry.
ExhaustedRetryException
See Also:
RetryOperations.execute(RetryCallback, RetryState)

doExecute

protected <T> T doExecute(RetryCallback<T> retryCallback,
                          RecoveryCallback<T> recoveryCallback,
                          RetryState state)
               throws Exception,
                      ExhaustedRetryException
Execute the callback once if the policy dictates that we can, otherwise execute the recovery callback.

Throws:
ExhaustedRetryException - if the retry has been exhausted.
Exception
See Also:
RetryOperations.execute(RetryCallback, RecoveryCallback, RetryState)

canRetry

protected boolean canRetry(RetryPolicy retryPolicy,
                           RetryContext context)
Decide whether to proceed with the ongoing retry attempt. This method is called before the RetryCallback is executed, but after the backoff and open interceptors.

Parameters:
retryPolicy - the policy to apply
context - the current retry context
Returns:
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).

Parameters:
context -
state -
succeeded -

registerThrowable

protected void registerThrowable(RetryPolicy retryPolicy,
                                 RetryState state,
                                 RetryContext context,
                                 Exception e)
Parameters:
retryPolicy -
state -
context -
e -

open

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.

Parameters:
retryPolicy - a RetryPolicy to delegate the context creation
Returns:
a retry context, either a new one or the one used last time the same state was encountered

handleRetryExhausted

protected <T> T handleRetryExhausted(RecoveryCallback<T> recoveryCallback,
                                     RetryContext context,
                                     RetryState state)
                          throws Exception
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.

Parameters:
recoveryCallback - the callback for recovery (might be null)
context - the current retry context
Throws:
Exception - if the callback does, and if there is no callback and the state is null then the last exception from the context
ExhaustedRetryException - if the state is not null and there is no recovery callback

shouldRethrow

protected boolean shouldRethrow(RetryPolicy retryPolicy,
                                RetryContext context,
                                RetryState state)
Extension point for subclasses to decide on behaviour after catching an exception in a RetryCallback. Normal stateless behaviour is not to rethrow, and if there is state we rethrow.

Parameters:
retryPolicy -
context - the current context
Returns:
true if the state is not null but subclasses might choose otherwise


Copyright © 2009 SpringSource. All Rights Reserved.