我有 Spring 批处理作业,它读取一些文件并将其保存到数据库。如果我需要能够编写一些自定义条件并在不满足条件时跳过文件处理。我尝试扩展 ItemReader 并抛出异常,但它导致整个作业失败,而我需要作业继续迭代文件。
谢谢
最佳答案
查看 org.springframework.batch.core.step.skip.SkipPolicy 接口(interface)。我给你一个从Pro Spring Batch written by T. Minella中提取的例子
import java.io.FileNotFoundException;
import org.springframework.batch.core.step.skip.SkipLimitExceededException;
import org.springframework.batch.core.step.skip.SkipPolicy;
import org.springframework.batch.item.ParseException;
public class FileVerificationSkipper implements SkipPolicy {
public boolean shouldSkip(Throwable exception, int skipCount) throws SkipLimitExceededException {
if(exception instanceof FileNotFoundException) {
return false;
} else if(exception instanceof ParseException && skipCount <= 10) {
return true;
} else {
return false;
}
}
}
在你的 xml 文件中:
<step id="copyFileStep">
<tasklet>
<chunk reader="customerItemReader" writer="outputWriter"
commit-interval="10" skip-limit="10">
<skippable-exception-classes>
<include class="java.lang.Exception"/>
<exclude class="org.springframework.batch.item.ParseException"/>
</skippable-exception-classes>
</chunk>
</tasklet>
</step>
或者另一种方法可能是在您的工作开始时添加一个步骤,将您的输入文件分类到两个单独的文件夹中。在一个文件夹中,您将拥有所有错误的文件,而在另一个文件夹中,只保留好的文件。
关于java - Spring批处理如何根据条件跳过整个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34435148/