Spring boot with Retry Mechanism

Spring Retry:

Spring provides Retry mechanism by adding dependency in pom file. Spring Retry provides an ability to automatically re-invoke the failed operation. By using the Spring Retry provided annotations.

Required Dependencies:

<dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
            <version>1.2.5.RELEASE</version>
</dependency>

Spring AOP also required to work with Retry implementation.

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

Spring Retry Annotations:

  1. Enable Retry:

To enable retry we need to add below annotation on Spring boot main class.

@EnableRetry

It will be imported from org.springframework.retry.annotation.EnableRetry package.

  1. Retryable: This annotation will be used at method level, indicates that method execution will be retryable if specified exception is occured based on maximum attempts and delay between the each attempt.

@Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 3000))

@Backoff: we should add a backoff policy to execute retry policy. otherwise till maximum attempts it will be retry for every milli seconds with no gap.

  1. Recover: This annotation will be used at fallback method level. After maximum attempts if still exception occurs, then it will be recovered by the fallback method.

@Recover

If Recover method not using then exception will be returned for the retryable method.

Retry with multiple exceptions:

we can retry with multiple exceptions using curly braces. like below.

@Retryable(
            value = {TypeOneException.class, TypeTwoException.class},
            maxAttempts = 4, backoff = @Backoff(2000))

Follow the github link

github.com/vijayalaxmiadepu/spring-retry/tr..