Java Reference
In-Depth Information
When you look at the output of this job, you find in the /output directory one file for every 10
customers currently loaded in the database. However, Spring Batch did something interesting. First,
note that you didn't pass in a file extension on the outputFile parameter you passed into the job. This
was for a reason. If you look at the directory listing shown in Listing 9-65, you see that
MultiResourceItemWriter added a .X to each file, where X is the number of the file that was created.
Listing 9-65. File Names Created by the Job
michael-minellas-macbook-pro:temp mminella$ ls /output/
custOutputs.1custOutputs.2custOutputs.4custOutputs.6custOutputs.8
custOutputs.10 custOutputs.3custOutputs.5custOutputs.7custOutputs.9
Although it makes sense that you need to distinguish each file name from another, this may or may
not be a workable solution for how to name the files (they don't exactly open nicely with your favorite
editor by default). Because of that, Spring Batch lets you to configure the suffix for each file created. You
do that by implementing the org.springframework.batch.item.file.ResourceSuffixCreator interface
and adding that as a dependency to the multiResourceItemWriter bean. When the
MultiResourceItemWriter is creating a new file, it uses ResourceSuffixCreator to generate a suffix that it
tacks onto the end of the new file's name. Listing 9-66 shows the suffix creator for the example.
Listing 9-66. CustomerOutputFileSuffixCreator
package com.apress.springbatch.chapter9;
import org.springframework.batch.item.file.ResourceSuffixCreator;
public class CustomerOutputFileSuffixCreator implements ResourceSuffixCreator {
@Override
public String getSuffix(int arg0) {
return arg0 + ".xml";
}
}
In Listing 9-66, you implement the ResourceSuffixCreator 's only method, getSuffix , and return a
suffix of the number provided and an .xml extension. The number provided is the number file that is
being created. If you were to re-create the same extension as the default, you would return a dot plus the
number provided.
To use CustomerOutputFileSuffixCreator , you configure it as a bean and add it as a dependency to
the multiResourceItemWriter bean using the property resourceSuffixCreator . Listing 9-67 shows the
added configuration.
Listing 9-67. Configuring CustomerOutputFileSuffixCreator
<beans:bean id="customerSuffixCreator"
class="com.apress.springbatch.chapter9.CustomerOutputFileSuffixCreator"/>
<beans:bean id="multiResourceItemWriter"
class="org.springframework.batch.item.file.MultiResourceItemWriter">
<beans:property name="resource" ref="outputFile"/>
 
 
Search WWH ::




Custom Search