Java Reference
In-Depth Information
CustomerFileReader
FlatFileItemReader
read
read
while the next record is not a
customer record
read
return Customer
Figure 7-3. CustomerFileReader flow
As Figure 7-3 shows, your read method will begin by determining if a Customer object has already
been read. If it hasn't, it will attempt to read one from the FlatFileItemReader. Assuming you read a
record (you won't have read one once you reach the end of the file), you will initialize the transaction
List on the Customer object. While the next record you read is a Transaction, you will add it to the
Customer object. Listing 7-26 shows the implementation of the CustomerFileReader.
Listing 7-26. CustomerFileReader
package com.apress.springbatch.chapter7;
import Java.util.ArrayList;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.ItemStreamReader;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
public class CustomerFileReader implements ItemStreamReader<Object> {
private Object curItem = null;
private ItemStreamReader<Object> delegate;
public Object read() throws Exception {
if(curItem == null) {
curItem = (Customer) delegate.read();
}
Customer item = (Customer) curItem;
curItem = null;
if(item != null) {
item.setTransactions(new ArrayList<Transaction>());
while(peek() instanceof Transaction) {
 
 
Search WWH ::




Custom Search