-->

Spring Integration - writing to a error queue when

2020-07-23 03:49发布

问题:

I'm starting to use Spring integration and I don't know how to resolve this situation if it's possible.

I would like to 'catch' automatically every Exception who could happend in the service activators of my application and send this errors to a dedicated queue. Gateway is not a solution because I need some custom code so I have to use service activator elements if I have correctly understood the principle.

I thought that something like would be ok:

<jms:outbound-channel-adapter channel="errorChannel"
                                  connection-factory="jmsConnectionFactory" destination="oneErrorQueue"/>

That is not working. I don't know if errorChannel is used by spring integration for putting the errors in indeed.


thank you, It seems to work.

I've put the transformer listening to the error-channel of the inbound component starting the flow and it gets the MessagingException when an error happens in service activator. Now the problem is that this error doesn't arrive to my queue. I let you see the code:

<jms:message-driven-channel-adapter
            channel="input-channel" concurrent-consumers="1"
            max-concurrent-consumers="3" connection-factory="jmsConnectionFactory"
            transaction-manager="jtaTransactionManager" destination="myQueue" error-channel="myErrorChannel"/>

<transformer input-channel="myErrorChannel" output-channel="create-error-channel" ref="errorTransporter" method="transform"/>

    <jms:outbound-channel-adapter channel="create-error-channel"
                                  connection-factory="jmsConnectionFactory" destination="creationErrorQueue" extract-payload="true"/>

...

And the transformer:

public class ErrorTransporter {

    @Transformer
    public Message<CreateCustomerOrder> transform(MessagingException exception) {
        return (Message<CreateCustomerOrder>) exception.getFailedMessage();
    }
}

Thanks in advance for helping!

回答1:

Add an error-channel attribute to the inbound component that starts the flow

error-channel="myErrorChannel"

when an upstream component (such as your service invoked by the service-activator) throws an exception, the inbound component will put an error message on the error channel. The payload of that message is a MessagingException that has two properies:

failedMessage
cause

So, on your error flow, add a transformer...

<int:transformer input-channel="myErrorChannel"
    output-channel="toJmsChannel"
    expression="payload.failedMessage"

followed by your jms outbound channel adapter.