spring 批处理失败时如何继续处理处理器中的下一行?

How to continue processing the next row in the processor when it fails in spring batch?

假设在spring批次中使用HibernateCursorItemReader读取了两个数据。 假设在处理进程中的第一行数据时发生异常。 作业侦听器正在处理失败。 但这里的工作正常结束。 我想在处理器中继续处理第二行,我该怎么办?

工作

@Bean
  public Job sampleJob() {
    return jobBuilderFactory.get("sampleJob")
        .start(sampleStep())
        .listener(jobListener)
        .build();
  }

Reader

@Bean
  @StepScope
  public HibernateCursorItemReader<Sample> sampleItemReader() {
    return new HibernateCursorItemReaderBuilder<Sample>()
        .sessionFactory(entityManagerFactory.unwrap(SessionFactory.class))
        .queryString("select ...")
        .fetchSize(100)
        .saveState(false)
        .build();
  }

处理器

@Override
  public Sample process(Sample sample) throws Exception {
    try {
         ...
    }catch (Exception e) {
       throw new MyException();
         //When an exception occurs, the job listener handles it. **When is the next row read from the reader processed?** It just ended...
    }

    return sample  
}

您可以在 sampleStep() bean 中使用 FaultTolerant skip logic。在您的 sampleStep() bean 中添加以下配置:

.faultTolerant()
.skipLimit(10)
.skip(Exception.class)
// If you want to skip for any specific exception, you can put it above.