处理后的Spring Batch移动文件 [英] Spring Batch moving file after processing

查看:117
本文介绍了处理后的Spring Batch移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring批处理的新手.

I am new to Spring batch.

我必须从一个文件夹中读取多个文件(定界符)并将它们加载到DB中.我做了.

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

但是我的问题是在处理完每个文件后,我必须将文件移至processed文件夹,或者将错误记录移至Error文件夹.

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

例如,如果我处理多个文件中的以下文件(abc.txt):

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.

现在,我必须将错误记录(abc-error.txt)中的错误记录写入错误文件夹,然后继续下一条记录.成功处理文件(排除错误记录)后,我需要将abc.txt移至已处理的文件夹.

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.

我如何达到上述要求?

我的工作:

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

工作不是一步.但是每个文件在处理后都必须移动. 错误记录必须写入每个文件名为filename-error.txt的单独文件中.

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.

推荐答案

侦听器. Spring Batch有一个侦听器集合,用于将这种逻辑注入几乎您可能需要的步骤或工作的任何点.根据发现错误的位置,将指示适合的侦听器.例如,如果在读取过程中发现错误,则实现ItemReadListener#onReadError方法将是有意义的.通常,通常通过实现正确的侦听器以在过程中的正确点执行所需的逻辑来处理这种类型的逻辑.

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.

您可以在以下参考文档中了解有关Spring Batch提供的某些侦听器的更多信息:

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

这篇关于处理后的Spring Batch移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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