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

时间:2021-05-08 00:53:48

标签: java spring spring-batch

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

工作

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

读者

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

1 个答案:

答案 0 :(得分:1)

您可以在 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.