Java Reference
In-Depth Information
<beans:property name="sql" value="INSERT INTO TRANSACTION
(ACCOUNT_SUMMARY_ID, TIMESTAMP, AMOUNT) VALUES ((SELECT ID FROM
ACCOUNT_SUMMARY WHERE ACCOUNT_NUMBER = :accountNumber), :timestamp, :amount)"
/>
<beans:property name="dataSource" ref="dataSource" />
</beans:bean>
<step id="importTransactionFileStep">
<tasklet allow-start-if-complete="true">
<chunk reader="transactionFileReader" writer="transactionWriter"
commit-interval="100">
<streams>
<stream ref="fileItemReader"/>
</streams>
</chunk>
<listeners>
<listener ref="transactionFileReader"/>
</listeners>
</tasklet>
</step>
...
The first step of this job consists of the input file, two ItemReaders (one to do the actual file work
and another to apply some interpretation) and an ItemWriter. The configuration begins with the
definition of the input file, a wrapper for the
FlatFileItemReader
used to read the file itself and the
related
FlatFileItemReader
. (Chapters 7 and 9 cover ItemReaders and ItemWriters, respectively.) All you
need to worry about for this example is that this is the configuration for the input file and the way you
read it. The reason for the wrapper is twofold. First, it's used to determine whether the record is a regular
transaction record or the summary record at the end of the file. Second, it's used as a
StepListener
to
determine whether the correct number of records were processed. If they were, no changes to
ExitStatus
are made. If the number of records doesn't match the summary record of your file,
ExitStatus
is changed to return
STOPPED
. The code for this is covered later in this section. It's important
to note that the tasklet for this step is configured with the
allow-start-if-complete
attribute set to
true
.
By configuring the step in this way, when the job is stopped for any reason, the job can reexecute this
step even if it has been successfully completed. By default, this value is
false
; and if the step was
successfully completed, it would be skipped on a retry.
The configuration of the
transactionJob
continues in Listing 6-24, where you configure the second
step (
applyTransactionStep
) and its components.
Listing 6-24.
Configuration of
applyTransactionStep
and Its Components
...
<beans:bean id="accountSummaryReader"
class="org.springframework.batch.item.database.JdbcCursorItemReader">
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="sql" value="select account_number, current_balance
from account_summary a where a.id in (select distinct t.account_summary_id
from transaction t) order by a.account_number"/>
<beans:property name="rowMapper">
<beans:bean
class="com.apress.springbatch.chapter6.AccountSummaryRowMapper"/>
</beans:property>