Spring Retry API
Spring provides spring-retry API for running business logic with retry options and recovery method or operations. In this tutorial we will learn the usage of all these with example. We are using Spring boot application here for our example code.Maven dependencies
To use spring retry we need below dependencies.spring-retry
This API provides many annotations which we can use in a declarative manner to implement the same.<dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> <version>1.2.5.RELEASE</version> </dependency>
spring-boot-starter-aop
Spring AOP is also required for retry implementation.<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
Retry implementation
To implement retry with our business logic we need to follow steps mentioned in this section.Enable retry
To enable retry we need to add below annotation on Spring boot main class.Service interface with Retry annotations
In service interface we have two methods for business logic which we will execute with retry option and then two recovery methods, one for each business logic. Retry annotation defines the maxAttempts for number of times to retry the execution and backoff attribute defines the delay in milliseconds during each attempt. Recovery methods are mapped with the best matching of method signature for business method. If it is not able to match any of recovery method for a given retry business method then it will throws an exception for no recovery methods found.
Method "sayHello" with recovery method. If this method is not able to respond in three attempts then it will call the recovery method with default message.@Retryable(value=Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) public String sayHello(String name)throws Exception; @Recover public String fallbackMessage(String name);
Method "sum" with recovery method. If this method is not able to provide sum in three attempts then it will call the recovery method which will return -1.@Retryable(value=Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) public int sum(int a, int b)throws Exception; @Recover public int fallbackSum();
Service class implementation
This class will provide the implementation for the above interface. Below is the complete code for this class.import org.springframework.retry.annotation.Recover; import org.springframework.stereotype.Service; @Service public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) throws Exception { System.out.println("Executing sayHello"); if("ERROR".equals(name)) throw new Exception("Throwing exception for retry..."); return "Hello "+name+"!"; } @Override public String fallbackMessage(String name) { return name+": Couldn't say hello. Try next time."; } @Override public int sum(int a, int b) throws Exception { System.out.println("Executing sum"); if(a+b==0) throw new Exception("Throwing exception for retry..."); return a+b; } @Override public int fallbackSum(){ return -1; } }
Spring boot main class implementation
Main class is a Spring boot main class which contains main method. Here we will call service methods for both scenario when it is able to respond without error and when it has to retry three times with recovery option. We are using Spring application events to execute this code once Spring boot application is up and running. Below is the complete code for this class.@EnableRetry @SpringBootApplication public class RetryDemoApplication { public static void main(String[] args) { SpringApplication.run(RetryDemoApplication.class, args); } @Autowired HelloService helloService; @EventListener(ApplicationReadyEvent.class) public void test()throws Exception{ System.out.println(helloService.sayHello("ERROR")); System.out.println(helloService.sayHello("Black pearl")); System.out.println("-----------------------------------------------------"); System.out.println("Sum: "+helloService.sum(0, 0)); System.out.println("Sum: "+helloService.sum(10, 20)); } }
@EnableRetry
Executing application
If we run this application then you will see that it is executing the "test" method from main class once application is started. Below is the output after the code execution and here, you can notice that it tries the code to execute three time and then executed recovery method due to all failed attempts. Then it has executed one success scenario where it is able to execute logic in first attempt without any error.2020-02-16 11:35:23.989 INFO 8519 --- [ main] com.ttj.retrydemo.RetryDemoApplication : Started RetryDemoApplication in 16.511 seconds (JVM running for 16.886) Executing sayHello Executing sayHello Executing sayHello ERROR: Couldn't say hello. Try next time. Executing sayHello Hello Black pearl! ----------------------------------------------------- Executing sum Executing sum Executing sum Sum: -1 Executing sum Sum: 30
Source code
You can download the complete source code from below Github location.https://github.com/thetechnojournals/spring-tutorials/tree/master/retry-demo
An awesome blog for the freshers. Thanks for posting this information.
ReplyDeleteMicroservices Online Training
Microservices Training in Hyderabad