org.springframework.batch.item.support
Class AbstractBufferedItemReaderItemStream

java.lang.Object
  extended by org.springframework.batch.item.support.AbstractBufferedItemReaderItemStream
All Implemented Interfaces:
ItemReader, ItemStream
Direct Known Subclasses:
FlatFileItemReader, HibernateCursorItemReader, JdbcCursorItemReader, StaxEventItemReader

public abstract class AbstractBufferedItemReaderItemStream
extends Object
implements ItemReader, ItemStream

Abstract superclass for ItemReaders which use item buffering to support reset/rollback. 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
AbstractBufferedItemReaderItemStream()
           
 
Method Summary
 void close(ExecutionContext executionContext)
          If any resources are needed for the stream to operate they need to be destroyed here.
protected abstract  void doClose()
          Close the resources opened in doOpen().
protected abstract  void doOpen()
          Open resources necessary to start reading input.
protected abstract  Object doRead()
          Read next item from input.
protected  int getCurrentItemCount()
           
protected  void jumpToItem(int itemIndex)
          Move to the given item index.
 void mark()
          Mark is supported as long as this ItemStream is used in a single-threaded environment.
 void open(ExecutionContext executionContext)
          Open the stream for the provided ExecutionContext.
 Object read()
          Reads a piece of input data and advance to the next one.
 void reset()
          Reset the stream to the last mark.
protected  void setCurrentItemCount(int count)
           
 void setName(String name)
           
 void setSaveState(boolean saveState)
          Set the flag that determines whether to save internal data for ExecutionContext.
 void update(ExecutionContext executionContext)
          Indicates that the execution context provided during open is about to be saved.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractBufferedItemReaderItemStream

public AbstractBufferedItemReaderItemStream()
Method Detail

doRead

protected abstract Object 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 Object read()
            throws Exception,
                   UnexpectedInputException,
                   NoWorkFoundException,
                   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.

Specified by:
read in interface ItemReader
Throws:
Exception - if an underlying resource is unavailable.
UnexpectedInputException
NoWorkFoundException
ParseException

mark

public void mark()
          throws MarkFailedException
Mark is supported as long as this ItemStream is used in a single-threaded environment. The state backing the mark is a single counter, keeping track of the current position, so multiple threads cannot be accommodated.

Specified by:
mark in interface ItemReader
Throws:
MarkFailedException - if there is a problem with the mark. If a mark fails inside a transaction, it would be worrying, but not normally fatal.
See Also:
AbstractItemReader.mark()

reset

public void reset()
           throws ResetFailedException
Description copied from interface: ItemReader
Reset the stream to the last mark. After a reset the stream state will be such that changes (items read or written) since the last call to mark will not be visible after a call to close.

Specified by:
reset in interface ItemReader
Throws:
ResetFailedException - if there is a problem with the reset. If a reset fails inside a transaction, it would normally be fatal, and would leave the stream in an inconsistent state. So while this is an unchecked exception, it may be important for a client to catch it explicitly.

getCurrentItemCount

protected int getCurrentItemCount()

setCurrentItemCount

protected void setCurrentItemCount(int count)

close

public void close(ExecutionContext executionContext)
           throws ItemStreamException
Description copied from interface: ItemStream
If any resources are needed for the stream to operate they need to be destroyed here. Once this method has been called all other methods (except open) may throw an exception.

Specified by:
close in interface ItemStream
Parameters:
executionContext - the current execution context in case it is needed
Throws:
ItemStreamException

open

public void open(ExecutionContext executionContext)
          throws ItemStreamException
Description copied from interface: ItemStream
Open the stream for the provided ExecutionContext.

Specified by:
open in interface ItemStream
Throws:
ItemStreamException

update

public void update(ExecutionContext executionContext)
            throws ItemStreamException
Description copied from interface: ItemStream
Indicates that the execution context provided during open is about to be saved. If any state is remaining, but has not been put in the context, it should be added here.

Specified by:
update in interface ItemStream
Parameters:
executionContext - to be updated
Throws:
ItemStreamException

setName

public void setName(String name)

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.

Parameters:
saveState - flag value (default true).


Copyright © 2009 SpringSource. All Rights Reserved.