Handling exceptions in a Spring-Integration transformer

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 05:16:36

问题


I have a spring-integration 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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6490904/handling-exceptions-in-a-spring-integration-transformer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!