Java 测试http连接
我正在制作一个spring引导应用程序,希望在其中启用对某些http连接的重试。我对这个应用程序如何比较状态代码并触发函数以执行重试有点困惑。任何关于这方面的见解都会有帮助,我假设有一个函数可以检索状态代码,但我找不到它。下面是我创建的类的示例: 公共类HttpFailedConnectionRetryPolicy扩展ExceptionClassifierRetryPolicy{Java 测试http连接,java,spring-boot,Java,Spring Boot,我正在制作一个spring引导应用程序,希望在其中启用对某些http连接的重试。我对这个应用程序如何比较状态代码并触发函数以执行重试有点困惑。任何关于这方面的见解都会有帮助,我假设有一个函数可以检索状态代码,但我找不到它。下面是我创建的类的示例: 公共类HttpFailedConnectionRetryPolicy扩展ExceptionClassifierRetryPolicy{ @Value("SomeValue") private Integer maxAttempts; public H
@Value("SomeValue")
private Integer maxAttempts;
public HttpFailedConnectionRetryPolicy() {
final NeverRetryPolicy doNotRetry = new NeverRetryPolicy();
final SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
simpleRetryPolicy.setMaxAttempts(maxAttempts);
this.setExceptionClassifier(new Classifier<Throwable, RetryPolicy>() {
@Override
public RetryPolicy classify(Throwable classifiable) {
if (condition) {
return simpleRetryPolicy;
}
return new NeverRetryPolicy();
}
});
}
@Value(“SomeValue”)
私有整数;
公共HttpFailedConnectionRetryPolicy(){
最终NeverRetryPolicy Donotry=新NeverRetryPolicy();
最终SimpleRetryPolicy SimpleRetryPolicy=新SimpleRetryPolicy();
simpleRetryPolicy.setMaxAttempts(maxAttempts);
this.setExceptionClassifier(新分类器(){
@凌驾
公共检索策略分类(可丢弃分类){
如果(条件){
回归简单主义;
}
返回新的NeverRetryPolicy();
}
});
}
}如果您的异常类型为HttpStatusCodeException,您可以检查e.getStatusCode() 你的情况应该是
public RetryPolicy classify(Throwable classifiable) {
if (classifiable instanceof HttpStatusCodeException) {
if(((HttpStatusCodeException)classifiable).getStatusCode().value!=404)
return simpleRetryPolicy;
}
return new NeverRetryPolicy();
}
嗨,谢谢你的评论。我期待设置重试所有失败的连接,除了404。你能再解释一下e.getStatusCode()的作用吗?我以前看过,但找不到一个好的解释。