7

According to the spring batch/retry documentation (http://docs.spring.io/spring-batch/reference/html/retry.html) in section 9.2 one can specify which exceptions you would like to retry or not retry on via setRetryableExceptions or setFatalExceptions when using the SimpleRetryPolicy. However, these methods are not defined in the current release (1.0.3) in GitHub https://github.com/spring-projects/spring-retry/blob/master/src/main/java/org/springframework/retry/RetryPolicy.java .

So, is this a documentation error? If not, then where are the methods located?

From the source code, it seems that only the retryable exceptions can be set via the constructor that takes a Map of exceptions. There doesn't appear to be a way to define the fatal exceptions.

user3537621
  • 151
  • 2
  • 4

3 Answers3

8

Maybe this can help. You have to create a map holding all retryable exceptions by classtype, and add it to the policy. Probably similar with fatal exceptions.

Map<Class<? extends Throwable>, Boolean> r = new HashMap<>();
r.put(RetryException.class, true);
SimpleRetryPolicy p = new SimpleRetryPolicy(MAX_RETRIES, r);
RetryTemplate t = new RetryTemplate();
t.setRetryPolicy(p);
membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • Yes, this is similar to what I ended up doing. I just defined the retry-able exceptions in the map. Any exception not in the map is treated as a fatal exception. – user3537621 Feb 17 '15 at 22:21
0

I had to implement Retry mechanism in my project as well and I created my own implementation.

Retry using AOP

This works like a charm (only i didn't find a way yet to ensure that only one aspect instance in instantiated at a time.)

You can just annotate your methods with the @Retry annotation, provide some config you want and its done.

Community
  • 1
  • 1
Vivek Kothari
  • 462
  • 6
  • 20
0

These methods are no longer available. There is an issue posted by Gary to correct the documentation. You can pass a Map mentioning the set of fatal exceptions for which you don't want to retry.

  @Bean
  public RetryTemplate retryTemplate() {
      RetryTemplate template = retryTemplate();
      Map<Class<? extends Throwable>, Boolean> retryableExceptions = new HashMap<>();
      retryableExceptions.put(Exception.class, true);
      retryableExceptions.put(RuntimeException.class, false); 
      template.setRetryPolicy(new SimpleRetryPolicy(2,retryableExceptions));
      return template;
  }

In the above example the template will retry for the family of Exception.class except the RuntimeException.class and its subclasses. Additionally, you can refer to the answer Gary has posted here for better understanding.

Rahul Dey
  • 163
  • 2
  • 7