无法理解 spring 重试
Not able to understand spring retry
我正在努力理解@Retryable。我需要的是在出现 5xx 异常时重试 3 次,如果重试也失败,则在恢复方法中抛出自定义异常。如果抛出其他异常,则捕获它并抛出自定义异常。
@Retryable(value = HttpServerErrorException.class, maxAttempts = 3, backoff = @Backoff(delay = 3000))
public String callToService(String key) {
String response;
try {
response = //assume a service call here
}catch (Exception ex) {
throw new customException("some message");
}
return response;
}
@Recover
public void retryFailed(HttpServerErrorException httpServerErrorException) {
throw new customException("some message");
}
在您的情况下,您添加了:
@Retryable(value = HttpServerErrorException.class, maxAttempts = 3, backoff = @Backoff(delay = 3000))
@Retryable
用于:
value = HttpServerErrorException.class
,因此只有当 HttpServerErrorException
是您的方法代码中的 occure/thrown 并且 时,您的方法才会被重试 ]注意:如果有其他异常抛出则不会重试,也不会调用recover方法,因为recover方法只有在@Retryable中value中提到的异常才会被调用
maxAttempts = 3
,所以它会最多重试执行你的方法 3 次
backoff = @Backoff(delay = 3000)
,所以它会在重试之间保持 3000 毫秒的延迟。
- 并且在重试 3 次后,如果您的方法仍然无效,您使用
@Recover
的方法将被调用 HttpServerErrorException
我希望它有意义并帮助您理解 @Retryable
的概念
现在要实现你想要的,你需要按如下方式实现它:
@Retryable(value = HttpServerErrorException.class, maxAttempts = 3, backoff = @Backoff(delay = 3000))
public String callToService(String key) {
String response;
try {
response = //assume a service call here
} catch (HttpServerErrorException httpServerErrorException) {
throw httpServerErrorException;
} catch (Exception ex) {
throw new CustomException("some message");
}
return response;
}
@Recover
public void retryFailed(HttpServerErrorException httpServerErrorException) {
//do whatever you want here, when HttpServerErrorException occured more than 3 times
}
我正在努力理解@Retryable。我需要的是在出现 5xx 异常时重试 3 次,如果重试也失败,则在恢复方法中抛出自定义异常。如果抛出其他异常,则捕获它并抛出自定义异常。
@Retryable(value = HttpServerErrorException.class, maxAttempts = 3, backoff = @Backoff(delay = 3000))
public String callToService(String key) {
String response;
try {
response = //assume a service call here
}catch (Exception ex) {
throw new customException("some message");
}
return response;
}
@Recover
public void retryFailed(HttpServerErrorException httpServerErrorException) {
throw new customException("some message");
}
在您的情况下,您添加了:
@Retryable(value = HttpServerErrorException.class, maxAttempts = 3, backoff = @Backoff(delay = 3000))
@Retryable
用于:
value = HttpServerErrorException.class
,因此只有当HttpServerErrorException
是您的方法代码中的 occure/thrown 并且 时,您的方法才会被重试 ]注意:如果有其他异常抛出则不会重试,也不会调用recover方法,因为recover方法只有在@Retryable中value中提到的异常才会被调用maxAttempts = 3
,所以它会最多重试执行你的方法 3 次backoff = @Backoff(delay = 3000)
,所以它会在重试之间保持 3000 毫秒的延迟。- 并且在重试 3 次后,如果您的方法仍然无效,您使用
@Recover
的方法将被调用HttpServerErrorException
我希望它有意义并帮助您理解 @Retryable
现在要实现你想要的,你需要按如下方式实现它:
@Retryable(value = HttpServerErrorException.class, maxAttempts = 3, backoff = @Backoff(delay = 3000))
public String callToService(String key) {
String response;
try {
response = //assume a service call here
} catch (HttpServerErrorException httpServerErrorException) {
throw httpServerErrorException;
} catch (Exception ex) {
throw new CustomException("some message");
}
return response;
}
@Recover
public void retryFailed(HttpServerErrorException httpServerErrorException) {
//do whatever you want here, when HttpServerErrorException occured more than 3 times
}