Handling exceptions in a Spring-Integration transf

2019-04-09 10:10发布

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.

2条回答
姐就是有狂的资本
2楼-- · 2019-04-09 10:38

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.

查看更多
forever°为你锁心
3楼-- · 2019-04-09 10:41

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.

查看更多
登录 后发表回答