Spring Batch moving file after processing

一曲冷凌霜 提交于 2019-12-25 04:57:05

问题


I am new to Spring batch.

I have to read multiple files(delimiter) from a folder and load them in DB. Which I did.

But my issue is after each file is processed I have to move the file to processed folder or error records to Error folder.

For example if I process a below file (abc.txt) from multiple file:

D|hello1|123 
D|hello2|three - Error 
D|hello3|123

I know that 2nd record is an error.

Now I have to write the error record in a error file(abc-error.txt) to error folder and proceed with the next record. After the file is processed successfully excluding error records, i need to move the abc.txt to a processed folder.

How can I achieve the above requirement?

My job:

<batch:job id="file_to_db">
    <batch:step id="step1">
        <batch:tasklet ref="moveFiletoTmpFolder" />
        <batch:end on="FAILED"/>
        <batch:next on="*" to="step2" />
    </batch:step>
    <batch:step id="step2">
        <batch:tasklet transaction-manager="transactionManager"
            start-limit="100">
            <batch:chunk reader="multiResourceReader" writer="databaseItemWriter"
                commit-interval="100">
            </batch:chunk>
        </batch:tasklet>
    </batch:step>
</batch:job>
<bean id="multiResourceReader"
        class=" org.springframework.batch.item.file.MultiResourceItemReader">
    <property name="resources" value="file:batch/csv/processing/*.csv" />
    <property name="delegate" ref="cvsFileItemReader" />
</bean>

The job is not one step. But each file must be moved once it is processed. Error records must be written into a separate file named filename-error.txt for each file.


回答1:


Listeners. Spring Batch has a collection of listeners for injecting this type of logic into just about any point of the step or job you could need. Depending on where the error is discovered will indicate which listener is appropriate. For example, if the error is discovered during reading, implementing the ItemReadListener#onReadError method would make sense. In general, this type of logic is commonly handled by implementing the correct listeners to execute the required logic at the right point in the process.

You can read more about some of the listeners provided by Spring Batch in the reference documentation here: https://docs.spring.io/spring-batch/reference/html/configureStep.html#interceptingStepExecution



来源:https://stackoverflow.com/questions/41712143/spring-batch-moving-file-after-processing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!