Java Reference
In-Depth Information
UrlReader) and you can pass it on. The output you receive from Yahoo! is in CSV format already, so your
ItemWriter becomes significantly simplified. In this case, because your item is a String , using the
FlatFileItemWriter with a PassThroughLineAggregator works perfectly. Listing 10-23 shows the
configuration for this ItemWriter.
Listing 10-23. The stockFileWriter Configuration
...
<beans:bean id="stockFile" class="org.springframework.core.io.FileSystemResource">
<beans:constructor-arg value="/output/stockFile.csv"/>
</beans:bean>
<beans:bean id="stockFileWriter"
class="org.springframework.batch.item.file.FlatFileItemWriter">
<beans:property name="resource" ref="stockFile" />
<beans:property name="lineAggregator">
<beans:bean
class="org.springframework.batch.item.file.transform.PassThroughLineAggregator"/>
</beans:property>
</beans:bean>
...
You can't get much simpler for an ItemWriter. By providing a file to write to and using a String as
the item, there is nothing for Spring Batch to do other than write the String to the file. Short, sweet, and
to the point.
When you build and run the job with only the first and second steps fully functional, the end result
of step 2 is a CSV file that looks like what is shown in Listing 10-24.
Listing 10-24. Output of Step 2
"A",42.94
"AA",16.11
"AAI",7.30
"AAP",64.80
"AAR",24.04
"AAV",8.31
"AB",21.57
"ABA",25.6231
"ABB",23.14
As you can see, the file consists of the stock ticker followed by the closing price of the previous day.
The next section looks at the process of importing this file into your Ticker table.
Importing Current Stock Prices
In the previous step, you read the results of a web service call and wrote it to disk to import. You might
be wondering, why not just read it straight into the database? The main reason is that if something goes
wrong with the import step (after you've successfully downloaded the stock prices), you don't have to
rerequest the prices. Once you've successfully received the stock prices, you can skip step 2 if the import
fails.
 
 
Search WWH ::




Custom Search