【问题标题】:Jms-message-driven channel adaper not rolling back the transactionJms-消息驱动的通道适配器不回滚事务
【发布时间】:2016-11-18 20:04:21
【问题描述】:

我不知道为什么交易没有回滚。

我在我的项目中使用 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");
    }
}

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

【问题讨论】:

    标签: spring spring-integration spring-transactions


    【解决方案1】:

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

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

    请参阅 Message ChannelsTransaction Support

    【讨论】:

    • 谢谢。这就像一个魅力。但是,AMQ 会尝试投递 8 次,然后才消费该消息。如何配置将即使在 8 次尝试后仍失败的消息放入死队列?
    • 这是在broker上配置的;它不是 JMS 规范的一部分;搜索 AMQ 文档。
    猜你喜欢
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 2014-01-23
    相关资源
    最近更新 更多