Jms 消息驱动的通道适配器不回滚事务

Jms-message-driven channel adaper not rolling back the transaction

我快要撞墙了,因为我不知道为什么交易没有回滚。

我在我的项目中使用 spring 集成,我的 applicationContext.xml 如下所示:

<context:component-scan base-package="com.jms.spring.integration.*"></context:component-scan>

<tx:annotation-driven/>

<int:poller default="true" id="poller" fixed-delay="500"></int:poller>

<int-jms:message-driven-channel-adapter
    channel="processEmpChannel" destination-name="com.test.inputqueue" acknowledge="transacted" connection-factory="targetConnectionFactory"/>

<bean id="targetConnectionFactory"  class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="tcp://localhost:61616"></property>
</bean>
<bean id="springExample" class="com.jms.spring.integration.SpringIntegrationJmsExample">
</bean>

<int:service-activator input-channel="processEmpChannel"
    ref="springExample" method="handleClient">
   <int:poller ref="poller"></int:poller>
</int:service-activator>

我的 java 文件如下所示:

package com.jms.spring.integration;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.transaction.annotation.Transactional;

public class SpringIntegrationJmsExample {
    @Transactional
    public void handleClient(String str){
        System.out.println("handleClient");
        throw new RuntimeException("Throwing some runtime exception....");
    }

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    }
}

当我post队列中的消息时,我看到异常但队列中的消息已被消耗。事务没有回滚,消息也没有放回队列中。请让我知道哪里出错了。

因为processEmpChannel是一个QueueChannel;一旦消息被放入通道队列(在服务处理之前),事务就会提交。

您需要使用 DirectChannels 以使事务按预期工作,因此服务激活器在侦听器容器线程上 运行(删除轮询器)。

参见Message Channels and Transaction Support