1

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
seenukarthi
  • 8,241
  • 10
  • 47
  • 68
Kaushik Das
  • 99
  • 1
  • 1
  • 8
  • 1
    Although the annotation is different, but may be [same issue](https://stackoverflow.com/questions/38212471/springboot-retryable-not-retrying) as the call in within the same class. – samabcde Apr 05 '22 at 10:01
  • unbelievable! It should've been supported unless I am doing a huge mistake. – Kaushik Das Apr 05 '22 at 11:15
  • I noticed this too. Moving it to another class worked. It's kind of bonkers that there are no error messages or anything. Such a hard thing to troubleshoot. – wmute Jun 24 '22 at 16:50

1 Answers1

3

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.

Krithick S
  • 408
  • 3
  • 15