2

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 ?

Here is the my code: What I need to change in below file? Let me know if you need more file code from my side.

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

1 Answers1

0

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" />
badjr
  • 2,166
  • 3
  • 20
  • 31