java - 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
,我无法抓住它们来正确处理它们。
解决方案
您仅在调用者线程或您拥有的地方捕获异常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
或缩小错误处理的范围RequestHandlerRetryAdvice
:https ://docs.spring.io/spring-integration/docs/5.0.6.RELEASE/reference/html/messaging-endpoints-chapter.html#消息处理程序建议链
推荐阅读
- r - 用前 13 个值的平均值估算缺失值
- javascript - 如何在 Nuxt 中使用动态 CSS 文件?
- javascript - 导出数据集对不同格式做出本机反应
- datepicker - Datepicker 不能只允许 ODOO V11 上的未来日期
- ios - 尝试保存数组,出现错误 - 无法将“[数据]”类型的值转换为预期的参数类型“[字典”
]' - python - Python:我怎样才能理解输入?
- unity3d - Unity3d:由于 C# 本机集成,无法在游戏对象中添加脚本组件
- airflow - 气流 - SQL Server 连接
- rest-assured - 无法使用 REST Assured 生成 Auth 令牌
- outlook - 使用 Surface 时 Outlook 加载项无法输入成功