Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring integration error channel when using PollableChannel

We're using Spring integration in my application. I'd like put some objects into channel for asynchronous processing and error handling. So for this, I configured MessageGateway with error channel and PollableChannel for handling objects to be processed.

The problem

So I'm calling messageGateway.processMessage(message) to put message into channel. This works as expected - calling this method is non-blocking, messages get processed and are forwarded to next channel. But when processing method throws an exception, it is not redirected to error channel.

On the other hand when I change my processing channel from PollableChannel to SubscribableChannel, error channel works as expected, but calling the gateway is of course blocking. What am I missing? Can I have both non blocking call and error channel?

The code

Component doing the message processing:

@Component
public MessageProcessor {

     @Transactional
     @ServiceActivator(inputChannel = "msg.process", outputChannel = "msg.postprocess")
     public void processMessage(MyMessage message) {
         // Message processing that may throw exception
     }
}    

Channel definition:

@Configuration
public class IntegrationConfig {

    @Bean(name = "msg.process")
    private MessageChannel processChannel() {
        return new RendezvousChannel();
    }

    @Bean(name = "msg.error")
    private MessageChannel errorChannel() {
        return new DirectChannel();
    }
}

My gateway looks like this:

@MessagingGateway(errorChannel = "msg.error")
public interface MessagingGateway {

    @Gateway(requestChannel = "msg.processing")
    void processMessage(MyMessage message);
}

Error handler:

@Component
public ErrorHandlers {

     @Transactional
     @ServiceActivator(inputChannel = "msg.error")
     public void processError(MessagingException me) {
         // Error handling is here
     }
}
like image 922
Kejml Avatar asked Jul 09 '26 20:07

Kejml


1 Answers

But when processing method throws an exception, it is not redirected to error channel.

When a gateway method returns void, the calling thread is released immediately when it returns to the gateway.

The gateway does not add an error channel header in this case (in the next release - 5.0) we have changed that.

In the meantime, you can use a header enricher to set the errorChannel header to your error channel. You can also use the defaultHeaders property on the @MessagingGateway - see the comments on this answer.

like image 193
Gary Russell Avatar answered Jul 11 '26 13:07

Gary Russell