org.springframework.batch.core.step.item
Class ItemSkipPolicyItemHandler

java.lang.Object
  extended by org.springframework.batch.core.step.item.SimpleItemHandler
      extended by org.springframework.batch.core.step.item.ItemSkipPolicyItemHandler
All Implemented Interfaces:
ItemHandler

public class ItemSkipPolicyItemHandler
extends SimpleItemHandler

ItemHandler that implements skip behavior. It delegates to setItemSkipPolicy(ItemSkipPolicy) to decide whether skip should be called or not. If exception is thrown while reading the item, skip is called on the ItemReader. If exception is thrown while writing the item, skip is called on both ItemReader and ItemWriter.

Author:
Dave Syer, Robert Kasanicky

Field Summary
protected  org.apache.commons.logging.Log logger
           
 
Constructor Summary
ItemSkipPolicyItemHandler(ItemReader itemReader, ItemWriter itemWriter)
           
 
Method Summary
protected  void doWriteWithSkip(Object item, StepContribution contribution)
           
 void mark()
          Implementations should delegate to an ItemReader.
protected  Object read(StepContribution contribution)
          Tries to read the item from the reader, in case of exception skip the item if the skip policy allows, otherwise re-throw.
 void registerSkipListener(SkipListener listener)
          Register a listener for callbacks at the appropriate stages in a skip process.
 void setItemKeyGenerator(ItemKeyGenerator itemKeyGenerator)
          Public setter for the ItemKeyGenerator.
 void setItemSkipPolicy(ItemSkipPolicy itemSkipPolicy)
           
 void setSkipCacheCapacity(int skipCacheCapacity)
          Public setter for the capacity of the skipped item cache.
 void setSkipListeners(SkipListener[] listeners)
          Register some SkipListeners with the handler.
protected  void write(Object item, StepContribution contribution)
          Tries to write the item using the writer.
 
Methods inherited from class org.springframework.batch.core.step.item.SimpleItemHandler
clear, doRead, doWrite, flush, handle, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final org.apache.commons.logging.Log logger
Constructor Detail

ItemSkipPolicyItemHandler

public ItemSkipPolicyItemHandler(ItemReader itemReader,
                                 ItemWriter itemWriter)
Parameters:
itemReader -
itemWriter -
Method Detail

setSkipListeners

public void setSkipListeners(SkipListener[] listeners)
Register some SkipListeners with the handler. Each will get the callbacks in the order specified at the correct stage if a skip occurs.

Parameters:
listeners -

registerSkipListener

public void registerSkipListener(SkipListener listener)
Register a listener for callbacks at the appropriate stages in a skip process.

Parameters:
listener - a SkipListener

setItemKeyGenerator

public void setItemKeyGenerator(ItemKeyGenerator itemKeyGenerator)
Public setter for the ItemKeyGenerator. Defaults to just return the item, and since it will be used before a write operation. Implementations must ensure that items always have the same key when they are read from the ItemReader (so if the item is mutable and the reader does any buffering the key generator might need to take care to only use data that do not change on write).

Parameters:
itemKeyGenerator - the ItemKeyGenerator to set. If null resets to default value.

setItemSkipPolicy

public void setItemSkipPolicy(ItemSkipPolicy itemSkipPolicy)
Parameters:
itemSkipPolicy -

setSkipCacheCapacity

public void setSkipCacheCapacity(int skipCacheCapacity)
Public setter for the capacity of the skipped item cache. If a large number of items are failing and not being recognized as skipped, it usually signals a problem with the key generation (often equals and hashCode in the item itself). So it is better to enforce a strict limit than have weird looking errors, where a skip limit is reached without anything being skipped.

Parameters:
skipCacheCapacity - the capacity to set

read

protected Object read(StepContribution contribution)
               throws Exception
Tries to read the item from the reader, in case of exception skip the item if the skip policy allows, otherwise re-throw.

Overrides:
read in class SimpleItemHandler
Parameters:
contribution - current StepContribution holding skipped items count
Returns:
next item for processing
Throws:
Exception

write

protected void write(Object item,
                     StepContribution contribution)
              throws Exception
Tries to write the item using the writer. In case of exception consults skip policy before re-throwing the exception. The exception is always re-thrown, but if the item is seen again on read it will be skipped.

Overrides:
write in class SimpleItemHandler
Parameters:
item - item to write
contribution - current StepContribution holding skipped items count
Throws:
Exception

doWriteWithSkip

protected final void doWriteWithSkip(Object item,
                                     StepContribution contribution)
                              throws Exception
Parameters:
item -
contribution -
Throws:
Exception

mark

public void mark()
          throws MarkFailedException
Description copied from interface: ItemHandler
Implementations should delegate to an ItemReader.

Specified by:
mark in interface ItemHandler
Overrides:
mark in class SimpleItemHandler
Throws:
MarkFailedException
See Also:
ItemReader.mark()


Copyright © 2008 SpringSource. All Rights Reserved.