org.springframework.batch.item.support
Class AbstractItemCountingItemStreamItemReader<T>

java.lang.Object
  extended by org.springframework.batch.item.ItemStreamSupport
      extended by org.springframework.batch.item.support.AbstractItemStreamItemReader<T>
          extended by org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader<T>
All Implemented Interfaces:
ItemReader<T>, ItemStream
Direct Known Subclasses:
AbstractCursorItemReader, AbstractPaginatedDataItemReader, AbstractPagingItemReader, FlatFileItemReader, HibernateCursorItemReader, RepositoryItemReader, StaxEventItemReader

public abstract class AbstractItemCountingItemStreamItemReader<T>
extends AbstractItemStreamItemReader<T>

Abstract superclass for ItemReaders that supports restart by storing item count in the ExecutionContext (therefore requires item ordering to be preserved between runs). Subclasses are inherently *not* thread-safe.

Author:
Robert Kasanicky

Constructor Summary
AbstractItemCountingItemStreamItemReader()
           
 
Method Summary
 void close()
          No-op.
protected abstract  void doClose()
          Close the resources opened in doOpen().
protected abstract  void doOpen()
          Open resources necessary to start reading input.
protected abstract  T doRead()
          Read next item from input.
protected  int getCurrentItemCount()
           
 boolean isSaveState()
          The flag that determines whether to save internal state for restarts.
protected  void jumpToItem(int itemIndex)
          Move to the given item index.
 void open(ExecutionContext executionContext)
          No-op.
 T read()
          Reads a piece of input data and advance to the next one.
 void setCurrentItemCount(int count)
          The index of the item to start reading from.
 void setMaxItemCount(int count)
          The maximum index of the items to be read.
 void setName(String name)
          The name of the component which will be used as a stem for keys in the ExecutionContext.
 void setSaveState(boolean saveState)
          Set the flag that determines whether to save internal data for ExecutionContext.
 void update(ExecutionContext executionContext)
          Return empty ExecutionContext.
 
Methods inherited from class org.springframework.batch.item.ItemStreamSupport
getExecutionContextKey, setExecutionContextName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractItemCountingItemStreamItemReader

public AbstractItemCountingItemStreamItemReader()
Method Detail

doRead

protected abstract T doRead()
                     throws Exception
Read next item from input.

Returns:
item
Throws:
Exception

doOpen

protected abstract void doOpen()
                        throws Exception
Open resources necessary to start reading input.

Throws:
Exception

doClose

protected abstract void doClose()
                         throws Exception
Close the resources opened in doOpen().

Throws:
Exception

jumpToItem

protected void jumpToItem(int itemIndex)
                   throws Exception
Move to the given item index. Subclasses should override this method if there is a more efficient way of moving to given index than re-reading the input using doRead().

Throws:
Exception

read

public final T read()
             throws Exception,
                    UnexpectedInputException,
                    ParseException
Description copied from interface: ItemReader
Reads a piece of input data and advance to the next one. Implementations must return null at the end of the input data set. In a transactional setting, caller might get the same item twice from successive calls (or otherwise), if the first call was in a transaction that rolled back.

Throws:
ParseException - if there is a problem parsing the current record (but the next one may still be valid)
NonTransientResourceException - if there is a fatal exception in the underlying resource. After throwing this exception implementations should endeavour to return null from subsequent calls to read.
UnexpectedInputException - if there is an uncategorised problem with the input data. Assume potentially transient, so subsequent calls to read might succeed.
Exception - if an there is a non-specific error.

getCurrentItemCount

protected int getCurrentItemCount()

setCurrentItemCount

public void setCurrentItemCount(int count)
The index of the item to start reading from. If the ExecutionContext contains a key [name].read.count (where [name] is the name of this component) the value from the ExecutionContext will be used in preference.

Parameters:
count - the value of the current item count
See Also:
setName(String)

setMaxItemCount

public void setMaxItemCount(int count)
The maximum index of the items to be read. If the ExecutionContext contains a key [name].read.count.max (where [name] is the name of this component) the value from the ExecutionContext will be used in preference.

Parameters:
count - the value of the maximum item count
See Also:
setName(String)

close

public void close()
           throws ItemStreamException
Description copied from class: ItemStreamSupport
No-op.

Specified by:
close in interface ItemStream
Overrides:
close in class ItemStreamSupport
Throws:
ItemStreamException
See Also:
ItemStream.close()

open

public void open(ExecutionContext executionContext)
          throws ItemStreamException
Description copied from class: ItemStreamSupport
No-op.

Specified by:
open in interface ItemStream
Overrides:
open in class ItemStreamSupport
Throws:
ItemStreamException
See Also:
ItemStream.open(ExecutionContext)

update

public void update(ExecutionContext executionContext)
            throws ItemStreamException
Description copied from class: ItemStreamSupport
Return empty ExecutionContext.

Specified by:
update in interface ItemStream
Overrides:
update in class ItemStreamSupport
Parameters:
executionContext - to be updated
Throws:
ItemStreamException
See Also:
ItemStream.update(ExecutionContext)

setName

public void setName(String name)
The name of the component which will be used as a stem for keys in the ExecutionContext. Subclasses should provide a default value, e.g. the short form of the class name.

Parameters:
name - the name for the component

setSaveState

public void setSaveState(boolean saveState)
Set the flag that determines whether to save internal data for ExecutionContext. Only switch this to false if you don't want to save any state from this stream, and you don't need it to be restartable. Always set it to false if the reader is being used in a concurrent environment.

Parameters:
saveState - flag value (default true).

isSaveState

public boolean isSaveState()
The flag that determines whether to save internal state for restarts.

Returns:
true if the flag was set


Copyright © 2013 SpringSource. All Rights Reserved.