首页 > 解决方案 > Spring 集成中使用 Http 出站网关的错误处理

问题描述

我试图了解在 Spring Integration 中应该如何处理错误。我找到了关于的文档errorChannel,我尝试使用它,但没有捕获到异常。

这是我的 HTTP 出站网关:

<int-http:outbound-gateway id="orderRequestHttpGateway"
                               request-channel="orders-channel"
                               url="${url}"
                               http-method="POST"
                               expected-response-type="java.lang.String"
                               reply-timeout='5000'
                               reply-channel='pushed-channel'
                               charset="UTF-8">
</int-http:outbound-gateway>

我不明白应该在哪里捕获该组件引发的异常(例如 500 HTTP 错误)

路线由poller. 我尝试添加一个error-channel属性:

<int:inbound-channel-adapter channel="orders-trigger-channel" expression="''">
        <int:poller fixed-delay="${poller}" error-channel="errorChannel"/>
</int:inbound-channel-adapter>

我尝试使用自定义错误通道。我还尝试覆盖现有的errorChannel:

<int:channel id="errorChannel"/>
<int:outbound-channel-adapter id="errorChannelHandler"
                               ref="errorManager"
                               method="foo"
                               channel="errorChannel"/>

到目前为止,我一直在得到MessageHandlingException,我无法抓住它们来正确处理它们。

标签: javaspringspring-integration

解决方案


您仅在调用者线程或您拥有的地方捕获异常try...catch- Spring Integration 与纯 Java 没有什么不同:只要我们有一个try...catch.

根据您的描述,error-channel="errorChannel"on the<poller>是要走的路,如果您没有任何其他线程向下游移动,那么 aMessageHandlingException真的会被扔给轮询器并包装到ErrorMessage要发送到errorChannel配置的那个中。

这是MessageHandlingException因为我们Messaging在 Spring Integration 中处理,并且这样的异常带有一些有关流和失败消息的上下文和重要信息。您500 HTTP error只是cause该消息中的一个。因此,当您捕获并处理 an 时,ErrorMessage您应该查看它的堆栈跟踪以获取要解析的信息。

另一方面,您可以通过ExpressionEvaluatingRequestHandlerAdvice或缩小错误处理的范围RequestHandlerRetryAdvicehttps ://docs.spring.io/spring-integration/docs/5.0.6.RELEASE/reference/html/messaging-endpoints-chapter.html#消息处理程序建议链


推荐阅读