Spring Integration - writing to a error queue when Exception happended in a Service Activator component

我的未来我决定 提交于 2019-12-06 08:44:40

问题


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.



来源:https://stackoverflow.com/questions/12284280/spring-integration-writing-to-a-error-queue-when-exception-happended-in-a-serv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!