Configuring maxAttempts for Retry (using annotations)

In this post under Spring Retry, I will explain with example on how to configure maxAttempts for retry operation using annotation.

For our example, we will use the below Service class

Service Class

1  import org.springframework.retry.annotation.Retryable;
2  import org.springframework.stereotype.Service;
4  @Service
5  public class Service12 {
6      private int i = 0;
8      @Retryable(maxAttempts = 5)
9      public void executeWithException() {
10         i = i + 1;
11         System.out.println("Executing method 'executeWithException' : " + i);
12         throw new NullPointerException();
13     }
14 }

In the above code, at line 8, I marked the method that has to be retried by “Retryable” annotation. In that annotation itself there is an attribute called “maxAttempts”.

In the above code, by setting this attribute to 5, we are telling Spring Retry to retry this method max 5 times and not more than that.

In this way we can configure the maxAttempts for retry operation using annotations.

Leave a Reply