Reputation: 2204
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.
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?
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
}
}
Upvotes: 3
Views: 1530
Reputation: 174769
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.
Upvotes: 4