gpt4 book ai didi

java - Spring 重试: NeverRetryLogic Not working as I expected with ExceptionClassifierRetryPolicy

转载 作者:行者123 更新时间:2023-12-02 02:48:51 24 4
gpt4 key购买 nike

我正在重试场景中工作(与 http 出站网关相关)。重试逻辑工作得很好,但我不重试的逻辑看起来有一个错误。

我想做的是,如果我收到与 404,500,503,504 不同的 http 状态错误,则不要重试。

为了测试它,我有一个可配置端点,我可以将其配置为响应任何 http 状态错误 X 次,然后才能成功。

例如,我可以将端点配置为在第一次访问时获取 http 400 状态,之后当我重试时,我将获得成功的响应。

也就是说,我所期望的是,当我将端点配置为第一次获取 http 400 状态时,永远不会重试,但看起来这不起作用。

我对永不重试场景的逻辑是这样的:

   <int-http:outbound-gateway
header-mapper="httpHeaderMapper"
request-channel="some_request_channel"
url-expression="'http://some_url"
http-method="POST"
expected-response-type="java.lang.String"
charset="UTF-8"
reply-timeout="${com.property.value.from.db.for.time.out:5000}"
reply-channel="some_reply_channel">

<int-http:request-handler-advice-chain>
<bean class="org.springframework.integration.handler.advice.RequestHandlerRetryAdvice">
<property name="recoveryCallback">
<bean class="org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer">
<constructor-arg ref="errorChannel" />
</bean>
</property>
<property name="retryTemplate" ref="retryTemplate" />
</bean>
</int-http:request-handler-advice-chain>

</int-http:outbound-gateway>


<bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">
<property name="retryPolicy">
<bean class="com.whatever.CustomRetryPolicy">
<property name="maxAttempts" value="${com.property.value.from.db.for.retry.MaxAttemps:5}" />
</bean>
</property>
<property name="backOffPolicy">
<bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
<property name="initialInterval" value="${com.property.value.from.db.for.backoffpolicy.initialInterval:1000}" />
<property name="multiplier" value="${com.property.value.from.db.for.backoffpolicy.initialInterval:6}" />
</bean>
</property>
</bean>

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 defaultRetry;
}
});
}

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;
}

}

根据NeverRetryPolicy类,它应该这样做:

A RetryPolicy that allows the first attempt but never permits a retry. Also be used as a base class for other policies, e.g. for test purposes as a stub.

所以我的理解是,第一次尝试是当我们到达端点时,我们收到 http 400 错误状态,然后不再重试。

这有什么问题吗?

最佳答案

您总是返回默认策略;看起来您需要在这里返回...

return handleHttpErrorCode(statusCode);

顺便说一句,最好创建一次策略,而不是每次都创建新策略。

关于java - Spring 重试: NeverRetryLogic Not working as I expected with ExceptionClassifierRetryPolicy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44137355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com