javaspring-bootresilience4j-retry

Resilience4j Retry does not retry


I have the following code where if I keep the retry annotation on the controller method, then it is getting retried but if I keep the retry annotation on a different method, it doesn't retry. Scenario is, in the API method, createOrder(), I fetch the orderId from an external system which is working fine. But I need to retry the createOrder(String orderId) method which fails some of the times.

@GetMapping("/order")
//@Retry(name = ORDERSERVICE, fallbackMethod = "fallback_retry")
public ResponseEntity<String> createOrder() {
        int orderId = 1; // makeDBCall or fetch it from somewhere
        return createOrder(orderId); // need to retry this method in case it fails
    }

@Retry(name = ORDERSERVICE, fallbackMethod = "fallback_retry")
public ResponseEntity<String> createOrder(int orderId) {
    logger.info("item service call attempted:::" + attempts++);
    String response = restTemplate.getForObject("http://localhost:8081/item/" + orderId, String.class);
    logger.info("item service called");
    return new ResponseEntity<String>(response, HttpStatus.OK);
}
resilience4j.retry:
  instances:
    orderService:
      maxRetryAttempts: 3
      waitDuration: 11000

Solution

  • This is because of Proxy. When annotated with @Retry a proxy instance of the class is created and is used. The proxies work in such a way that calls from one bean/class to another bean/class are intercepted, and it cannot intercept calls from method to method within the bean/class.

    Hence, the workaround would be to move the method to a different class. Once the method is moved to a different class, the spring proxy would consider the call from a separate bean and it can intercept.