2

I am using Spring Retry for the first time. I am trying to retry for HTTP Status code 5**. I took below Question as reference and tried to create own Custom Retry Policy:

Spring Retry Junit: Testing Retry template with Custom Retry Policy

but when I try to execute the RetryTemplate it only retries once even if I set Maximum attempts more than 1. Please help me to resolve the issue. Thanks in advance!!!

  @Bean("MyRetryTemplate")
  public RetryTemplate myRetryTemplate() {
  RetryTemplate retryTemplate = new RetryTemplate();
    CustomRetryPolicy customRetryPolicy = new CustomRetryPolicy();
    customRetryPolicy.setMaxAttempts(5);
    retryTemplate.setRetryPolicy(customRetryPolicy);

    return retryTemplate;
  }

Service Class having execute:

public class MyServiceImpl {

@Autowired
private RestTemplate restTemplate;

@Autowired
@Qualifier("MyRetryTemplate")
private RetryTemplate myRetryTemplate;


public List<Employee> getEmployees(){

// other code 

try {          
     response = myRetryTemplate.execute(new 
      RetryCallback<ResponseEntity<String>, HttpStatusCodeException>() {
         @Override
        public ResponseEntity doWithRetry(RetryContext context) throws 
               HttpStatusCodeException {
                System.out.println("Retrying ========================>");
           ResponseEntity<String> responseRetry = restTemplate.exchange(Url, 
                              HttpMethod.POST, entity,
                                new ParameterizedTypeReference<String>() {
                 });
           return responseRetry;
        }
     });

  //other code

  } catch (IOExcetption e){
  // catch code

  } catch(ResorceAccessException e){
  // catch code

  }
  return employee;
 }

CustomRetryPolicy Class:

public class CustomRetryPolicy extends ExceptionClassifierRetryPolicy {

private String maxAttempts;

@PostConstruct
public void init() {

    final RetryPolicy defaultRetry = defaultRetryPolicy();
    this.setExceptionClassifier(new Classifier<Throwable, RetryPolicy>() {
        @Override
        public RetryPolicy classify(Throwable classifiable) {
            Throwable exceptionCause = classifiable.getCause();
            if (exceptionCause instanceof HttpStatusCodeException) {
                int statusCode = ((HttpStatusCodeException) classifiable.getCause()).getStatusCode().value();
                handleHttpErrorCode(statusCode);
            }
            return neverRetry();
        }
    });
}

public void setMaxAttempts(String maxAttempts) {
    this.maxAttempts = maxAttempts;
}


private RetryPolicy handleHttpErrorCode(int statusCode) {
    RetryPolicy retryPolicy = null;
    switch(statusCode) {
    case 404 :
    case 500 :
    case 503 :
    case 504 :
        retryPolicy = defaultRetryPolicy();
        break;
    default :
        retryPolicy = neverRetry();
        break;
    }

    return retryPolicy;
}

private RetryPolicy neverRetry() {
    return new NeverRetryPolicy();
}

private RetryPolicy defaultRetryPolicy() {
    final SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
    simpleRetryPolicy.setMaxAttempts(5);
    return simpleRetryPolicy;
}

}

0 Answers0