Spring Batch-每次创建一个新文件,而不是覆盖该文件以将数据从CSV传输到XML [英] Spring Batch - create a new file each time instead of overriding it for transferring data from CSV to XML

查看:75
本文介绍了Spring Batch-每次创建一个新文件,而不是覆盖该文件以将数据从CSV传输到XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring Batch的新手.我试图将数据从CSV file to XML file&能够成功转移它.但是,当我每次运行代码时,我的XML(输出文件)都会被覆盖,而我却不想为每次运行创建新的输出文件(旧的输出文件应存在,需要进行数据跟踪).我该怎么办?

I am new to Spring Batch. I was trying to shift data from CSV file to XML file & able to shift it successfully. But when each time I run the code my XML (output file) getting override which I dont want, instead I want to create new output file (old output files should be there, require for data tracking purpose) for each run. How can I do that ?

这是我的代码:我需要在下面的文件中更改什么?让我知道您是否需要我身边的更多文件代码.

<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>    

推荐答案

尝试使用

Try using the Spring Expression Language (SpEL) to add a date and time to the end of the output file name. Something like:

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

这篇关于Spring Batch-每次创建一个新文件,而不是覆盖该文件以将数据从CSV传输到XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆