Java Reference
In-Depth Information
private Customer buildCustomer() {
Customer customer = new Customer();
customer.setFirstName(
firstNames[generator.nextInt(firstNames.length - 1)]);
customer.setMiddleInitial(
String.valueOf(middleInitial.charAt(
generator.nextInt(middleInitial.length() - 1))));
customer.setLastName(
lastNames[generator.nextInt(lastNames.length - 1)]);
customer.setAddress(generator.nextInt(9999) + " " +
streets[generator.nextInt(streets.length - 1)]);
customer.setCity(cities[generator.nextInt(cities.length - 1)]);
customer.setState(states[generator.nextInt(states.length - 1)]);
customer.setZip(String.valueOf(generator.nextInt(99999)));
return customer;
}
public Customer read() {
Customer cust = null;
if(curIndex < customers.size()) {
cust = customers.get(curIndex);
curIndex++;
}
return cust;
}
}
Even if you ignore the fact that your CustomerItemReader builds a new list with each run, the
CustomerItemReader as it is written in Listing 7-56 will restart at the beginning of your list each time the
job is executed. Although this will be the behavior you want in many cases, it will not always be the case.
Instead, if there is an error after processing half a million records out of a million, you will want to start
over again in that same chunk.
To provide the ability for Spring Batch to maintain the state of your reader in the jobRepository and
restart your reader where you left off, you need to implement an additional interface, the ItemStream
interface. Shown in Listing 7-57, the ItemStream interface consists of three methods: open , update , and
close .
Listing 7-57. The ItemStream Interface
package org.springframework.batch.item;
public interface ItemStream {
void open(ExecutionContext executionContext) throws ItemStreamException;
void update(ExecutionContext executionContext) throws ItemStreamException;
void close() throws ItemStreamException;
}
Search WWH ::




Custom Search