【问题标题】:Spring -@Recover - Cannot locate recovery methodSpring -@Recover - 找不到恢复方法
【发布时间】:2021-06-14 19:15:24
【问题描述】:

我有一个 Spring 项目,其中包含一个类中的 Retryable 和 Recover 方法。 重试 2 次后,代码无法命中 Recover 方法块,错误为

Cannot locate recovery method; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:8081/api/start": Connection refused; nested exception is java.net.ConnectException: Connection refused

Retryable 方法附加到 AOP。

代码可在 Github Repo 中找到:https://github.com/Nikhilgupta1891/RetryRecover

在 repo 中,这里是相关的类名:

  1. AOP:ApcAspect.java
  2. 调度程序(入口点):ScheduledClass.java
  3. 服务类未运行恢复:ClassTwo.java#L37

附上ClassTwo的代码供快速参考:

package com.abc.pbm.racassignmentpoll.services;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class ClassTwo {
    private final RestTemplate restTemplate = new RestTemplate();
    private final String crimApiBaseUrl = "http://localhost:8081/api/";

    @Retryable(maxAttemptsExpression = "2", backoff = @Backoff(delayExpression = "5000"))
    public ResponseEntity startSecondMethod(String invEligDt, String runType) {
        Map<String, Object> reqBody = new HashMap<>();
        reqBody.put("INV_ELIG_DT", invEligDt);
        reqBody.put("RUNTYPE", runType);

        ResponseEntity responseEntity = restTemplate.postForEntity(crimApiBaseUrl + "start",
                reqBody,
                null,
                Collections.EMPTY_MAP);
        return responseEntity;
    }

    @Recover
    public void recover(Exception error, String invEligDt, String runType){
        // Some action.
        log.info("INside recovery");
    }
}

【问题讨论】:

  • 仅供参考,如果其他人发现此问题:此问题是我回答的问题here 的后续问题。示例存储库也是一样的。

标签: java spring spring-boot aop


【解决方案1】:

两个方法的返回类型和输入参数:Retryable和Recovery方法应该相同。

【讨论】:

  • 在回答问题(包括自己的问题)时,请添加更多详细信息,以便对其他人有所帮助。您也没有在此处发布任何更正的示例代码以供其他人学习如何修复原始代码,也没有将修复问题的提交推送到您的 GitHub 存储库。更糟糕的是,您还提交了大量垃圾(二进制文件、IntelliJ 项目文件),即在检查您的提交时,其他人“因为树木太多而看不到森林”,正如我们用德语所说的那样。请尝试在这里发布您的帖子以及您的承诺,以使它们有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
相关资源
最近更新 更多