Java Reference
In-Depth Information
scope="step">
<beans:property name="lineAggregator">
<beans:bean
class="org.springframework.batch.item.file.transform.
DelimitedLineAggregator">
<beans:property name="delimiter" value=","/>
<beans:property name="fieldExtractor">
<beans:bean
class="org.springframework.batch.item.file.transform.
BeanWrapperFieldExtractor">
<beans:property name="names"
value="accountNumber,currentBalance"/>
</beans:bean>
</beans:property>
</beans:bean>
</beans:property>
<beans:property name="resource" ref="summaryFile" />
</beans:bean>
<step id="generateAccountSummaryStep">
<tasklet>
<chunk reader="accountSummaryReader" writer="accountSummaryWriter"
commit-interval="100"/>
</tasklet>
</step>
…
With all the steps configured, you can finally configure the job itself. In the job, you configure the
three steps as discussed. However, for
step1
, if the step returns
STOPPED
, you stop the job. If the job is
restarted, it reexecutes
step1
. If
step1
returns any other successful value, the job continues with
step2
and finally
step3
. Listing 6-26 shows the configuration for this logic using the
<stop>
tag.
Listing 6-26. Configuring
a Job Using the
<stop>
Tag
…
<job id="transactionJob">
<step id="step1" parent="importTransactionFileStep">
<stop on="STOPPED" restart="step1"/>
<next on="*" to="step2"/>
</step>
<step id="step2" parent="applyTransactionsStep" next="step3"/>
<step id="step3" parent="generateAccountSummaryStep"/>
</job>
</beans:beans>
How do you perform the check to be sure you read in the correct number of records? In this case,
you develop a custom reader that also serves as a step listener. The code in Listing 6-27 shows how the
reader reads in all the records and keeps a count of how many are read in. When the step is complete,
the listener validates that the number of records read in matches the number of records expected. If they
match, the
ExitStatus
that was determined by the regular processing is returned. If they don't match,
you override the
ExitStatus
by returning your own
ExitStatus.STOPPED
value.