Spring Batch - 跳过进程记录

Sha*_*kar 5 spring-batch

我想跳过一些过程记录.

我试过的是,我已经创建了自定义异常,当我想跳过记录并且它调用Skip监听器onSkipInProcess方法时抛出异常.它工作正常.

请找到配置.

 <batch:chunk reader="masterFileItemReader" writer="masterFileWriter" processor="itemProcessor" commit-interval="5000" skip-limit="100000" >
  <batch:skippable-exception-classes>
        <batch:include class="org.springframework.batch.item.file.FlatFileParseException"/>
        <batch:include class="com.exception.SkipException"/>
  </batch:skippable-exception-classes>
  <batch:listeners>
        <batch:listener ref="recordSkipListener"/>
</batch:listeners>
Run Code Online (Sandbox Code Playgroud)

但我想知道有没有其他方法可以跳过进程中的记录?

此致,尚卡尔

Nen*_*zic 8

确实有两种方法可以做到这一点,一种方法就像你提到跳过机制一样,另一种方式返回null它会过滤掉项目而不是写入它.这是文档链接 - 6.3.2.过滤记录可以很好地解释两种方法之间的区别.此博客文章还详细解释了跳过细节和交易.

当您解析csv文件并且您希望每行有5个项目但是一行包含6个无效项目时,您可以选择跳过它(通过将读者异常标记为可跳过并在策略中定义条件,如您给出的示例).但是,如果每一行都包含名称,并且您的用例是不写入以字母开头的项目,这些项目N更好地通过返回null(过滤项目)实现,因为它是有效项目,但不是根据您的业务案例.

另请注意,如果您返回null这些项目的数量将在StepContextgetFilterCount(),如果你使用跳过的方法,他们将在getReadSkipCount(),getProcessorSkipCountgetWriteSkipCount恭敬地.