我是Spring batch的新手。
我必须从一个文件夹中读取多个文件(分隔符),并将它们加载到DB中。我确实做到了。
但我的问题是,在每个文件被处理后,我必须将文件移动到processed文件夹或将错误记录移动到Error文件夹。
例如,如果我处理来自多个文件的以下文件(abc.txt):
D|hello1|123
D|hello2|three - Error
D|hello3|123我知道第二条记录是错误的。
现在,我必须将错误文件(abc-error.txt)中的错误记录写入error文件夹,然后继续下一条记录。成功处理文件(不包括错误记录)后,我需要将abc.txt移到已处理文件夹中。
我如何才能达到上述要求?
我的工作是:
<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的单独文件中。
发布于 2017-01-18 23:56:34
听众。Spring Batch有一个侦听器集合,可以将这种类型的逻辑注入到步骤或作业的任何点上。根据发现错误的位置,将指示哪个侦听器是合适的。例如,如果在读取过程中发现错误,则实现ItemReadListener#onReadError方法将是有意义的。通常,这种类型的逻辑通常是通过实现正确的侦听器来处理的,以便在流程中的正确位置执行所需的逻辑。
您可以在参考文档中阅读有关Spring Batch提供的一些侦听器的更多信息:https://docs.spring.io/spring-batch/reference/html/configureStep.html#interceptingStepExecution
https://stackoverflow.com/questions/41712143
复制相似问题