Vidya_85
Vidya_85

Reputation: 77

How to rollback message taken from IBM MQ in spring integration

I have a spring integration flow like this:

    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

I have set acknowledge="transacted" in message-driven-channel-adapter. I want to introduce rollback for a specific type of exception, after error-channel.

First, I tried to connect the exception-type-router output to service-activator. But i get exception:

   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

Then, I tried with outbound-channel-adapter expression , But got another exception again

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()

Please advise to implement rollback in this scenario.

Upvotes: 1

Views: 1515

Answers (1)

Gary Russell
Gary Russell

Reputation: 174564

The container uses local transactions on the session by default. There's no AOP involved. Simply throw an exception and the container will roll back the message.

Upvotes: 0

Related Questions