如何在Spring集成中回滚从IBM MQ获取的消息

时间:2014-10-27 14:53:57

标签: spring-integration spring-jms

我有一个像这样的弹簧集成流程:

    1) message-driven-channel-adapter -> 
                 1.1) output-channel connected to -> service-activator -> outbound-channel-adapter (for sending response)
                 1.2) error-channel connected to -> exception-type-router 
                            1.2.1) message is sent to different queues depending on the exception type using outbound-channel-adapter

我在message-driven-channel-adapter中设置了acknowledge =“transacted”。我想在错误通道之后为特定类型的异常引入回滚。

首先,我尝试将异常类型路由器输出连接到service-activator。但我得到例外:

   Code: 

     <service-activator id="rollBackActivator" input-channel="RollBackChannel" 
             ref="errorTransformer" method="rollBackMessage"/>

     public void rollBackMessage(MessagingException  message){       
         TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
     System.out.println("Message rolled back:"+TransactionAspectSupport.currentTransactionStatus().isRollbackOnly());
 }

   Exception:
   org.springframework.messaging.MessageHandlingException: org.springframework.transaction.NoTransactionException: No transaction aspect-managed TransactionStatus in scope

然后,我尝试使用出站通道适配器表达式,但再次遇到另一个异常

Code:
<outbound-channel-adapter id="rollbackOut" channel="RollBackChannel" 
     expression="T(org.springframework.transaction.interceptor.TransactionAspectSupport).currentTransactionStatus().setRollbackOnly()"/>

 Exception:
 org.springframework.messaging.MessageHandlingException: Expression evaluation failed: T(org.springframework.transaction.interceptor.TransactionAspectSupport).currentTransactionStatus().setRollbackOnly()

请建议在此方案中实施回滚。

1 个答案:

答案 0 :(得分:0)

默认情况下,容器在会话中使用本地事务。没有涉及AOP。只需抛出异常,容器就会回滚消息。