javaspring-bootspring-retry

Print retry count with @Retryable


I am using @Retryable on a method like this :-

@Retryable( value = SQLException.class, 
      maxAttempts = 5, backoff = @Backoff(delay = 100))
void testMethod(String abc) throws SQLException{
//some method body that could throw sql exception
};

And i want to print the count of retry , with a message like :

Retry Number : 1
Retry Number : 2
...
Retry Number : 5

How can i achieve this ?


Solution

  • you can add below code:

    @Retryable( value = SQLException.class, 
          maxAttempts = 5, backoff = @Backoff(delay = 100))
    void testMethod(String abc) throws SQLException{
    log.info("Retry Number : {}",RetrySynchronizationManager.getContext().getRetryCount());
    };
    

    RetrySynchronizationManager.getContext().getRetryCount() will give you the retry count on flow.