8

Is it possible to retry based on certain conditions? If I annotate with Retryable, it will retry based on some Exceptions but I want to retry if that exception is caught and the corresponding conditions is met. Example:

@Retryable(value={MyException.class},maxAttempts=2)
public myMethod(Request request){

    try{
        doSomething();
    } Catch(Exception ex){
        throw new MyException();
    }

}

Here in the above request, I have a flag, isRetryRequired if that is true and MyException is caught then I want to retry

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
user123475
  • 1,065
  • 4
  • 17
  • 29
  • You should throw up different exceptions from the Catch block. MyException if you want it to be retried, MyOtherException(MyException) if you want to notify of an exception, but not retried, and not lose the stacktrace. – UserF40 Oct 12 '17 at 21:21

1 Answers1

1

Not directly in the annotation; you would need a custom retry policy via the interceptor property.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • How can I intercept the request since I am not sure about this flag. Its the flag sent by client – user123475 Oct 10 '17 at 15:17
  • 1
    See the [documentation](https://github.com/spring-projects/spring-retry). Create and configure a `RetryOperationsInterceptor ` bean with a custom policy and provide a reference to the interceptor in the annotation. – Gary Russell Oct 10 '17 at 16:11