Class RepeatTemplate
java.lang.Object
org.springframework.batch.repeat.support.RepeatTemplate
- All Implemented Interfaces:
RepeatOperations
- Direct Known Subclasses:
TaskExecutorRepeatTemplate
Simple implementation and base class for batch templates implementing
N.B. the template accumulates thrown exceptions during the iteration, and they are all processed together when the main loop ends (i.e. finished processing the items). Clients that do not want to stop execution when an exception is thrown can use a specific
Clients that want to take some business action when an exception is thrown by the
RepeatOperations. Provides a framework including interceptors and policies.
Subclasses just need to provide a method that gets the next result and one that waits
for all the results to be returned from concurrent processes or threads.N.B. the template accumulates thrown exceptions during the iteration, and they are all processed together when the main loop ends (i.e. finished processing the items). Clients that do not want to stop execution when an exception is thrown can use a specific
CompletionPolicy that does not finish when exceptions are received.
This is not the default behaviour.Clients that want to take some business action when an exception is thrown by the
RepeatCallback can consider using a custom RepeatListener instead of
trying to customise the CompletionPolicy. This is generally a friendlier
interface to implement, and the
RepeatListener.after(RepeatContext, RepeatStatus) method is passed in the
result of the callback, which would be an instance of Throwable if the business
processing had thrown an exception. If the exception is not to be propagated to the
caller, then a non-default CompletionPolicy needs to be provided as well, but
that could be off the shelf, with the business action implemented only in the
interceptor.- Author:
- Dave Syer, Mahmoud Ben Hassine
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected final booleancanContinue(RepeatStatus value) Check return value from batch operation.protected RepeatInternalStatecreateInternalState(RepeatContext context) Create an internal state object that is used to store data needed internally in the scope of an iteration.protected voidexecuteAfterInterceptors(RepeatContext context, RepeatStatus value) Convenience method to execute after interceptors on a callback result.protected RepeatStatusgetNextResult(RepeatContext context, RepeatCallback callback, RepeatInternalState state) Get the next completed result, possibly executing several callbacks until one finally finishes.protected booleanisComplete(RepeatContext context) Delegate toCompletionPolicy.protected booleanisComplete(RepeatContext context, RepeatStatus result) Delegate to theCompletionPolicy.iterate(RepeatCallback callback) Execute the batch callback until the completion policy decides that we are finished.voidregisterListener(RepeatListener listener) Register an additional listener.voidsetCompletionPolicy(CompletionPolicy terminationPolicy) Setter for policy to decide when the batch is complete.voidsetExceptionHandler(ExceptionHandler exceptionHandler) Setter for exception handler strategy.voidsetListeners(RepeatListener[] listeners) Set the listeners for this template, registering them for callbacks at appropriate times in the iteration.protected RepeatContextstart()Delegate to theCompletionPolicy.protected voidupdate(RepeatContext context) Delegate to theCompletionPolicy.protected booleanIf necessary, wait for results to come back from remote or concurrent processes.
-
Field Details
-
logger
protected org.apache.commons.logging.Log logger
-
-
Constructor Details
-
RepeatTemplate
public RepeatTemplate()
-
-
Method Details
-
setListeners
Set the listeners for this template, registering them for callbacks at appropriate times in the iteration.- Parameters:
listeners- listeners to be used
-
registerListener
Register an additional listener.- Parameters:
listener- a single listener to be added to the list
-
setExceptionHandler
Setter for exception handler strategy. The exception handler is called at the end of a batch, after theCompletionPolicyhas determined that the batch is complete. By default all exceptions are re-thrown.- Parameters:
exceptionHandler- theExceptionHandlerto use.- See Also:
-
setCompletionPolicy
Setter for policy to decide when the batch is complete. The default is to complete normally when the callback returns aRepeatStatuswhich is not marked as continuable, and abnormally when the callback throws an exception (but the decision to re-throw the exception is deferred to theExceptionHandler).- Parameters:
terminationPolicy- a TerminationPolicy.- Throws:
IllegalArgumentException- if the argument is null- See Also:
-
iterate
Execute the batch callback until the completion policy decides that we are finished. Wait for the whole batch to finish before returning even if the task executor is asynchronous.- Specified by:
iteratein interfaceRepeatOperations- Parameters:
callback- the batch callback.- Returns:
- the aggregate of the result of all the callback operations. An indication
of whether the
RepeatOperationscan continue processing if this method is called again. - See Also:
-
createInternalState
Create an internal state object that is used to store data needed internally in the scope of an iteration. Used by subclasses to manage the queueing and retrieval of asynchronous results. The default just provides an accumulation of Throwable instances for processing at the end of the batch.- Parameters:
context- the currentRepeatContext- Returns:
- a
RepeatInternalStateinstance. - See Also:
-
getNextResult
protected RepeatStatus getNextResult(RepeatContext context, RepeatCallback callback, RepeatInternalState state) throws Throwable Get the next completed result, possibly executing several callbacks until one finally finishes. Normally a subclass would have to override both this method andcreateInternalState(RepeatContext)because the implementation of this method would rely on the details of the internal state.- Parameters:
context- current BatchContext.callback- the callback to execute.state- maintained by the implementation.- Returns:
- a finished result.
- Throws:
Throwable- any Throwable emitted during the iteration- See Also:
-
waitForResults
If necessary, wait for results to come back from remote or concurrent processes. By default does nothing and returns true.- Parameters:
state- the internal state.- Returns:
- true if
canContinue(RepeatStatus)is true for all results retrieved.
-
canContinue
Check return value from batch operation.- Parameters:
value- the last callback result.- Returns:
- true if the value is
RepeatStatus.CONTINUABLE.
-
executeAfterInterceptors
Convenience method to execute after interceptors on a callback result.- Parameters:
context- the current batch context.value- the result of the callback to process.
-
isComplete
Delegate to theCompletionPolicy.- Parameters:
context- the current batch context.result- the result of the latest batch item processing.- Returns:
- true if complete according to policy and result value, else false.
- See Also:
-
isComplete
Delegate toCompletionPolicy.- Parameters:
context- the current batch context.- Returns:
- true if complete according to policy alone not including result value, else false.
- See Also:
-
start
Delegate to theCompletionPolicy.- Returns:
- a
RepeatContextobject that can be used by the implementation to store internal state for a batch step. - See Also:
-
update
Delegate to theCompletionPolicy.- Parameters:
context- the value returned by start.- See Also:
-