简体   繁体   中英

Spring retry exception handling execution behavior

I am trying to get to the best way to wrap spring-retry @Retryable annotation around external service call. Here is my code:

@Retryable(exclude = HttpClientErrorException.BadRequest.class, value = RestClientException.class)
private ResponseEntity<Item> retrieveItemById(String id) 
{
    HttpHeaders headers = new HttpHeaders();
    try {
        return restTemplate.exchange(httpConnectionProperties.getBaseUrl() + "/items",
                HttpMethod.GET, new HttpEntity<>(item, headers), Item.class, id);
    } 
    catch (RestClientException e) {
        log.error("Exception occurred while retrieving an item" , e);
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
}

I have a few questions on what happens when a RestClientException occurs :

  1. Is the catch block executed before retry kicks in or does the retry kicks in before the catch block execution? Do I need a recovery block?
  2. Probably more of an exception handling question - Is there a way to differentiate between an actual retry worthy scenario (service momentarily down, network issues, I/O error etc.) vs exception occurring due to lack of presence of an item in the above case?

Since you are catching and "handling" the exception, retry is disabled; retry will only work if the method throws an exception.

To change the result (instead of throwing the exception to the caller when retries are exhausted, you need a @Recover method.

Not retryable exceptions will go straight there; you can have multiple @Recover methods for different exception types, or one generic one and you can check the exception type yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM