Class SingleItemPeekableItemReader<T>
- java.lang.Object
- org.springframework.batch.item.support.SingleItemPeekableItemReader<T>
- All Implemented Interfaces:
ItemReader<T>,ItemStream,ItemStreamReader<T>,PeekableItemReader<T>
public class SingleItemPeekableItemReader<T> extends java.lang.Object implements ItemStreamReader<T>, PeekableItemReader<T>
A
PeekableItemReaderthat allows the user to peek one item ahead. Repeated calls topeek()will return the same item, and this will be the next item returned fromread().Intentionally not thread-safe: it wouldn't be possible to honour the peek in multiple threads because only one of the threads that peeked would get that item in the next call to read.
- Author:
- Dave Syer
Constructor Summary
Constructors Constructor Description SingleItemPeekableItemReader()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()If the delegate is anItemStream, just pass the call on, otherwise reset the peek cache.voidopen(ExecutionContext executionContext)If the delegate is anItemStream, just pass the call on, otherwise reset the peek cache.Tpeek()Peek at the next item, ensuring that if the delegate is anItemStreamthe state is stored for the next call toupdate(ExecutionContext).Tread()Get the next item from the delegate (whether or not it has already been peeked at).voidsetDelegate(ItemReader<T> delegate)The item reader to use as a delegate.voidupdate(ExecutionContext executionContext)If there is a cached peek, then retrieve the execution context state from that point.
Method Detail
setDelegate
public void setDelegate(ItemReader<T> delegate)
The item reader to use as a delegate. Items are read from the delegate and passed to the caller inread().- Parameters:
delegate- the delegate to set
read
public T read() throws java.lang.Exception, UnexpectedInputException, ParseException
Get the next item from the delegate (whether or not it has already been peeked at).- Specified by:
readin interfaceItemReader<T>- 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.java.lang.Exception- if an there is a non-specific error.- See Also:
ItemReader.read()
peek
public T peek() throws java.lang.Exception, UnexpectedInputException, ParseException
Peek at the next item, ensuring that if the delegate is anItemStreamthe state is stored for the next call toupdate(ExecutionContext).- Specified by:
peekin interfacePeekableItemReader<T>- Returns:
- the next item (or null if there is none).
- Throws:
java.lang.Exception- if there is a problemUnexpectedInputExceptionParseException- See Also:
PeekableItemReader.peek()
close
public void close() throws ItemStreamExceptionIf the delegate is anItemStream, just pass the call on, otherwise reset the peek cache.- Specified by:
closein interfaceItemStream- Throws:
ItemStreamException- if there is a problem- See Also:
ItemStream.close()
open
public void open(ExecutionContext executionContext) throws ItemStreamException
If the delegate is anItemStream, just pass the call on, otherwise reset the peek cache.- Specified by:
openin interfaceItemStream- Parameters:
executionContext- the current context- Throws:
ItemStreamException- if there is a problem- See Also:
ItemStream.open(ExecutionContext)
update
public void update(ExecutionContext executionContext) throws ItemStreamException
If there is a cached peek, then retrieve the execution context state from that point. If there is no peek cached, then call directly to the delegate.- Specified by:
updatein interfaceItemStream- Parameters:
executionContext- the current context- Throws:
ItemStreamException- if there is a problem- See Also:
ItemStream.update(ExecutionContext)