Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
620 views
in Technique[技术] by (71.8m points)

java - Spring Retry doesn't works when we use RetryTemplate?

I've Develop a retry mechanism by taking a reference from the following course. Below is the code I developed in Spring Batch, in this code @Recover method is not getting called. What am I doing wrong here ?

@EnableRetry
@Configuration
public class RetryConfig {
    @Value("${retry.interval.in.seconds}")
    private long retryIntervalInSeconds;

    @Value("${max.attempts}")
    private int attempts;
    
    @Bean
    public RetryTemplate mdsRetryTemplate() {
        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        retryPolicy.setMaxAttempts(attempts);

        FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
        backOffPolicy.setBackOffPeriod(1000 * retryIntervalInSeconds);
        
        RetryTemplate template = new RetryTemplate();
        template.setRetryPolicy(retryPolicy);
        template.setBackOffPolicy(backOffPolicy);
        return template;
    }
}

Below is the Controller

@RestController
@Slf4j
public class BatchJobController {

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    @Qualifier(value = "sampleAcctJob")
    private Job sampleAcctJob;


    @Autowired
    private RetryTemplate retryTemplate;

    @GetMapping(value = "/invoke-job")
    public String handle() throws Throwable {

        long diff = 0;
        JobExecution je= this.invokeJob();
        Date start = je.getCreateTime();
        Date end = je.getEndTime();

        diff = end.getTime() - start.getTime();

        return "All data has been loaded successfully.";
    }

    private JobExecution invokeJob() throws Throwable {
        // PDF Job
        JobParameters pdfParams = new JobParametersBuilder()
                .addString(".id", String.valueOf(System.currentTimeMillis()))
                .addDate("date", new Date()).toJobParameters();

        return retryTemplate.execute(retryContext  -> {
            JobExecution jobExecution = jobLauncher.run(sampleAcctJob, pdfParams);
            if(!jobExecution.getAllFailureExceptions().isEmpty()) {
                log.error("============== sampleAcctJob Job failed, retrying.... ================");
                throw jobExecution.getAllFailureExceptions().iterator().next();
            }
            logDetails(jobExecution);
            return jobExecution;
        });
    }

    private void logDetails(JobExecution jobExecution) {
        log.info("JOB_NAME = {}, JOB_STATUS = {}, START_TIME={}, END_TIME = {} ", 
                jobExecution.getJobInstance().getJobName(), 
                jobExecution.getStatus(),
                jobExecution.getStartTime(), 
                jobExecution.getEndTime());
    }
    
    @Recover
    private void recover() {
        System.out.println("===============");
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are using the retryTemplate in a programmatic way, so you need to provide the RecoveryCallback as a second parameter:

  retryTemplate.execute(new MyRetryCallback(), new MyRecoveryCallback());

If you want to use the declarative approach using annotations, you need to annotate the retryable method with @Retryable and the recovery method with @Recover.

You can find an example of each approach in the home page: https://github.com/spring-projects/spring-retry#quick-start


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...