Retry not working with Spring Batch with Java Config

adesai :

I have Spring batch job with following config:

@Bean
public Job myJob(Step step1, Step step2, Step step3) {
    return jobs.get("myJob").start(step1).next(step2).next(step3).build();
}

@Bean
public Step step1(ItemReader<String> myReader,
                ItemProcessor<String, String> myProcessor,
                ItemWriter<String> myWriter) {
    return steps.get("step1").<String, String>chunk(1)
            .reader(myReader)
            .faultTolerant().retryLimit(3).retry(MyException.class)
            .processor(myProcessor)
            .writer(myWriter)
            .build();
}

@Bean
@StepScope
public MyReader myReader() {
    return new MyReader();
}
@Bean
public MyProcessor myProcessor() {
    return new MyProcessor();
}
@Bean
public MyWriter myWriter() {
    return new MyWriter();
}

When MyReader class throws MyException it is stopping the execution of the job without retrying with following stack trace:

2019-05-16 14:45:09.460 ERROR 22485 --- [           main] o.s.batch.core.step.AbstractStep         : Encountered an error executing step step1 in job myJob

org.springframework.batch.core.step.skip.NonSkippableReadException: Non-skippable exception during read
    at org.springframework.batch.core.step.item.FaultTolerantChunkProvider.read(FaultTolerantChunkProvider.java:105) ~[spring-batch-core-4.0.1.RELEASE.jar:4.0.1.RELEASE]
    at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:116) ~[spring-batch-core-4.0.1.RELEASE.jar:4.0.1.RELEASE]
Mahmoud Ben Hassine :

When MyReader class throws MyException it is stopping the execution of the job without retrying

The retry policy is not applied to the item reader. So even if you declare an exception as retryable and that exception is thrown from the reader, the retry policy is not invoked.

The retry policy is only applied to the processor and writer.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=329610&siteId=1