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
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
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected int
doUpdate
(RepeatContext context) Extension point for subclasses.protected abstract int
getCount
(RepeatContext context) Extension point for subclasses.final 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.start
(RepeatContext parent) Build a newRepeatContextSupport
and return it.final 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
-
Field Details
-
COUNT
Session key for global counter.
-
-
Constructor Details
-
CountingCompletionPolicy
public CountingCompletionPolicy()
-
-
Method Details
-
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
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
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
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:
-
start
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:
-
update
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:
-