-1
@Retryable(value = ABCDException.class,
        maxAttemptsExpression = 3,
        backoff = @Backoff(delayExpression = "#{${application.delay}}"))

public String postABCDrequest(ABCDrequest abcdRequest) throws ABCDException {
    try {
        return restCalltopostData(abcdRequest);
    } catch (AnyException e) {
        log.error("Error Occured ", e);
        throw new ABCDException("Error Occured ", e);
    }
}

In this method, I need to retry posting data only when I get certain response codes. I have searched for a few options which isnt suitable for my solution. Is there any simpler way by using annotation?

  • Can you please provide the response codes where you prefer to retry? – Rahul Dey May 28 '21 at 15:36
  • for example if the response code is > 500 , I would want to retry.. Here, catch block has AnyException. I wouldn't want all cases to be retried. Only specific cases to be retried. I can seperate them with response codes and hoping to retry with that – Vaibhav Meenakshi May 30 '21 at 10:42
  • See https://stackoverflow.com/questions/47680711/which-http-errors-should-never-trigger-an-automatic-retry – Michael Freidgeim Mar 03 '23 at 12:28

1 Answers1

0

In catch block, you won't be able to get hold of the response code. Since you're interested in all 5xx, put a check for response.getStatusCode().is5xxServerError() and re-throw the exception ABCDException.class if the exceptions are well handled at the server end and returns the status code. This way you code will continue to retry for all 5xx exceptions until the maxAttempts gets exhausted.

Else, you can simply retry for HttpServerErrorException.class by replacing ABCDException.class.

Rahul Dey
  • 163
  • 2
  • 7