Class AbstractItemCountingItemStreamItemReader<T>
java.lang.Object
org.springframework.batch.item.ItemStreamSupport
org.springframework.batch.item.support.AbstractItemStreamItemReader<T>
org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader<T>
- All Implemented Interfaces:
ItemReader<T>,ItemStream,ItemStreamReader<T>
- Direct Known Subclasses:
AbstractCursorItemReader,AbstractPaginatedDataItemReader,AbstractPagingItemReader,AvroItemReader,FlatFileItemReader,JpaCursorItemReader,JsonItemReader,LdifReader,MappingLdifReader,MongoCursorItemReader,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, Glenn Renfro, Mahmoud Ben Hassine
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()If any resources are needed for the stream to operate they need to be destroyed here.protected abstract voiddoClose()Close the resources opened indoOpen().protected abstract voiddoOpen()Open resources necessary to start reading input.protected abstract TdoRead()Read next item from input.intReturns the current item count.booleanThe flag that determines whether to save internal state for restarts.protected voidjumpToItem(int itemIndex) Move to the given item index.voidopen(ExecutionContext executionContext) Open the stream for the providedExecutionContext.read()Reads a piece of input data and advance to the next one.voidsetCurrentItemCount(int count) The index of the item to start reading from.voidsetMaxItemCount(int count) The maximum index of the items to be read.voidsetSaveState(boolean saveState) Set the flag that determines whether to save internal data forExecutionContext.voidupdate(ExecutionContext executionContext) Indicates that the execution context provided during open is about to be saved.Methods inherited from class org.springframework.batch.item.ItemStreamSupport
getExecutionContextKey, getName, setExecutionContextName, setName
-
Constructor Details
-
AbstractItemCountingItemStreamItemReader
public AbstractItemCountingItemStreamItemReader()
-
-
Method Details
-
doRead
Read next item from input.- Returns:
- an item or
nullif the data source is exhausted - Throws:
Exception- Allows subclasses to throw checked exceptions for interpretation by the framework
-
doOpen
Open resources necessary to start reading input.- Throws:
Exception- Allows subclasses to throw checked exceptions for interpretation by the framework
-
doClose
Close the resources opened indoOpen().- Throws:
Exception- Allows subclasses to throw checked exceptions for interpretation by the framework
-
jumpToItem
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 usingdoRead().- Parameters:
itemIndex- index of item (0 based) to jump to.- Throws:
Exception- Allows subclasses to throw checked exceptions for interpretation by the framework
-
read
Description copied from interface:ItemReaderReads a piece of input data and advance to the next one. Implementations must returnnullat 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.- Returns:
- T the item to be processed or
nullif the data source is exhausted - 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
public int getCurrentItemCount()Returns the current item count.- Returns:
- the current item count
- Since:
- 5.1
-
setCurrentItemCount
public void setCurrentItemCount(int count) The index of the item to start reading from. If theExecutionContextcontains a key[name].read.count(where[name]is the name of this component) the value from theExecutionContextwill be used in preference.- Parameters:
count- the value of the current item count- See Also:
-
setMaxItemCount
public void setMaxItemCount(int count) The maximum index of the items to be read. If theExecutionContextcontains a key[name].read.count.max(where[name]is the name of this component) the value from theExecutionContextwill be used in preference.- Parameters:
count- the value of the maximum item count. count must be greater than zero.- See Also:
-
close
Description copied from interface:ItemStreamIf 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.- Throws:
ItemStreamException
-
open
Description copied from interface:ItemStreamOpen the stream for the providedExecutionContext.- Parameters:
executionContext- current step'sExecutionContext. Will be the executionContext from the last run of the step on a restart.- Throws:
ItemStreamException
-
update
Description copied from interface:ItemStreamIndicates 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.- Parameters:
executionContext- to be updated- Throws:
ItemStreamException
-
setSaveState
public void setSaveState(boolean saveState) Set the flag that determines whether to save internal data forExecutionContext. 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
-