0

If any exception is thrown in "firstChannel" flow, either of below two is happening:

  1. With ignore-send-failures="true" property, "secondChannel" is invoked and "myErrorChannel" is not invoked.
  2. Without ignore-send-failures="true" property, "secondChannel" is not invoked and "myErrorChannel" is invoked.

Please suggest me how I can invoke both "secondChannel" and "myErrorChannel" both if any exception is thrown in "firstChannel" flow. Please see below configuration details:

<int:channel id="myErrorChannel" datatype="java.lang.Throwable"/>
    <int:service-activator input-channel="myErrorChannel" >
        <beans:bean class="org.springframework.integration.samples.jms.ErrorServiceActivatorProxy"></beans:bean>
    </int:service-activator>

<jms:message-driven-channel-adapter id="jmsIn" channel="jmsInChannel" destination="requestQueue" error-channel="myErrorChannel"/>

<int:channel id="firstChannel" />
<int:channel id="secondChannel" />

<int:recipient-list-router id="recipientListRouter" input-channel="jmsInChannel" ignore-send-failures="true">
    <int:recipient channel="firstChannel"/>
    <int:recipient channel="secondChannel"/>
</int:recipient-list-router>

<int:channel id="firstChannelOutboundChannel"/>
<int:transformer input-channel="firstChannel" output-channel="firstChannelOutboundChannel">
    <beans:bean class="org.springframework.integration.samples.jms.FileIOTransformer"></beans:bean>
</int:transformer>

<jms:outbound-channel-adapter
        id="firstChannelOutbound"
        channel="firstChannelOutboundChannel"
        connection-factory="jmsConnectionFactory"
        destination="outputQueueOne"
        auto-startup="true" />

<jms:outbound-channel-adapter
        id="secondChannelOutbound"
        channel="secondChannel"
        connection-factory="jmsConnectionFactory"
        destination="outputQueueTwo"
        auto-startup="true"/>

1 Answers1

0

It works that way because the downstream flow is like a single black box call for the first component in the flow. We send from there one message and therefor we can catch there only one exception and stop execution.

However we can just move try-catch mechanism closer to the guilty code. Since you have a problem with the first recipient, you have to handle error there and swallow exception . This way recipient-list-router won’t know about error in the first recipient and will call the second.

The easiest way to add try-catch in your FileIOTransformer. However you can use ExpressionEvaluatingRequestHandlerAdvice with its trapException to true: https://docs.spring.io/spring-integration/docs/4.3.12.RELEASE/reference/html/messaging-endpoints-chapter.html#expression-advice. However need to keep in mind that this kind of advice is applied only directly to the current component. It doesn’t catch exceptions downstream.

Another trick is to implement ChannelInterceptor with try-catch and add it into the firstChannel.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118