Java Reference
In-Depth Information
Since you can't do much without the orders you will process, Step 1 of your job is to generate orders
for processing. The next section discusses the code and configuration required to make this happen.
Preloading Data for Processing
Without a storefront to sell things, you will need to build your own orders for processing. To do that, you
will create an ItemReader implementation that generates random orders based upon hard coded data.
While you wouldn't do this in production, it will allow you to setup the data required to test the rest of
your job. In this section we will code and configure the components required to generate test data.
Let's start by looking at the OrderGenerator ItemReader implementation. To generate test files for
most of the jobs in this topic, I wrote Ruby scripts (included in the topic's source code) to generate data
much faster than I could do it by hand. This class isn't much more than a Java equivalent of those Ruby
scripts. Listing 11-7 shows the code for the OrderGenerator .
Listing 11-7. OrderGenerator
package com.apress.springbatch.chapter11processor;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import org.springframework.batch.item.ItemReader;
import com.apress.springbatch.chapter11.domain.Customer;
import com.apress.springbatch.chapter11.domain.Order;
import com.apress.springbatch.chapter11.domain.OrderItem;
public class OrderGenerator implements ItemReader<Order> {
private static final String [] STREETS = {"Second", "Third", "Fourth", "Park", "Fifth"};
private static final String[] CITIES = {"Franklin", "Clinton", "Springfield",
"Greenville"};
private static final String[] FIRST_NAME = {"Jacob", "Ethan", "Michael", "Alexander"};
private static final String[] LAST_NAME = {"Smith", "Jones", "Thompson", "Williams"};
private static final String[] STATES = {"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE"};
private Random generator = new Random();
private DateFormat formatter = new SimpleDateFormat("MM/yy");
private int counter = 0;
public Order read() throws Exception {
if(counter < 100) {
Order curOrder = new Order();
curOrder.setCreditCardNumber(String.valueOf(generator.nextLong()));
 
Search WWH ::




Custom Search