首页 文章

使用ActiveMq和Atomikos两次使JMS消息出列

提问于
浏览
2

我使用ActiveMq作为JMS服务器,使用Atomikos作为事务管理器 .

在ActiveMq Admin Web界面上,我看到一条消息已入队,但2(!)条消息已出列 .

但是,jms消费者进程消息只有一次,处理中没有重复 . 当我使用简单的Spring JmsTransactionManager时,有一个排队的消息和一个排队的消息 . 该问题仅出现在Atomikos JTA事务管理器中 .

问题是什么?如何配置Atomikos不要看到两次出列的消息?

我的配置如下 . 它与教程中的几乎相同 . 顺便说一句,Atomikos的spring集成示例运行良好,但它使用的是Spring 2.0,而我使用的是Spring 3.1 .

<bean id="activeMqXaConnectionFactory" class="org.apache.activemq.spring.ActiveMQXAConnectionFactory">
    <property name="brokerURL" value="tcp://localhost:61616"/>
</bean>

<bean id="atomikosQueueConnectionFactory" class="com.atomikos.jms.QueueConnectionFactoryBean" init-method="init">
    <property name="resourceName" value="xaMq"/>
    <property name="xaQueueConnectionFactory" ref="activeMqXaConnectionFactory"/>
</bean>

<bean id="singleConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
    <property name="targetConnectionFactory" ref="atomikosQueueConnectionFactory"/>
</bean>

<bean id="jmsDefaultContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="singleConnectionFactory"/>
    <property name="messageListener" ref="consumer"/>
    <property name="concurrentConsumers" value="1"/>
    <property name="destinationName" value="q.jtaxa"/>
    <property name="receiveTimeout" value="3000"/>
    <property name="recoveryInterval" value="5000"/>
    <property name="transactionManager" ref="transactionManager"/>
    <property name="sessionTransacted" value="true"/>
</bean>

<!-- Transactions -->
<!--Construct Atomikos UserTransactionManager, needed to configure Spring-->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init"
      destroy-method="close">
    <!--  when close is called, should we force transactions to terminate or not? -->
    <property name="forceShutdown" value="true"/>
</bean>

<!-- Also use Atomikos UserTransactionImp, needed to configure Spring  -->
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
    <property name="transactionTimeout" value="300"/>
</bean>

<!-- Configure the Spring framework to use JTA transactions from Atomikos -->
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager" ref="atomikosTransactionManager"/>
    <property name="userTransaction" ref="atomikosUserTransaction"/>
    <property name="nestedTransactionAllowed" value="true"/>
</bean>

消费者类是:

@Component
public class Consumer implements MessageListener {
  @Override
  public void onMessage(Message message) {
    if (message instanceof TextMessage) {
        try {
            TextMessage textMsg = (TextMessage) message;
            System.out.println("         " + textMsg.getText());
        } catch (JMSException ex) {
            ex.printStackTrace();
        }
    }
  }
}

1 回答

  • 1

    我找到了错误配置的内容 . 这是“atomikosQueueConnectionFactory” . 我确实参与了教程,但它应该只是AtomikosConnectionFactoryBean类而不是QueueConnectionFactoryBean . 我删除了atomikosQueueConnectionFactory并添加了atomikosConnectionFactory

    <bean id="atomikosConnectionFactory" class="com.atomikos.jms.AtomikosConnectionFactoryBean" init-method="init">
        <property name="uniqueResourceName" value="amq1"/>
        <property name="xaConnectionFactory" ref="mqXaConnectionFactory"/>
    </bean>
    

    它之后工作正常 . 我找到了正确的配置here .

相关问题