处理 Spring-Integration 转换器中的异常

发布于 2024-11-17 05:18:03 字数 405 浏览 4 评论 0原文

我有一个 变压器接受 org.w3c.dom.Document 并返回域对象。这很好。如果缺少元素,我会引发应用程序异常。

但是,我想将该异常发送到错误通道,而不是按照当前的工作方式通过一系列处理程序进行冒泡。如果有一种方法可以在转换失败的情况下指定错误通道,那就太好了。

我可以:

  • 之前(或之后)检查丢失的元素
  • 通过路由器传递消息,以在变压器路由消息

但这意味着两次解析文档并进行一些重写。

I have a transformer that accepts a org.w3c.dom.Document and returns a domain object. And this is nice. If there are elements missing I raise an application exception.

However, I'd like to get that exception onto the error channel but instead the way it currently works by bubbling back through a chain of handlers. It would be nice if there a way of specifying an error channel in the case of a failed transform.

I could:

  • pass the message through a router to check for missing elements before (or after) the transformer
  • route the message

However that means both parsing the document twice and a bit of a re-write.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掩耳倾听 2024-11-24 05:18:03

我想到的答案是将转换器的返回类型从 POJO 域更改为 Message。然后,在异常情况下,返回一条消息。然后,异常由有效负载类型路由器路由到正确的处理程序。

The answer I came up with was to change the return type of the transformer from the domain POJO to a Message. And then, in the exception case, to return a Message. The exception is then routed to the correct handler by a Payload Type Router.

和影子一齐双人舞 2024-11-24 05:18:03

为了确定异常消息应该发送到哪里,您需要设置“errorChannel”标头。例如,

<int:chain input-channel="myInputChannel">
    <int:header-enricher>
        <int:error-channel ref="myErrorChannel" />
    </int:header-enricher>
    <int:transformer ref="myTransformer" />
    <!-- Further actions after the transformer here -->
</int:chain>

该链将在调用变压器之前分配错误通道标头。如果转换器成功,它将继续沿着链向下,但如果抛出异常,MessagingException 将作为消息发送到 myErrorChannel。 (如果您希望在链中稍后处理异常的方式有所不同,则可以在转换器之后使用另一个标头丰富器,以将 errorChannel 标头更新到您想要发送异常的下一个位置。)

请参阅中的详细信息错误处理 Spring Integration 文档的部分。

In order to determine where exception messages should go, you need to set the "errorChannel" header. For example,

<int:chain input-channel="myInputChannel">
    <int:header-enricher>
        <int:error-channel ref="myErrorChannel" />
    </int:header-enricher>
    <int:transformer ref="myTransformer" />
    <!-- Further actions after the transformer here -->
</int:chain>

This chain will assign the error channel header before calling the transformer. If the transformer succeeds, it continues down the chain, but if it throws an exception a MessagingException will be sent as a message to myErrorChannel. (If you want the way to handle an exception to be different later on in the chain, you could have another header-enricher after the transformer to update the errorChannel header to the next place you want to send exceptions.)

Refer to the details in the Error Handling section of the Spring Integration documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文