Nick
Nick

Reputation: 265

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

Suppose that two data were read using HibernateCursorItemReader in spring batch. Suppose that an Exception occurred while processing the first row data in process. The job listener is handling the failure. But here the job ends normally. I want to continue processing in the processor for the second row, what should I do?

Job

@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();
  }

processor

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

Upvotes: 0

Views: 818

Answers (1)

Rahul Dey
Rahul Dey

Reputation: 173

You can use FaultTolerant skip logic in your sampleStep() bean. Add these below configuration in your sampleStep() bean:

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

Upvotes: 1

Related Questions