java Spring JMS 侦听器中的事务管理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27736082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:18:50  来源:igfitidea点击:

Transaction Management in Spring JMS listener

javaspringtransactionsspring-jmsdistributed-transactions

提问by KBR

I have a spring JMS listener which is listening to queue . Once the message arrives at the input queue , it does certain processing on the message and puts the messages to multiple other queues for further processing (we may call these other queues as output queues) . While its posting to other output queues, in case posting the message to one of the output queues may fail due to any reason , I want to make sure that other posts to output queues which are done prior to failure gets rolled back. Basically I want to ensure it as atomic operation . is there any annotation/configuration on the listener/container that I can use to achieve this in single transaction.?

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

Here Is the configuration that I am using

这是我正在使用的配置

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

it looks like JMS template and listener container both refer to same connection factory bean (jmsConnectionFactory)

看起来 JMS 模板和侦听器容器都引用同一个连接工厂 bean (jmsConnectionFactory)

采纳答案by Gary Russell

Set acknowledge="transacted"on the listener container; any downstream operations on the same thread, using a JmsTemplate(configured with the same connection factory) will use the container's Sessionand any failure will cause all JMS operations to roll back. The session will be committed by the container on success.

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