Java Reference
In-Depth Information
By looking up the customer in the ItemProcessor and updating the item with the database id before
passing it onto the writer, you allow the writer to be dumb and do nothing more than a typical save-or-
update style operation. Without this, your ItemWriter would need to do both a lookup and an insert,
which isn't the behavior you're looking for in an ItemWriter.
The configuration for the job needs to be updated to include the new ItemProcessor. To configure
the ItemProcessor, you add it to the statementJob.xml file as well as the DAOs the ItemProcessor is
dependent on. Listing 10-17 shows the configuration to add to the statementJob.xml file.
Listing 10-17. Configuration for customerLookupItemProcessor
...
<beans:bean id="customerLookupItemProcessor"
class="com.apress.springbatch.statement.processor.CustomerLookupItemProcessor">
<beans:property name="customerDao" ref="customerDao"/>
<beans:property name="tickerDao" ref="tickerDao"/>
<beans:property name="accountDao" ref="accountDao"/>
</beans:bean>
<beans:bean id="customerDao"
class="com.apress.springbatch.statement.dao.impl.CustomerDaoJdbc">
<beans:property name="dataSource" ref="dataSource"/>
</beans:bean>
<beans:bean id="tickerDao"
class="com.apress.springbatch.statement.dao.impl.TickerDaoJdbc">
<beans:property name="dataSource" ref="dataSource"/>
</beans:bean>
<beans:bean id="accountDao"
class="com.apress.springbatch.statement.dao.impl.AccountDaoJdbc">
<beans:property name="dataSource" ref="dataSource"/>
</beans:bean>
...
The added configuration shown in Listing 10-17 begins with the configuration of
CustomerLookupItemProcessor itself. The ItemProcessor is dependent only on the DAOs you coded in this
section, which are also configured as the next three beans; each requires only a reference to a
datasource.
The last piece of the process to import the customer and transaction data is to write the data to the
database. The required ItemWriters are covered in the next section.
Writing the Customer and Transaction Data
The last piece of importing the customer and transaction data is updating the database with your newly
read items. This section looks at how to write both the Customer items and Transaction items processed
in this step.
Spring Batch provides a great tool to handle the writing for this step:
ClassifierCompositeItemWriter. With its ability to determine which writer to use based on a classifier
paired with Spring Batch's SubclassClassifier, which allows you to define class-to-ItemWriter
associations, it's a perfect fit for this type of problem. Figure 10-5 shows the structure of the
importCustomerAndTransaction step as a whole.
 
Search WWH ::




Custom Search