Spring Transformer -Intergration Exception Handling

I have spring-integration that takes an org.w3c.dom.Document object and returns a domain object. And it’s nice. If there are no elements, I throw an application exception.

However, I would like to get this exception on the error channel, but instead of how it works at the moment, going through the chain of handlers. It would be nice if there was a way to indicate the error channel in case of a failed conversion.

I could:

  • send a message through the router to check the absence of elements before (or after) the transformer
  • send message

However, this means both analyzing the document twice, and a bit of rewriting.

+4
source share
2 answers

The answer I came up with was to change the return type of the transformer from the POJO domain to Message. And then, in case of exception, return the message. The exception is then routed to the correct router by the payload type router.

+5
source

To determine where to send exception messages, you need to set the "errorChannel" header. For instance,

<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 an error channel header before calling the transformer. If the transformer succeeds, it continues to move along the chain, but if it throws an exception, then a MessagingException will be sent as a message to myErrorChannel. (If you want the method for handling the exception to be different later in the chain, you could enrich the header again after the transformer has updated the errorChannel header to the next place you want to send the exceptions.)

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

0
source

All Articles