Java Reference
In-Depth Information
As you can see in Listing 9-41,
CustomerServiceImpl
is short, sweet, and to the point. But it serves the
purpose for the example. To put this service to work in
formatJob
, you can configure it to be the target of
a new
ItemWriterAdapter
. Using the same input configuration you've used in the other jobs this chapter,
Listing 9-42 shows the configuration for the ItemWriter using the
CustomerServiceImpl
's
logCustomer
method and job referencing it.
Listing 9-42.
ItemWriterAdapter
Configuration
...
<beans:bean id="customerService"
class="com.apress.springbatch.chapter9.CustomerServiceImpl"/>
<beans:bean id="itemWriterAdapter"
class="org.springframework.batch.item.adapter.ItemWriterAdapter">
<beans:property name="targetObject" ref="customerService"/>
<beans:property name="targetMethod" value="logCustomer"/>
</beans:bean>
<step id="formatFileStep">
<tasklet>
<chunk reader="customerFileReader" writer="itemWriterAdapter"
commit-interval="10"/>
</tasklet>
</step>
<job id="formatJob">
<step id="step1" parent="formatFileStep"/>
</job>
…
Listing 9-42 starts with the configuration of
customerService
. The ItemWriter is next as the
itemWriterAdapter
. The two dependencies it uses are a reference to
customerService
and the name of
the
logCustomer
method. Finally, you reference the
itemWriterAdapter
in the step to be used by the job.
To execute this job, you build it, like all jobs, with a
mvn clean install
from the command line.
With the job built, you can execute it by executing the jar file as you've done in the past. A sample of the
output of this job is shown in Listing 9-43.
Listing 9-43.
ItemWriterAdapter
Output
2011-03-09 22:43:56,526 DEBUG main
[org.springframework.batch.repeat.support.RepeatTemplate] - <Repeat operation
about to start at count=10>
2011-03-09 22:43:56,526 DEBUG main
[org.springframework.batch.repeat.support.RepeatTemplate] - <Repeat is
complete according to policy and result value.>
I just saved Richard N. Darrow
5570 Isabella Ave
St. Louis, IL
58540
I just saved Warren L. Darrow
4686 Mt. Lee Drive