Java Reference
In-Depth Information
MultiResourceItemWriter
Chapter 7 looked at Spring Batch's ability to read from multiple files with the same format in a single
step. Spring Batch provides a similar feature on the ItemWriter side as well. This section looks at how to
generate multiple resources based on the number of items written to a file.
Spring Batch offers the ability to create a new resource after a given number of records has been
processed. Say you want to extract all the customer records and write them to XML files with only 10
customers per file. To do that, you use MultiResourceItemWriter .
MultiResourceItemWriter dynamically creates output resources based on the number of records it
has processed. It passes each item it processes to a delegate writer so that the actual writing piece is
handled there. All MultiResourceItemWriter is responsible for is maintaining the current count and
creating new resources as items are processed. Figure 9-9 shows the flow of a step using
org.springframework.batch.item.file.MultiResourceItemWriter .
MultiResourceItemWriter
Resource
write
check item count
and chunk boundary
create new resource
Figure 9-9. Processing using a MultiResourceItemWriter
When the write method on MultiResourceItemWriter is called, it verifies that the current resource
has been created and is open (if not, it creates and opens a new file) and passes the items to the delegate
ItemWriter. Once the items have been written, it checks to see if the number of items written to the file
has reached the configured threshold for a new resource. If it has, the current file is closed.
It's important to note that when MultiResourceItemWriter is processing, it doesn't create a new
resource mid-chunk. It waits for the end of the chunk before creating a new resource. For example, if the
writer is configured to roll the file after 15 items have been processed but the chunk size is configured to
20, MultiResourceItemWriter writes the 20 items in the chunk before creating a new resource.
MultiResourceItemWriter has five available dependencies you can configure. Table 9-4 shows each
one and how they're used.
Table 9-4. MultiResourceItemWriter Configuration Options
Option
Type
Default
Description
delegate
ResourceAware
ItemWriterIte
mStream
null (required)
The delegate ItemWriter that
the MultiResourceItemWriter
uses to write each item.
itemCountLimitPerResource
int
Integer.MAX_VA
LUE
The number of items to write
to each resource.
 
 
Search WWH ::




Custom Search