Java Spring重试自定义RetryPolicy不工作
我第一次使用Spring重试。我正在尝试重试HTTP状态代码5**。 我将以下问题作为参考,并尝试创建自己的自定义重试策略: 但当我尝试执行RetryTemplate时,即使我将最大尝试次数设置为1次以上,它也只重试一次。请帮我解决这个问题。提前感谢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();
@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捕获块谢谢!