Java 使用重试模板执行自定义重试策略
我有一个自定义重试策略,该策略设计为每当应用程序接收到非404的http状态代码时执行重试。我还创建了一个重试模板。我被困在如何使用重试模板执行自定义重试策略的问题上。任何关于这方面的帮助以及如何测试我的结果都将非常好。我已经包含了重试模板和自定义重试策略的代码Java 使用重试模板执行自定义重试策略,java,maven,spring-boot,spring-retry,Java,Maven,Spring Boot,Spring Retry,我有一个自定义重试策略,该策略设计为每当应用程序接收到非404的http状态代码时执行重试。我还创建了一个重试模板。我被困在如何使用重试模板执行自定义重试策略的问题上。任何关于这方面的帮助以及如何测试我的结果都将非常好。我已经包含了重试模板和自定义重试策略的代码 public RetryTemplate retryTemplate() { RetryTemplate retryTemplate = new RetryTemplate(); FixedBackOf
public RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(maxBackOffPeriod);
retryTemplate.setBackOffPolicy(backOffPolicy);
NeverRetryPolicy doNotRetry = new NeverRetryPolicy();
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(maxAttempts);
retryTemplate.setRetryPolicy(retryPolicy);
return retryTemplate;
}
public class HttpFailedConnectionRetryPolicy extends ExceptionClassifierRetryPolicy {
@Value("${maxAttempts:-1}")
private String maxAttempts;
public void HttpFailedConnectionRetryPolicy() {
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();
return handleHttpErrorCode(statusCode);
}
return simpleRetryPolicy();
}
});
}
public void setMaxAttempts(String maxAttempts) {
this.maxAttempts = maxAttempts;
}
private RetryPolicy handleHttpErrorCode(int statusCode) {
RetryPolicy retryPolicy = null;
switch (statusCode) {
case 404:
retryPolicy = doNotRetry();
break;
default:
retryPolicy = simpleRetryPolicy();
break;
}
return retryPolicy;
}
private RetryPolicy doNotRetry() {
return new NeverRetryPolicy();
}
private RetryPolicy simpleRetryPolicy() {
final SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
simpleRetryPolicy.setMaxAttempts(Integer.valueOf(maxAttempts));
return simpleRetryPolicy;
}
public RetryTemplate RetryTemplate(){
RetryTemplate RetryTemplate=新RetryTemplate();
FixedBackOffPolicy backOffPolicy=新的FixedBackOffPolicy();
退避政策。退避期(maxBackOffPeriod);
retryTemplate.setBackOffPolicy(backOffPolicy);
NeverRetryPolicy donotry=新的NeverRetryPolicy();
SimpleRetryPolicy retryPolicy=新的SimpleRetryPolicy();
retryPolicy.setMaxAttempts(maxAttempts);
setRetryPolicy(retryPolicy);
返回retryTemplate;
}
公共类HttpFailedConnectionRetryPolicy扩展ExceptionClassifierRetryPolicy{
@值(${maxAttempts:-1}”)
私有字符串;
公共无效HttpFailedConnectionRetryPolicy(){
this.setExceptionClassifier(新分类器(){
@凌驾
公共检索策略分类(可丢弃分类){
Throwable exceptionCause=classifiable.getCause();
if(HttpStatusCodeException的异常实例){
int statusCode=((HttpStatusCodeException)classifiable.getCause()).getStatusCode().value();
返回handleHttpErrorCode(状态码);
}
返回simpleRetryPolicy();
}
});
}
public void setMaxAttempts(字符串maxAttempts){
this.maxAttempts=maxAttempts;
}
私有RetryPolicy handleHttpErrorCode(int状态码){
RetryPolicy RetryPolicy=null;
开关(状态代码){
案例404:
retryPolicy=donotry();
打破
违约:
retryPolicy=simpleRetryPolicy();
打破
}
退货政策;
}
私人RetryPolicy doNotRetry(){
返回新的NeverRetryPolicy();
}
私人RetryPolicy simpleRetryPolicy(){
最终SimpleRetryPolicy SimpleRetryPolicy=新SimpleRetryPolicy();
simpleRetryPolicy.setMaxAttempts(Integer.valueOf(maxAttempts));
回归简单主义;
}
}只需将重试策略的实例插入模板,而不是
SimpleRetryPolicy
要进行测试,您可以将RetryListener
添加到测试用例中的模板中