类 RepeatTemplate
- java.lang.Object
- org.springframework.batch.repeat.support.RepeatTemplate
- 所有已实现的接口:
RepeatOperations
- 直接已知子类:
TaskExecutorRepeatTemplate
public class RepeatTemplate extends java.lang.Object implements RepeatOperations
Simple implementation and base class for batch templates implementingRepeatOperations
. 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 specificCompletionPolicy
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 theRepeatCallback
can consider using a customRepeatListener
instead of trying to customise theCompletionPolicy
. This is generally a friendlier interface to implement, and theRepeatListener.after(RepeatContext, RepeatStatus)
method is passed in the result of the callback, which would be an instance ofThrowable
if the business processing had thrown an exception. If the exception is not to be propagated to the caller, then a non-defaultCompletionPolicy
needs to be provided as well, but that could be off the shelf, with the business action implemented only in the interceptor.- 作者:
- Dave Syer
字段概要
字段 修饰符和类型 字段 说明 protected org.apache.commons.logging.Log
logger
构造器概要
构造器 构造器 说明 RepeatTemplate()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected boolean
canContinue(RepeatStatus value)
Check return value from batch operation.protected RepeatInternalState
createInternalState(RepeatContext context)
Create an internal state object that is used to store data needed internally in the scope of an iteration.protected void
executeAfterInterceptors(RepeatContext context, RepeatStatus value)
Convenience method to execute after interceptors on a callback result.protected RepeatStatus
getNextResult(RepeatContext context, RepeatCallback callback, RepeatInternalState state)
Get the next completed result, possibly executing several callbacks until one finally finishes.protected boolean
isComplete(RepeatContext context)
Delegate toCompletionPolicy
.protected boolean
isComplete(RepeatContext context, RepeatStatus result)
Delegate to theCompletionPolicy
.RepeatStatus
iterate(RepeatCallback callback)
Execute the batch callback until the completion policy decides that we are finished.void
registerListener(RepeatListener listener)
Register an additional listener.void
setCompletionPolicy(CompletionPolicy terminationPolicy)
Setter for policy to decide when the batch is complete.void
setExceptionHandler(ExceptionHandler exceptionHandler)
Setter for exception handler strategy.void
setListeners(RepeatListener[] listeners)
Set the listeners for this template, registering them for callbacks at appropriate times in the iteration.protected RepeatContext
start()
Delegate to theCompletionPolicy
.protected void
update(RepeatContext context)
Delegate to theCompletionPolicy
.protected boolean
waitForResults(RepeatInternalState state)
If necessary, wait for results to come back from remote or concurrent processes.
字段详细资料
logger
protected org.apache.commons.logging.Log logger
构造器详细资料
RepeatTemplate
public RepeatTemplate()
方法详细资料
setListeners
public void setListeners(RepeatListener[] listeners)
Set the listeners for this template, registering them for callbacks at appropriate times in the iteration.- 参数:
listeners
- listeners to be used
registerListener
public void registerListener(RepeatListener listener)
Register an additional listener.- 参数:
listener
- a single listener to be added to the list
setExceptionHandler
public void setExceptionHandler(ExceptionHandler exceptionHandler)
Setter for exception handler strategy. The exception handler is called at the end of a batch, after theCompletionPolicy
has determined that the batch is complete. By default all exceptions are re-thrown.- 参数:
exceptionHandler
- theExceptionHandler
to use.- 另请参阅:
ExceptionHandler
,DefaultExceptionHandler
,setCompletionPolicy(CompletionPolicy)
setCompletionPolicy
public void setCompletionPolicy(CompletionPolicy terminationPolicy)
Setter for policy to decide when the batch is complete. The default is to complete normally when the callback returns aRepeatStatus
which 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
).- 参数:
terminationPolicy
- a TerminationPolicy.- 抛出:
java.lang.IllegalArgumentException
- if the argument is null- 另请参阅:
setExceptionHandler(ExceptionHandler)
iterate
public RepeatStatus iterate(RepeatCallback callback)
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.- 指定者:
iterate
在接口中RepeatOperations
- 参数:
callback
- the batch callback.- 返回:
- the aggregate of the result of all the callback operations. An indication of whether the
RepeatOperations
can continue processing if this method is called again. - 另请参阅:
RepeatOperations.iterate(org.springframework.batch.repeat.RepeatCallback)
createInternalState
protected RepeatInternalState createInternalState(RepeatContext context)
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.- 参数:
context
- the currentRepeatContext
- 返回:
- a
RepeatInternalState
instance. - 另请参阅:
waitForResults(RepeatInternalState)
getNextResult
protected RepeatStatus getNextResult(RepeatContext context, RepeatCallback callback, RepeatInternalState state) throws java.lang.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.- 参数:
context
- current BatchContext.callback
- the callback to execute.state
- maintained by the implementation.- 返回:
- a finished result.
- 抛出:
java.lang.Throwable
- any Throwable emitted during the iteration- 另请参阅:
isComplete(RepeatContext)
,createInternalState(RepeatContext)
waitForResults
protected boolean waitForResults(RepeatInternalState state)
If necessary, wait for results to come back from remote or concurrent processes. By default does nothing and returns true.- 参数:
state
- the internal state.- 返回:
- true if
canContinue(RepeatStatus)
is true for all results retrieved.
canContinue
protected final boolean canContinue(RepeatStatus value)
Check return value from batch operation.- 参数:
value
- the last callback result.- 返回:
- true if the value is
RepeatStatus.CONTINUABLE
.
executeAfterInterceptors
protected void executeAfterInterceptors(RepeatContext context, RepeatStatus value)
Convenience method to execute after interceptors on a callback result.- 参数:
context
- the current batch context.value
- the result of the callback to process.
isComplete
protected boolean isComplete(RepeatContext context, RepeatStatus result)
Delegate to theCompletionPolicy
.- 参数:
context
- the current batch context.result
- the result of the latest batch item processing.- 返回:
- true if complete according to policy and result value, else false.
- 另请参阅:
CompletionPolicy.isComplete(RepeatContext, RepeatStatus)
isComplete
protected boolean isComplete(RepeatContext context)
Delegate toCompletionPolicy
.- 参数:
context
- the current batch context.- 返回:
- true if complete according to policy alone not including result value, else false.
- 另请参阅:
CompletionPolicy.isComplete(RepeatContext)
start
protected RepeatContext start()
Delegate to theCompletionPolicy
.- 返回:
- a
RepeatContext
object that can be used by the implementation to store internal state for a batch step. - 另请参阅:
CompletionPolicy.start(RepeatContext)
update
protected void update(RepeatContext context)
Delegate to theCompletionPolicy
.- 参数:
context
- the value returned by start.- 另请参阅:
CompletionPolicy.update(RepeatContext)