Java Reference
In-Depth Information
Because of this, the third step is dedicated to reading the file generated in the previous step and
updating the database with its contents. This section looks at how to write the reader and writer
appropriate for the import of the stock price file.
Reading the Stock Price File
Unlike the reader from step 2, which wasn't exactly an ItemReader you need to write every day, this
ItemReader is much more off the shelf. For this, you use the FlatFileItemReader and the
DelimitedLineTokenizer to read in each stock ticker and parse it into a Ticker item. The domain object
you use for this step, Ticker , consists of nothing more than two fields, ticker and price. Listing 10-25
shows the Ticker class.
Listing 10-25. Ticker
package com.apress.springbatch.statement.domain;
import java.math.BigDecimal;
public class Ticker {
private long id;
private String ticker;
private BigDecimal price;
// Accessors go here
public String toString() {
return ticker + " closed at " + price;
For the ItemReader, you define a FlatFileItemReader with a resource (the output file from step 2)
and a LineMapper. Because the output you receive from step 2 is comma delimited as shown in Listing
10-24, you use the DefaultLineMapper with a DelimitedLineTokenizer to chop up the line and Spring
Batch's BeanWrapperFieldSetMapper to map the FieldSet to your domain object by naming convention.
The configuration for your ItemReader and the importStockPrices step is in Listing 10-26.
Listing 10-26. Configuration of stockFileReader and the importStockPrices Step
<beans:bean id="stockFileReader"
<beans:property name="resource" ref="stockFile" />
<beans:property name="lineMapper">
<beans:bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<beans:property name="lineTokenizer">
<beans:property name="names"value="ticker,price"/>
Search WWH ::

Custom Search