gpt4 book ai didi

spring-integration - Spring Integration 消息流中的条件重试建议?

转载 作者:行者123 更新时间:2023-12-04 03:09:41 33 4
gpt4 key购买 nike

我有一个偶尔会返回 503 错误的 http 网关调用。我想配置 retry advice围绕那个调用,但我不想为每个错误都这样做,只是 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),但我没有看到一种明显的方法来控制如何根据我的建议应用建议可以在错误处理程序中执行。 This question处理相同的问题,但答案没有解释如何控制建议行为或在错误处理程序级别重新发出请求。是否有特定的异常类型可以抛出?

编辑:基于答案的示例:

<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 将它与其他人区分开来并且不重试,我建议采取查看:

 * 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 工厂初始化期间进行评估。我建议你这样做:

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

并且在 ExpressionRetryPolicy bean 定义中做:

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

克服碰撞。

关于spring-integration - Spring Integration 消息流中的条件重试建议?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46310714/

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