Java Reference
In-Depth Information
Custom Input
Spring Batch provides readers for just about every type of input Java applications normally face, however
if you are using a form of input that Spring Batch provides an ItemReader, you will need to create one
yourself. Implementing the ItemReader interface's read() method is the easy part. However, what
happens when you need to be able to restart your reader? How do you maintain state across executions?
This section will look at how to implement an ItemReader that can handle state across executions.
As mentioned, implementing Spring Batch's ItemReader interface is actually quite simple. In fact,
with a small tweak, you can convert the CustomerService you used in the previous section to an
ItemReader. All you need to do is implement the interface and rename the method getCustomer() to
read() . Listing 7-56 shows the updated code.
Listing 7-56. CustomerItemReader
package com.apress.springbatch.chapter7;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.springframework.batch.item.ItemReader;
public class CustomerItemReader implements ItemReader<Customer> {
private List<Customer> customers;
private int curIndex;
private String [] firstNames = {"Michael", "Warren", "Ann", "Terrence",
"Erica", "Laura", "Steve", "Larry"};
private String middleInitial = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private String [] lastNames = {"Gates", "Darrow", "Donnelly", "Jobs",
"Buffett", "Ellison", "Obama"};
private String [] streets = {"4th Street", "Wall Street", "Fifth Avenue",
"Mt. Lee Drive", "Jeopardy Lane",
"Infinite Loop Drive", "Farnam Street",
"Isabella Ave", "S. Greenwood Ave"};
private String [] cities = {"Chicago", "New York", "Hollywood", "Aurora",
"Omaha", "Atherton"};
private String [] states = {"IL", "NY", "CA", "NE"};
private Random generator = new Random();
public CustomerItemReader () {
curIndex = 0;
customers = new ArrayList<Customer>();
for(int i = 0; i < 100; i++) {
Search WWH ::

Custom Search