Using spring retry

 

    Photo by cottonbro from Pexels: https://www.pexels.com/photo/person-holding-an-iphone-5053987/

This blog post will be maily focus on explaining and providing a practical example of how to use spring retry. Spring retry provides retry support for Spring applications. This allows automatic retry for failed operations. You should choose wisely which type of operations you needs to retry, for an example you should not retry an payment operation, you might end up with charging the user twice. It's safer to retry idempotent requests.


Dependencies


- Spring-retry
- Spring-aspects


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

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.20</version>
</dependency>


Code


Enable Retry


To use spring retry you need to use the annotation @EnableRtry, this can be used in your configuration class (annotated with @configuration) here i'm using the main class. 


@EnableRetry
@SpringBootApplication
public class RetryApplication {


Now we are going to define our service interface 

Retryable


- The getInventoryStatus method is the method supports retry which is enabled by adding the     @Retryable annotation.
- value: The retry is triggered once the declared Exception is thrown.
- backoff: The time intervel between retries.
- maxAttemps: No of times a retry should occur. 3 by default.
- You also can pass these configurable values from your application.properties/yml files.

 Recover


- This method is called if all the retry attempts get failed, you can have your logic to handle the senario based on your usecase, for example you could send a notification for a channel indicating the failure.
- Recover method should have the same return type as of the retryable method.
- You can have the exact same parameters as the retryable method, so you could you this values to     handle the failure.

Now let's have a look in the service implementation.
- Here we have written some custom logics to fail this method intentionally and also trigger a success after a retry.
- Simply the method is throwing an Exception which will be triggering a retry.
- The recover method here simply logging the error and throwing an exception, so we could handled that and sent back the response accordingly.

That's pretty much it you need to do to add the retry functionality to your application. Let's run and see the behaviour. I have written a controller to test both retry failure and success senarios. Run the application and hit the below endpoints.

To fail all retries invoke : http://localhost:8080/api/v1/inventory/1
We should see the application will be retrying the endpoint twice since we have configured 2 as the maxAttempts and finally called the recovery method.


Now let's try a endpoint where retry is going to success, please restart the application do test this endpoint. 

http://localhost:8080/api/v1/inventory/1?query=success


As you can see the 2nd request has processed successfully. 

Bonus Content - Retry Template


RetryTemplate is a template class that provides execution of operations with retry semantics, for an example you can use the retry template for calling a rest endpoint by wrapping the RestTemplate and handle retry support. I have provided an simple example to use RetryTemplate in my code.

That's pretty much it I wanted to share with you in this post, as always the code can be found in below github repository. 

 
References & Useful Readings.

Comments

Post a Comment