Java Reference
In-Depth Information
Reading the Tickers
There are a couple different ways you can approach downloading the current stock prices you need:
You can use an ItemReader that returns an item representing the stock symbol for
which you need to get the price. From there, you can get the price for each of the
stock tickers and write it to a file to be imported later.
You can use an ItemReader to read a single stream from the web service, getting
all the stock prices at once.
Although the first option fits better with the components of Spring Batch, let's consider what you're
attempting to do. The New York Stock Exchange (NYSE) has over 2,000 listed stocks, not to mention
bonds, mutual funds, and other securities. To loop through each of these financial products one by one
and make a web service call to get a single number (the closing price of the stock) isn't a practical way to
process this data. Because of this, this example uses option 2.
This opens up a different can of worms. Although Spring Batch provides a nice array of ItemReader
implementations, it doesn't offer one that reads from a URL. To implement this functionality, you have
to create your own custom URL reader. This reader loads all the stocks that your customers currently
hold, calls a web service to obtain the closing price for each ticker, and returns the response as a single
string to be written out as a file by the writer.
To start, let's create a DAO to obtain a list of all the stock tickers your customers currently have. To
do this, you add a method to the ticker DAO to return a list of tickers for which you need to get prices.
Listing 10-19 shows the updated TickerDaoJdbc with the new method.
Listing 10-19. TickerDao Jdbc
package com.apress.springbatch.statement.dao.impl;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import com.apress.springbatch.statement.dao.TickerDao;
import com.apress.springbatch.statement.domain.Ticker;
import com.apress.springbatch.statement.domain.Transaction;
public class TickerDaoJdbc extends JdbcTemplate implements TickerDao {
private static final String FIND_BY_SYMBOL = "select * from ticker t where ticker = ?";
private static final String SAVE_TICKER =
"insert into ticker (ticker, currentPrice) values (?,?)";
private static final String FIND_ALL =
select distinct ticker from ticker order by ticker limit ?, ?";
public Ticker findTickerBySymbol(String symbol) {
List<Ticker> tickers = query(FIND_BY_SYMBOL, new Object [] {symbol}, new RowMapper() {
Search WWH ::

Custom Search