0

My Spring batch configuration looks like this:

@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(10)
            .reader(myReader)
            .processor(myProcessor)
            .writer(myWriter)
            .faultTolerant().retryLimit(3).retry(MyException.class)
            .build();
}

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

so, when I am running my application the retry that I had set on the StepBuilderFactory is not working. Also, I would like to have a timed interval for the retry. I see that its implemented via backOffPolicy. I did some digging and implemented it in the following way

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

The following is the code backoffpolicy method:

@Bean
    public BackOffPolicy backoffPolicy() {
        ExponentialBackOffPolicy exponentialBackOffPolicy = new ExponentialBackOffPolicy();
        exponentialBackOffPolicy.setInitialInterval(1000);
        exponentialBackOffPolicy.setMultiplier(2.0);
        exponentialBackOffPolicy.setMaxInterval(10000);
        return exponentialBackOffPolicy;
    }

My main intention for Retry is that in my ItemReader I will be calling a stored procedure. So, if there occurs a deadlock I want to retry after n number of minutes. I also want retry on the ItemWriter as well as it will be writing to a file. I want both retry and backoffPolicy (so that I can set an interval on when my ItemReader and ItemWriter should retry again) working on my code. Can someone tell me where I am going wrong in my code. Thanks in advance.

Vamsi
  • 619
  • 3
  • 9
  • 22
  • Hi, can you please post a sample on how to do it? I read the response but I am not sure on how to do it for both ItemReader and ItemWriter as I am not able to get it working. – Vamsi Apr 19 '21 at 08:05
  • According to your description, you want retry for the reader and for the writer. For the reader, you need to create a decorator like MyRetryableItemReader that delegates to your current reader or use spring-retry to create a retryable proxy for you like here: https://stackoverflow.com/a/61454022/5019386. For the writer, the retry policy that you configured on the step should work. – Mahmoud Ben Hassine Apr 19 '21 at 08:10
  • How can I configure a retry time to do retry at a specific time intervals on reader if I configure it via @Retryable annotation – Vamsi Apr 19 '21 at 08:36
  • That's another question which is related to the spring-retry library and not Spring Batch per se. Please open another question and share your code and requirement. – Mahmoud Ben Hassine Apr 19 '21 at 08:47
  • Retry policy doesn't work on item readers. Make sure you're throwing the custom ```MyException``` from either the item processor or the item writer. Please refer to [this post](https://stackoverflow.com/questions/56170179/retry-not-working-with-spring-batch-with-java-config) which almost looks similar to your post. I wanted to comment, but I can't since I'm a newbie here. – Rahul Dey Apr 18 '21 at 09:21
  • I have StoredProcedureItemReader as my reader and I couldn't get the retry working on it after adding @Retryable – Vamsi Apr 19 '21 at 23:17
  • I have posted a new question explaining the issue I am getting in using `StoredProcedureItemReader` with Retry on it. https://stackoverflow.com/questions/67170575/storedprocedureitemreader-not-able-to-retry-on-deadlock-exceptions – Vamsi Apr 19 '21 at 23:44

0 Answers0