Transaction handling while using message driven ch

2019-05-22 15:18发布

I am working on a POC which does the following

  1. Uses a message driven channel adapter to recieve message in a transaction
  2. Calls the Service Activator which uses a handler to insert the message recieved from the adapter to DB and also post message to outbound channel.

Now, if the DB insert of the message fails i want the JMS message returned back to the queue so that it can be re-tried later.

With my below configuration it doesnt seems to work.(i.e. even if there is a failure while inserting into the database the message is removed from the queue.

Any pointers or sample configuration would be helpful.

<integration:channel id="jmsInChannel">         
    <integration:queue/>
</integration:channel>

<int-jms:message-driven-channel-adapter id="jmsIn"
    transaction-manager="transactionManager"
    connection-factory="sConnectionFactory"
    destination-name="emsQueue"
    acknowledge="client" channel="jmsInChannel"
    extract-payload="false"/>   

<integration:service-activator input-channel="jmsInChannel"
    output-channel="fileNamesChannel" ref="handler" method="process" />

<bean id="handler" class="com.irebalpoc.integration.MessageProcessor">
    <property name="jobHashTable" ref="jobsMapping" />
</bean>

1条回答
乱世女痞
2楼-- · 2019-05-22 15:45

Set acknowledge="transacted" and, I presume the transactionManager is a JDBC (or JTA) transaction manager.

You also need to remove <queue/> from JmsInChannel so that the database transaction occurs on the same thread.

Spring will synchronize the database transaction with the JMS transaction.

However, read http://www.javaworld.com/javaworld/jw-01-2009/jw-01-spring-transactions.html for the implications.

If you can't make your service idempotent, you may need to look at an XA transaction manager.

查看更多
登录 后发表回答