Spring Integration: How to use an "error gateway" with Splitter Aggregator when Exception thrown

522 views Asked by At

I've seen more than a few posts about this topic, but can't seem to find a solution to my specific problem, which I think is a pretty typical one, namely: how to keep processing messages when an error occurs (Exception thrown) using a Splitter / Aggregator.

The best explanation that I have come upon is here. But there's no explanation of exactly what/how the filters/transformers work. And in the end, the author posts "That worked!" but without posting an updated SI.config.xml.

From what I understand, the idea is to use an "error gateway", which is downstream from the original calling gateway and after the Splitter. This Gateway's job would be if there is an Exception thrown, to deal with it, but to make sure (via a transformer or a filter) that all Messages make it to the Aggregator.

My very simplified SI.config.xml if more or less like this:

<int:gateway id="myGateway" ... /> // incoming gateway
<int:chain ... input-channel="in" output-channel="out">
  <int:splitter ... />
  <int:service-activator />
  <int:aggregator />
</int:chain>

So my question is, where exactly to stick this other gateway? And how to configure filters/transformers that (from what I gather) would grab the Message which launched an Exception and put it back on the correct channel (after logging it or whatever ...) so all Messages make it to the Aggregator.

I have looked at the SI samples, on SO, and the 2 SI books (SI in Acton and Pro SI) and can't find an example of this.

1

There are 1 answers

3
Artem Bilan On BEST ANSWER

The solution looks like:

<int:chain ... input-channel="in" output-channel="out">
  <int:splitter ... />
  <int:gateway request-channel="processChannel" errorChannel="processError"/>
  <int:aggregator />
</int:chain>

<int:chain input-channel="processChannel">
  <int:service-activator />
</int:chain>

The error handler on the processError channel should consult incoming ErrorMessage and returns some compensation which will be sent to the aggregator.

The ErrorMessage typically contains MessagingException with a failedMessage where error has caused. That failedMessage contains all useful headers to go ahead with a compensation. Some of them are replyChannel and errorChannel, but for your aggregator case you need all those correlationId, sequenceNumber, sequenceSize etc. In other words when you build compensation message to be sent forward to the downstream for the aggregator, you should copy all the headers from that failedMessage.

For more info: http://docs.spring.io/spring-integration/docs/4.3.5.RELEASE/reference/html/configuration.html#namespace-errorhandler