Spring 批处理 - 每次都创建一个新文件而不是覆盖它以将数据从 CSV 传输到 XML

Spring Batch - create a new file each time instead of overriding it for transferring data from CSV to XML

我是 Spring Batch 的新手。我试图从 CSV file to XML file 转移数据并且能够成功转移它。但是当每次我 运行 我的代码 XML (输出文件)得到我不想要的覆盖时,我想创建新的输出文件(旧的输出文件应该在那里,需要用于数据跟踪目的) 每个 运行。我该怎么做?

这是我的代码:我需要在下面的文件中更改什么?如果您需要我这边的更多文件代码,请告诉我。

<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.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- JobRepository and JobLauncher are configuration/setup classes -->
    <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" />

    <bean id="jobLauncher"  class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
    </bean>


    <!-- ============= ItemReader reads a complete line one by one from input file ============ -->
    <bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">

        <!-- Get the Resource file -->
        <property name="resource" value="classpath:ExamResult.txt" />

        <property name="lineMapper">
            <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">

                <property name="fieldSetMapper">
                    <!-- Mapper which maps each individual items in a record to properties in POJO -->
                    <bean class="com.websystique.springbatch.mapper.ExamResultFieldSetMapper" />
                </property>

                <property name="lineTokenizer">
                    <!-- A tokenizer class to be used when items in input record are separated by specific characters -->
                    <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                        <property name="delimiter" value="|" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean>


    <!-- ======== XML ItemWriter which writes the data in XML format =========== -->
    <bean id="xmlItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">

        <property name="resource" value="file:xml/ExamResult.xml" />

        <property name="rootTagName" value="UniversityExamResultList" />

        <property name="marshaller">
            <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
                <property name="classesToBeBound">
                    <list>
                        <value>com.websystique.springbatch.model.ExamResult</value>
                    </list>
                </property>
            </bean>
        </property>
    </bean>

    <!-- Optional ItemProcessor to perform business logic/filtering on the input records -->
    <bean id="itemProcessor" class="com.websystique.springbatch.processor.ExamResultItemProcessor" />

    <!-- Optional JobExecutionListener to perform business logic before and after the job -->
    <bean id="jobListener" class="com.websystique.springbatch.listener.ExamResultJobListener" />

    <!-- Step will need a transaction manager -->
    <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />


    <!-- ==================== Actual Job =================== -->
    <batch:job id="examResultJob">
        <batch:step id="step1">
            <batch:tasklet transaction-manager="transactionManager">
                <batch:chunk reader="flatFileItemReader" writer="xmlItemWriter" processor="itemProcessor" commit-interval="10" />
            </batch:tasklet>
        </batch:step>
        <batch:listeners>
            <batch:listener ref="jobListener" />
        </batch:listeners>
    </batch:job>
</beans>    

尝试使用 Spring Expression Language (SpEL) 在输出文件名的末尾添加日期和时间。类似于:

<property name="resource"
          value="file:xml/ExamResult-#{new java.text.SimpleDateFormat(&quot;Mddyyyyhhmmss&quot;).format(new java.util.GregorianCalendar().getTime())}.xml" />