Spring batch Spring Batch MultiResourceItemWriter无法在文件中正确写入数据

Spring batch Spring Batch MultiResourceItemWriter无法在文件中正确写入数据,spring-batch,Spring Batch,这是我对SpringBatch maven的依赖: <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-core</artifactId> <version>2.2.0.RELEASE</version> </dependency&

这是我对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次

根据我的测试,只有当区块提交间隔为1时,它才能正常工作

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


提前感谢您

您的Customer5ToBean应该有scope原型。目前它是一个单例,因此bean属性将始终被最后一项覆盖。

是!我不知道我怎么会错过。非常感谢你!