Spring Batch MultiResourceItemWriter没有正确地将数据写入文件中

时间:2015-01-28 07:39:45

标签: spring-batch

这是我的SpringBatch maven依赖:

    <dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-core</artifactId>
        <version>2.2.0.RELEASE</version>
    </dependency>

以下是我的job.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:batch="http://www.springframework.org/schema/batch" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/batch
        http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <import resource="../config/launch-context.xml" />

    <bean id="inputFileForMultiResource" class="org.springframework.core.io.FileSystemResource" scope="step">
        <constructor-arg value="src/main/resources/files/customerInputValidation.txt"/>
    </bean>

    <bean id="outputFileForMultiResource" class="org.springframework.core.io.FileSystemResource" scope="step">
        <constructor-arg value="src/main/resources/files/xml/customerOutput.xml"/>
    </bean>

    <bean id="readerForMultiResource" class="org.springframework.batch.item.file.FlatFileItemReader">
        <property name="resource" ref="inputFileForMultiResource" />
        <property name="lineMapper">
            <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <property name="lineTokenizer">
                <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <property name="names" value="firstName,middleInitial,lastName,address,city,state,zip" />
                    <property name="delimiter" value="," />
                </bean>
            </property>
            <property name="fieldSetMapper">
                <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                    <property name="prototypeBeanName" value="customer5TO" />
                </bean>
            </property>
            </bean>
        </property>
    </bean>
    <bean id="customer5TO" class="net.gsd.group.spring.batch.tutorial.to.Customer5TO"></bean>

    <bean id="xmlOutputWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
        <property name="resource" ref="outputFileForMultiResource" />
        <property name="marshaller" ref="customerMarshallerJMSJob" />
        <property name="rootTagName" value="customers" />
    </bean>
    <bean id="customerMarshallerJMSJob" class="org.springframework.oxm.xstream.XStreamMarshaller">
        <property name="aliases">
            <map>
                <entry key="customer" value="net.gsd.group.spring.batch.tutorial.to.Customer5TO"></entry>
            </map>
        </property>
    </bean>
    <bean id="multiResourceItemWriter" class="org.springframework.batch.item.file.MultiResourceItemWriter">
        <property name="resource" ref="customerOutputXmlFile"/>
        <property name="delegate" ref="xmlOutputWriter"/>
        <property name="itemCountLimitPerResource" value="3"/>
        <property name="resourceSuffixCreator" ref="suffix"/>
    </bean>

    <bean id="suffix" class="net.gsd.group.spring.batch.tutorial.util.CustomerOutputFileSuffixCreator"></bean>

    <batch:step id="multiResourceWriterParentStep">
        <batch:tasklet>
            <batch:chunk 
                reader="readerForMultiResource"
                writer="multiResourceItemWriter"
                commit-interval="3">
            </batch:chunk>
        </batch:tasklet>
    </batch:step>

    <batch:job id="multiResourceWriterJob">
        <batch:step id="multiResourceStep" parent="multiResourceWriterParentStep"/>
    </batch:job>
</beans>

基本上我有一个输入文件和更多输出文件。我从输入文件(包含10行)中读取数据,我希望将它以3块的形式写入多个输出文件(在我的情况下,将有4个文件3/3/3/1)。对于每个块,将有一个文件。 作业正常,但输出文件的内容错误。 每个文件都包含在当前块中读取bean的最后一个元素。

让我们说第一个块读取A,B和C.当这个块写入文件时,只写入C并写入3次。

从我的测试中,只有当chunk commit-interval为1时才能正常工作。

这是正确的行为吗? 我做错了什么?

提前谢谢

1 个答案:

答案 0 :(得分:0)

您的customer5TO bean应该具有范围prototype。目前它是一个单例,因此bean属性将始终被最后一项覆盖。