Sunday, 10 December 2017

Implement Spring Retry in Spring Boot

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.8</version>

</dependency>


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



@EnableRetry
@SpringBootApplication
@ComponentScan(basePackages="com.example")
@EnableAutoConfiguration
public class SampleApplication extends SpringBootServletInitializer{

///.....
}


@Service
public class MyService {

@Retryable(value = {
IndexOutOfBoundsException.class, Exception.class}, maxAttempts = 10, backoff = @Backoff(delay = 5000))
  public void retryWithException() {
System.out.println("running......");
    List<Integer> list  = new ArrayList<>();
    list.get(3);
   

  }


@RequestMapping(value="/testRetry",method=RequestMethod.GET)
public @ResponseBody String retry(){
System.out.println("requestReceived::"+"/retry");
myservice.retryWithException();
return "done";
}



output:

requestReceived::/retry
running......
running......
running......
running......
running......
running......
running......
running......
running......
running......
2017-12-10 19:24:26.924 ERROR 30540 --- [io-8080-exec-17] o.s.boot.web.support.ErrorPageFilter     : Forwarding to error page from request [/testRetry] due to exception [Index: 3, Size: 0]

java.lang.IndexOutOfBoundsException: Index: 3, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653) ~[na:1.8.0_144]
at java.util.ArrayList.get(ArrayList.java:429) ~[na:1.8.0_144]





//recover after retry

//add below in MyService class

 @Recover
  public void recover(IndexOutOfBoundsException exception) {
          System.out.println("ah... catch");

  }



output:


requestReceived::/retry
running......
running......
running......
running......
running......
running......
running......
running......
running......
running......
ah... catch


links for Data Structure

  1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭:  https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...