org.springframework.batch.repeat.policy
Class CountingCompletionPolicy

java.lang.Object
  extended by org.springframework.batch.repeat.policy.CompletionPolicySupport
      extended by org.springframework.batch.repeat.policy.DefaultResultCompletionPolicy
          extended by 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
protected  class CountingCompletionPolicy.CountingBatchContext
           
 
Field Summary
static String COUNT
          Session key for global counter.
 
Constructor Summary
CountingCompletionPolicy()
           
 
Method Summary
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 new RepeatContextSupport 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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COUNT

public static final String COUNT
Session key for global counter.

Constructor Detail

CountingCompletionPolicy

public CountingCompletionPolicy()
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 if setUseParent(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 interface CompletionPolicy
Overrides:
isComplete in class DefaultResultCompletionPolicy
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 new RepeatContextSupport and return it.

Specified by:
start in interface CompletionPolicy
Overrides:
start in class CompletionPolicySupport
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 interface CompletionPolicy
Overrides:
update in class CompletionPolicySupport
Parameters:
context - the value returned by start.
See Also:
CompletionPolicy.update(org.springframework.batch.repeat.RepeatContext)


Copyright © 2013 SpringSource. All Rights Reserved.