Spring Integration 消息流中的条件重试建议? [英] Conditional retry advice in a Spring Integration message flow?

查看:28
本文介绍了Spring Integration 消息流中的条件重试建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 http 网关调用,偶尔会返回 503 错误.我想围绕该调用配置 重试建议,但我不想对每个错误都这样做,只是 503s.

I have a http gateway call that's occasionally returning 503 errors. I'd like to configure retry advice around that call, but I don't want to do it for every error, just the 503s.

<int-http:outbound-gateway ... errorHandler="...">
    <int-http:request-handler-advice-chain>
        <int:retry-advice max-attempts="3" />
    </int-http:request-handler-advice-chain>
</int-http:outbound-gateway>

我已经配置了一个自定义错误处理程序来过滤我不想将其视为错误的状态(例如:404),但我没有看到一种明显的方法来控制根据我的内容应用建议的方式可以在错误处理程序中做.这个问题 处理相同的问题,但答案并未解释如何在错误处理程序级别控制建议行为或重新发出请求.是否有特定的异常类型要抛出?

I already have a custom error handler configured that filters statuses (ex: 404) that I don't want to treat as errors, but I don't see an obvious way to control how the advice is applied based on what I can do in the error handler. This question deals with the same issue, but the answer doesn't explain how to control the advice behavior or reissue the request at the error handler level. Is there a specific exception type to throw?

基于答案的示例:

<bean id="spelParser" class="org.springframework.expression.spel.standard.SpelExpressionParser" />

<int-http:outbound-gateway ...>
    <int-http:request-handler-advice-chain>
        <bean class="org.springframework.integration.handler.advice.RequestHandlerRetryAdvice">
            <property name="retryTemplate">
                <bean class="org.springframework.retry.support.RetryTemplate">
                    <property name="retryPolicy">
                        <bean class="org.springframework.retry.policy.ExpressionRetryPolicy">
                            <constructor-arg index="0" type="org.springframework.expression.Expression" value="#{spelParser.parseExpression('cause.statusCode.value() == 503')}" />
                            <property name="maxAttempts" value="3" />
                        </bean>
                    </property>
                    <property name="backOffPolicy">
                        <bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
                            <property name="initialInterval" value="1000" />
                            <property name="multiplier" value="2" />
                        </bean>
                    </property>
                </bean>
            </property>
        </bean>
    </int-http:request-handler-advice-chain>
</int-http:outbound-gateway>

推荐答案

好吧,对于当您有 HttpServerErrorException 但想通过 statusCode 区分它的情况来自其他人,不要重试,我建议看看:

Well, for the case when you have a HttpServerErrorException but would like to distinguish it by the statusCode from others and don't retry, I would suggest to take a look into the:

 * Subclass of {@link SimpleRetryPolicy} that delegates to super.canRetry() and,
 * if true, further evaluates an expression against the last thrown exception.
 *
 * @author Gary Russell
 * @since 1.2
 *
 */
@SuppressWarnings("serial")
public class ExpressionRetryPolicy extends SimpleRetryPolicy implements BeanFactoryAware {

你的表达方式如下:

expression="statusCode.value() == 503"

更新

啊!我明白了.由于 ExpressionRetryPolicy 使用 TemplateParserContext,您的表达式肯定必须像 #{statusCode.value() == 503}.但与此同时,它将在 bean 工厂初始化期间进行评估.我建议你做这样的事情:

Ah! I see. Since ExpressionRetryPolicy uses TemplateParserContext your expression definitely must be like #{statusCode.value() == 503}. But at the same time it is going to be evaluate during bean factory initialization. I suggest you to do something like this:

<bean id="spelParser" class="org.springframework.expression.spel.standard.SpelExpressionParser"/>

并在 ExpressionRetryPolicy bean 定义中执行:

and in the ExpressionRetryPolicy bean definition do:

<constructor-arg index="0" type="org.springframework.expression.Expression" 
                 value="#{spelParser.parseExpression('statusCode.value() == 503')}" />

克服碰撞.

这篇关于Spring Integration 消息流中的条件重试建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆