Java Reference
In-Depth Information
class="com.apress.springbatch.statement.processor.FeesItemProcessor"/>
...
When you've calculated the fees for each transaction, you need to save them to the database, as you
look at next.
Saving Transaction Fees to the Database
After a transaction's fee has been calculated, you need to do two things: update the transaction record
and update the account's cash balance, deducting the fee. This section looks at the ItemWriters required
to apply transaction fees to a customer's account.
Coming out of the feesItemProcessor in this step, you have an item that has information that needs
to be applied to two different locations. The first location where it needs to be applied is the Transaction
table. Each transaction stores the fee applied to it. The second place the item's information needs to be
applied is in the account's cash balance, where you need to deduct the amount of the fee.
To do this, Spring Batch provides the ability to process a single item with multiple ItemWriters using
CompositeItemWriter, as you saw earlier in Chapter 9. CompositeItemWriter is the perfect tool for a
scenario like this. Figure 10-6 shows the structure of this step as a whole.
JdbcBatchItemWriter
FlatFileItemReader
ItemProcessor
CompositeItemWriter
JdbcBatchItemWriter
Figure 10-6. calculateTransactionFees step structure
Figure 10-6 shows that you read in each transaction with transactionPricingItemReader (the
FlatFileItemReader) and process it with feesItemProcessor (the ItemProcessor) . However, using
CompositeItemWriter, you apply the transaction to the Transaction table with feesUpdateWriter and the
Account table with cashBalanceUpdateWriter . Listing 10-40 shows the configuration for the three
required ItemWriters for this step.
Listing 10-40. ItemWriter Configuration for Step 5
<beans:bean id="feesUpdateWriter"
class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="sql" value="update transaction set fee = :fee where id = :id"/>
<beans:property name="itemSqlParameterSourceProvider">
<beans:bean
class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider"/>
</beans:property>
</beans:bean>
<beans:bean id="cashBalanceUpdateWriter"
class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<beans:property name="dataSource" ref="dataSource"/>
 
Search WWH ::




Custom Search