title: Spring Retry Custom RetryPolicy not working
body: <p>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:</p>
  
  <p><a href="https://stackoverflow.com/questions/44150323/">Spring Retry Junit: Testing Retry template with Custom Retry Policy</a></p>
  
  <p>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!!!</p>
  
  <pre><code>  @Bean("MyRetryTemplate")
    public RetryTemplate myRetryTemplate() {
    RetryTemplate retryTemplate = new RetryTemplate();
      CustomRetryPolicy customRetryPolicy = new CustomRetryPolicy();
      customRetryPolicy.setMaxAttempts(5);
      retryTemplate.setRetryPolicy(customRetryPolicy);
  
      return retryTemplate;
    }
  </code></pre>
  
  <p>Service Class having execute:</p>
  
  <pre><code>public class MyServiceImpl {
  
  @Autowired
  private RestTemplate restTemplate;
  
  @Autowired
  @Qualifier("MyRetryTemplate")
  private RetryTemplate myRetryTemplate;
  
  
  public List&lt;Employee&gt; getEmployees(){
  
  // other code 
  
  try {          
       response = myRetryTemplate.execute(new 
        RetryCallback&lt;ResponseEntity&lt;String&gt;, HttpStatusCodeException&gt;() {
           @Override
          public ResponseEntity doWithRetry(RetryContext context) throws 
                 HttpStatusCodeException {
                  System.out.println("Retrying ========================&gt;");
             ResponseEntity&lt;String&gt; responseRetry = restTemplate.exchange(Url, 
                                HttpMethod.POST, entity,
                                  new ParameterizedTypeReference&lt;String&gt;() {
                   });
             return responseRetry;
          }
       });
  
    //other code
  
    } catch (IOExcetption e){
    // catch code
  
    } catch(ResorceAccessException e){
    // catch code
  
    }
    return employee;
   }
  </code></pre>
  
  <p>CustomRetryPolicy Class:</p>
  
  <pre><code>public class CustomRetryPolicy extends ExceptionClassifierRetryPolicy {
  
  private String maxAttempts;
  
  @PostConstruct
  public void init() {
  
      final RetryPolicy defaultRetry = defaultRetryPolicy();
      this.setExceptionClassifier(new Classifier&lt;Throwable, RetryPolicy&gt;() {
          @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;
  }
  </code></pre>
  
  <p>}</p>