Search code examples
javaspringerror-handlingspring-integrationspring-cloud-sleuth

Spring Integration Error Channel Handling Broken when used with Spring Cloud Sleuth


I have a demo project created: https://github.com/imram/si-errorhandling-sleuth.

I am having issue that when I use Spring Cloud Sleuth in my Spring Integration Application then Error Flow gets broken, i.e. gateway halts infinitely/ until timeouts if reply timeouts is given and then it returns null to invoker of the gateway.

If same application is executed without Sleuth Dependency, there is no issue.

Could someone please assist if there is something wrong in my SI setting or its known issue. If it is an issue then could someone please suggest workaround?

FYI, I do want to use Sleuth for the Correlation Id generation per transaction b/w my micro services for traceability purpose.

Thanks!!


Solution

  • It's a problem with Sleuth; I have raised an issue there.

    EDIT

    Here's a workaround - a bit cheesy but it works...

    public class SleuthWorkAroundInterceptor extends ChannelInterceptorAdapter {
    
        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            if (!(message instanceof ErrorMessage)) {
                return message;
            }
            MessagingException payload = (MessagingException) message.getPayload();
            Message<?> failedMessage = payload.getFailedMessage();
            failedMessage = MessageBuilder.fromMessage(failedMessage)
                    .removeHeader(MessageHeaders.REPLY_CHANNEL)
                    .removeHeader(MessageHeaders.ERROR_CHANNEL)
                    .build();
            return new ErrorMessage(new MessagingException(failedMessage, payload), message.getHeaders());
        }
    
    }
    

    and

    @Bean
    public SmartInitializingSingleton ecInterceptorConfigurer(AbstractMessageChannel errorChannel) {
        return () -> errorChannel.addInterceptor(0, new SleuthWorkAroundInterceptor());
    }