Retry not working with Spring Batch with Java Config

前端 未结 2 1785
礼貌的吻别
礼貌的吻别 2021-01-21 12:56

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(step         


        
相关标签:
2条回答
  • 2021-01-21 13:46

    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.

    0 讨论(0)
  • 2021-01-21 13:48

    Seems the retry functionality was pulled out of Spring Batch as of 2.2.0. It is now part of a new library, Spring Retry. https://docs-stage.spring.io/spring-batch/docs/current/reference/html/retry.html#retry

    Steps to handle this with declarative retry:

    Step 1: Include @EnableRetry in application

    Spet 2: Add aop starter based on https://github.com/spring-projects/spring-retry#additional-dependencies

    Gradle runtime('org.springframework.boot:spring-boot-starter-aop')

    Maven

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
        <scope>runtime</scope>
    </dependency>
    

    Spet 3: Include @Retryable in MyReader

    @Override
    @Retryable(include = { MyException.class }, maxAttempts = 5)
    public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
    

    Sample code is checked into github: https://github.com/atulkulkarni18/spring-batch-reader-retry

    Sample output:

    MyReader : 0
    MyProcessor : 0
    MyWriter : [0]
    MyReader : 1
    MyProcessor : 1
    MyWriter : [1]
    MyReader : 2
    MyProcessor : 2
    MyWriter : [2]
    MyReader : 3
    ****
    MyReader : 3
    ****
    MyReader : 3
    ****
    MyReader : 3
    MyProcessor : 3
    MyWriter : [3]
    MyReader : 4
    MyProcessor : 4
    MyWriter : [4]
    MyReader : 5
    
    0 讨论(0)
提交回复
热议问题