0

我正在重试场景中工作(与 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班级,它应该这样做:

允许第一次尝试但从不允许重试的 RetryPolicy。也可用作其他策略的基类,例如作为存根用于测试目的。

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

那有什么问题?

4

1 回答 1

2

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

return handleHttpErrorCode(statusCode);

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

于 2017-05-23T15:28:32.060 回答