2016-07-15 82 views
0

由于我不知道为什么交易没有回滚,我的头靠在墙上。Jms消息驱动的渠道adaper不回滚交易

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

当我张贴在队列中的消息,我看到异常但队列中的消息已被占用。事务不回滚,消息不会放回队列中。请让我知道我出错的地方。

回答

1

因为processEmpChannel是一个QueueChannel;只要消息放入通道队列(在由服务处理之前),事务就立即提交。

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

参见Message ChannelsTransaction Support

+0

谢谢。这就像一个魅力。但是,AMQ会尝试传送8次,然后消息被消耗。如何配置将失败的消息即使在8次尝试后置于死队列中? – zilcuanu

+1

这是在代理上配置的;它不是JMS规范的一部分;搜索AMQ文档。 –