Spring JMS监听器中的事务管理

时间:2015-01-02 00:33:01

标签: java spring transactions spring-jms distributed-transactions

我有一个正在侦听队列的spring JMS侦听器。一旦消息到达输入队列,它就对消息进行某些处理,并将消息放到多个其他队列中进行进一步处理(我们可以将这些其他队列称为输出队列)。当它发布到其他输出队列时,如果将消息发布到其中一个输出队列可能由于任何原因而失败,我想确保在故障之前完成的输出队列的其他帖子被回滚。基本上我想确保它作为原子操作。 在侦听器/容器上是否有任何注释/配置可用于在单个事务中实现此目的。?

这是我正在使用的配置

<jms:listener-container acknowledge="transacted" cache="session" connection-factory="jmsSecurityFactory" concurrency="1" container-type="default" container-class="abc.xyz">
<jms:listener id="listenerId" destination="inputQueue" ref="" />
</jms:listener-container>
<beans:bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<beans:property name="sessionTransacted" value="true"></beans:property>
<beans:property name="connectionFactory" ref="inCachingConnectionFactory"></beans:property>
</beans:bean>
<beans:bean id="inCachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
    <beans:property name="targetConnectionFactory" ref="jmsSecurityFactory" />
</beans:bean>
<beans:bean id="jmsSecurityFactory"
    class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
    <beans:property name="targetConnectionFactory" ref="jmsConnectionFactory" />
    <beans:property name="username" value=" " />
    <beans:property name="password" value=" " />
</beans:bean>
<beans:bean id="jmsConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
    <beans:property name="hostName" value="${mq.conn.hostName}" />
    <beans:property name="port" value="${mq.conn.hostPort}" />
    <beans:property name="queueManager" value="${mq.conn.queueManager}" />
    <beans:property name="channel" value="${mq.conn.channel}" />
    <beans:property name="transportType" value="${mq.conn.transportType}" />
    <beans:property name="useConnectionPooling" value="true"></beans:property>
</beans:bean>

看起来JMS模板和监听器容器都引用相同的连接工厂bean(jmsConnectionFactory)

1 个答案:

答案 0 :(得分:5)

在侦听器容器上设置acknowledge="transacted";使用JmsTemplate(使用相同的连接工厂配置)在同一线程上的任何下游操作都将使用容器的Session,任何失败都将导致所有JMS操作回滚。容器会在成功时提交会话。

相关问题