Java Reference
In-Depth Information
</step>
<job id="formatJob">
<step id="step1" parent="formatFileStep"/>
</job>
</beans:beans>
Writing to multiple files based on the number of records per file is made easy using
MultiResourceItemWriter . Spring's ability to add a header and/or footer record is also managed in a
simple and practical way using the appropriate interfaces and configuration. The next section looks at
how to write the same item to multiple writers with the addition of no code.
CompositeItemWriter
Although it may not seem like it, the examples you've reviewed in this chapter up to this point have been
simple. A step writes to a single output location. That location may be a database, a file, an e-mail, and
so on, but they each have written to one endpoint. However, it's not always that simple. An enterprise
may need to write to a database that a web application uses as well as a data warehouse. While items are
being processed, various business metrics may need to be recorded. Spring Batch allows you to write to
multiple places as you process each item of a step. This section looks at how the CompositeItemWriter
lets a step write items to multiple ItemWriters.
Like most things in Spring Batch, the ability to call multiple ItemWriters for each item you process is
quite easy. Before you get into the code, however, let's look at the flow of writing to multiple ItemWriters
with the same item. Figure 9-11 shows a sequence diagram of the process.
ItemReader
ItemProcessor
CompositeItemWriter
ItemWriter
ItemWriter
ItemWriter
read
process
read
process
read
process
write
write
write
write
Figure 9-11. Sequence diagram of writing to multiple ItemWriters
As Figure 9-11 shows, reading in occurs one at a time, as does processing. However, the diagram
also shows that writing occurs in chunks as you would expect, calling each ItemWriter with the items in
the current chunk in the order they're configured.
To look at how this feature works, you create a job that reads in the customerWithEmail.csv file from
earlier in the chapter. let's start with the input. Listing 9-74 shows the configuration to read in the
customerWithEmail.csv file.
 
Search WWH ::




Custom Search