Class CountingCompletionPolicy
- java.lang.Object
- org.springframework.batch.repeat.policy.CompletionPolicySupport
- org.springframework.batch.repeat.policy.DefaultResultCompletionPolicy
- org.springframework.batch.repeat.policy.CountingCompletionPolicy
- All Implemented Interfaces:
CompletionPolicy
public abstract class CountingCompletionPolicy extends DefaultResultCompletionPolicy
Abstract base class for policies that need to count the number of occurrences of some event (e.g. an exception type in the context), and terminate based on a limit for the counter. The value of the counter can be stored between batches in a nested context, so that the termination decision is based on the aggregate of a number of sibling batches.- Author:
- Dave Syer
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
CountingCompletionPolicy.CountingBatchContext
Field Summary
Fields Modifier and Type Field Description static java.lang.String
COUNT
Session key for global counter.
Constructor Summary
Constructors Constructor Description CountingCompletionPolicy()
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected int
doUpdate(RepeatContext context)
Extension point for subclasses.protected abstract int
getCount(RepeatContext context)
Extension point for subclasses.boolean
isComplete(RepeatContext context)
Always false.void
setMaxCount(int maxCount)
Setter for maximum value of count before termination.void
setUseParent(boolean useParent)
Flag to indicate whether the count is at the level of the parent context, or just local to the context.RepeatContext
start(RepeatContext parent)
Build a newRepeatContextSupport
and return it.void
update(RepeatContext context)
Increment the context so the counter is up to date.Methods inherited from class org.springframework.batch.repeat.policy.DefaultResultCompletionPolicy
isComplete
Method Detail
setUseParent
public void setUseParent(boolean useParent)
Flag to indicate whether the count is at the level of the parent context, or just local to the context. If true then the count is aggregated among siblings in a nested batch.- Parameters:
useParent
- whether to use the parent context to cache the total count. Default value is false.
setMaxCount
public void setMaxCount(int maxCount)
Setter for maximum value of count before termination.- Parameters:
maxCount
- the maximum number of counts before termination. Default 0 so termination is immediate.
getCount
protected abstract int getCount(RepeatContext context)
Extension point for subclasses. Obtain the value of the count in the current context. Subclasses can count the number of attempts or violations and store the result in their context. This policy base class will take care of the termination contract and aggregating at the level of the session if required.- Parameters:
context
- the current context, specific to the subclass.- Returns:
- the value of the counter in the context.
doUpdate
protected int doUpdate(RepeatContext context)
Extension point for subclasses. Inspect the context and update the state of a counter in whatever way is appropriate. This will be added to the session-level counter ifsetUseParent(boolean)
is true.- Parameters:
context
- the current context.- Returns:
- the change in the value of the counter (default 0).
isComplete
public final boolean isComplete(RepeatContext context)
Description copied from class:DefaultResultCompletionPolicy
Always false.- Specified by:
isComplete
in interfaceCompletionPolicy
- Overrides:
isComplete
in classDefaultResultCompletionPolicy
- Parameters:
context
- the current batch context.- Returns:
- true if the batch should terminate.
- See Also:
CompletionPolicy.isComplete(org.springframework.batch.repeat.RepeatContext)
start
public RepeatContext start(RepeatContext parent)
Description copied from class:CompletionPolicySupport
Build a newRepeatContextSupport
and return it.- Specified by:
start
in interfaceCompletionPolicy
- Overrides:
start
in classCompletionPolicySupport
- Parameters:
parent
- the current context if one is already in progress.- Returns:
- a context object that can be used by the implementation to store internal state for a batch.
- See Also:
CompletionPolicy.start(RepeatContext)
update
public final void update(RepeatContext context)
Description copied from class:CompletionPolicySupport
Increment the context so the counter is up to date. Do nothing else.- Specified by:
update
in interfaceCompletionPolicy
- Overrides:
update
in classCompletionPolicySupport
- Parameters:
context
- the value returned by start.- See Also:
CompletionPolicy.update(org.springframework.batch.repeat.RepeatContext)