java - Spring -@Recover - 找不到恢复方法
问题描述
我在一个类中有一个带有 Retryable 和 Recover 方法的 Spring 项目。重试 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 中,以下是相关的类名称:
- AOP:ApcAspect.java
- 调度程序(入口点):ScheduledClass.java
- 服务类未运行恢复: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");
}
}
解决方案
两个方法的返回类型和输入参数:Retryable 和 Recovery 方法应该相同。
推荐阅读
- nativescript - NativeScript DepreciationWarning : parse() 已弃用。改用 Builder.parse()
- azure - Azure blob:了解 BlobListingDetails 枚举中的不同选项
- asp.net-mvc - 从本机客户端授予对 api 的访问权限
- bash - 连接到 heroku bash
- javascript - 拖动时按下按钮
- lua - 如何使用 BodyPosition 使零件在一条线上平滑移动?
- c - 无法从 arduino 发送 RS-232 命令
- spring-boot - 如何配置 Redis Kubernetes 部署以在 master 宕机时使 slave redis pod 接管?
- c# - 如何使用 JsonConverter 在 System.Text.Json.JsonSerializer.Serialize() 中排除属性被序列化
- rust - 使用私有模块类型定义结构成员