Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring重试自定义RetryPolicy不工作_Java_Spring Boot_Spring Retry - Fatal编程技术网

Java Spring重试自定义RetryPolicy不工作

Java Spring重试自定义RetryPolicy不工作,java,spring-boot,spring-retry,Java,Spring Boot,Spring Retry,我第一次使用Spring重试。我正在尝试重试HTTP状态代码5**。 我将以下问题作为参考,并尝试创建自己的自定义重试策略: 但当我尝试执行RetryTemplate时,即使我将最大尝试次数设置为1次以上,它也只重试一次。请帮我解决这个问题。提前感谢 @Bean("MyRetryTemplate") public RetryTemplate myRetryTemplate() { RetryTemplate retryTemplate = new RetryTemplate();

我第一次使用Spring重试。我正在尝试重试HTTP状态代码5**。 我将以下问题作为参考,并尝试创建自己的自定义重试策略:

但当我尝试执行RetryTemplate时,即使我将最大尝试次数设置为1次以上,它也只重试一次。请帮我解决这个问题。提前感谢

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

    return retryTemplate;
  }
具有执行功能的服务类:

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;
 }
公共类MyServiceImpl{
@自动连线
私有RestTemplate RestTemplate;
@自动连线
@限定符(“MyRetryTemplate”)
私有RetryTemplate myRetryTemplate;
公开名单{
//其他代码
试试{
response=myRetryTemplate.execute(新的
RetryCallback(){
@凌驾
public ResponseEntity doWithRetry(RetryContext上下文)抛出
HttpStatusCodeException{
System.out.println(“重试=============================>”;
ResponseEntity responseRetry=restTemplate.exchange(Url,
HttpMethod.POST,实体,
新的ParameteredTypeReference(){
});
返回响应测试;
}
});
//其他代码
}捕获(IOE除外){
//捕获码
}捕获(ResorceAccessE异常){
//捕获码
}
返回员工;
}
CustomRetryPolicy类:

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;
}
公共类CustomRetryPolicy扩展ExceptionClassifierRetryPolicy{
私有字符串;
@施工后
公共void init(){
final RetryPolicy defaultRetry=defaultRetryPolicy();
this.setExceptionClassifier(新分类器(){
@凌驾
公共检索策略分类(可丢弃分类){
Throwable exceptionCause=classifiable.getCause();
if(HttpStatusCodeException的异常实例){
int statusCode=((HttpStatusCodeException)classifiable.getCause()).getStatusCode().value();
handleHttpErrorCode(状态代码);
}
return-neverry();
}
});
}
public void setMaxAttempts(字符串maxAttempts){
this.maxAttempts=maxAttempts;
}
私有RetryPolicy handleHttpErrorCode(int状态码){
RetryPolicy RetryPolicy=null;
开关(状态代码){
案例404:
案例500:
案例503:
案例504:
retryPolicy=defaultRetryPolicy();
打破
违约:
retryPolicy=neverRetry();
打破
}
退货政策;
}
private RetryPolicy neverRetry(){
返回新的NeverRetryPolicy();
}
私有RetryPolicy defaultRetryPolicy(){
最终SimpleRetryPolicy SimpleRetryPolicy=新SimpleRetryPolicy();
简单策略。设置最大尝试次数(5次);
回归简单主义;
}

}

请提供堆栈跟踪以了解更多信息info@HashJang它只尝试一次,然后跳转到ResourceAccessException捕获块谢谢!