9

I am using annotation based approach - @Retryable to do retry in spring boot application.

@Retryable(value = {DataAccessException.class, JpaSystemException.class}, maxAttempts = Integer.MAX_VALUE, backoff = @Backoff(delay = 30000))

What I want to do is, everytime it retries I want to access its current retry attempt.

I tried doing this using Java Reflection -

Retryable retryable = mth.getAnnotation(Retryable.class);

But using this I am not getting the current retry attempt. It just gives the details which are been added to the @Retryable attribute like value, maxAttempts, Backoff details

Any idea on this? Help is highly appreciated.

Saurabh Deshpande
  • 1,153
  • 4
  • 18
  • 35

1 Answers1

14

You can't access the template, unless you wire up your own interceptor and provide it to the @Retryable in the interceptor property.

You can, however access the count within the @Retryable method using

int retry = RetrySynchronizationManager.getContext().getRetryCount();
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • very useful. Thanks – UnguruBulan Aug 27 '21 at 13:37
  • Any prerequisite for using RetrySynchronizationManager.getContext()? In which scenarios it could be null? – YS_NE Nov 25 '21 at 20:57
  • The only thing I can think of is that the bean with the method is not actually advised by a retry interceptor - that would happen, for example, if there is no `@EnableRetry` on a `@Configutation` bean. Another possibility is an internal call to the method from the same class, which bypasses the proxy. If neither of those are the case, I suggest you ask a new question with code/config. – Gary Russell Nov 26 '21 at 14:47