Java Reference
In-Depth Information
item.getTransactions().add((Transaction) curItem);
curItem = null;
}
}
return item;
}
public Object peek() throws Exception, UnexpectedInputException,
ParseException {
if (curItem == null) {
curItem = delegate.read();
}
return curItem;
}
public void setDelegate(ItemStreamReader<Object> delegate) {
this.delegate = delegate;
}
public void close() throws ItemStreamException {
delegate.close();
}
public void open(ExecutionContext arg0) throws ItemStreamException {
delegate.open(arg0);
}
public void update(ExecutionContext arg0) throws ItemStreamException {
delegate.update(arg0);
}
}
The CustomerFileReader has two key methods that you should look at. The first is the read()
method. This method is responsible for implementing the logic involved in reading and assembling a
single Customer item including its child transaction records. It does so by reading in a customer record
from the file you are reading. It then reads the related transaction records until the next record is the
next customer record. Once the next customer record is found, the current customer is considered
complete and returned by your ItemReader. This type of logic is called control break logic.
The other method of consequence is the peak method. This method is used to read ahead while still
working on the current Customer. It caches the current record. If the record has been read but not
processed, it will return the same record again. If the record has been processed (indicated to this
method by setting curItem to null), it will read in the next record 2 .
You should notice that your custom ItemReader does not implement the ItemReader interface.
Instead, it implements on of its subinterfaces, the ItemStreamReader interface. The reason for this is
that when using one of the Spring Batch ItemReader implementations, they handle the opening and
2 It is important to note that there is an ItemReader subiterface called the
org.springframework.batch.item.PeekableItemReader<T>. Since the CustomerFileReader not firmly
meet the contract defined by that interface here we do not implement it.
 
Search WWH ::




Custom Search