title: Twice dequeued JMS messages using ActiveMq and Atomikos
body: <p>I use ActiveMq as JMS server and Atomikos as transaction manager.</p>
  
  <p>On ActiveMq Admin web-interface I see that one message was enqueued, but 2 (!) messages were dequeued.</p>
  
  <p>However, jms consumer process message only once, there is no duplication in processing. When I use simple Spring JmsTransactionManager, there are one enqueued and one dequeued messages. The problem appeares only for Atomikos JTA transaction manager.</p>
  
  <p>What is the problem? How to configure Atomikos not to see twice dequeued messages?</p>
  
  <p>My configuration is below. It almost the same as in tutorial. BTW, Atomikos's spring integration example works well but it uses Spring 2.0 whereas I use Spring 3.1.</p>
  
  <pre><code>&lt;bean id="activeMqXaConnectionFactory" class="org.apache.activemq.spring.ActiveMQXAConnectionFactory"&gt;
      &lt;property name="brokerURL" value="tcp://localhost:61616"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="atomikosQueueConnectionFactory" class="com.atomikos.jms.QueueConnectionFactoryBean" init-method="init"&gt;
      &lt;property name="resourceName" value="xaMq"/&gt;
      &lt;property name="xaQueueConnectionFactory" ref="activeMqXaConnectionFactory"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="singleConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"&gt;
      &lt;property name="targetConnectionFactory" ref="atomikosQueueConnectionFactory"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="jmsDefaultContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"&gt;
      &lt;property name="connectionFactory" ref="singleConnectionFactory"/&gt;
      &lt;property name="messageListener" ref="consumer"/&gt;
      &lt;property name="concurrentConsumers" value="1"/&gt;
      &lt;property name="destinationName" value="q.jtaxa"/&gt;
      &lt;property name="receiveTimeout" value="3000"/&gt;
      &lt;property name="recoveryInterval" value="5000"/&gt;
      &lt;property name="transactionManager" ref="transactionManager"/&gt;
      &lt;property name="sessionTransacted" value="true"/&gt;
  &lt;/bean&gt;
  
  &lt;!-- Transactions --&gt;
  &lt;!--Construct Atomikos UserTransactionManager, needed to configure Spring--&gt;
  &lt;bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init"
        destroy-method="close"&gt;
      &lt;!--  when close is called, should we force transactions to terminate or not? --&gt;
      &lt;property name="forceShutdown" value="true"/&gt;
  &lt;/bean&gt;
  
  &lt;!-- Also use Atomikos UserTransactionImp, needed to configure Spring  --&gt;
  &lt;bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"&gt;
      &lt;property name="transactionTimeout" value="300"/&gt;
  &lt;/bean&gt;
  
  &lt;!-- Configure the Spring framework to use JTA transactions from Atomikos --&gt;
  &lt;bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"&gt;
      &lt;property name="transactionManager" ref="atomikosTransactionManager"/&gt;
      &lt;property name="userTransaction" ref="atomikosUserTransaction"/&gt;
      &lt;property name="nestedTransactionAllowed" value="true"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Consumer class is:</p>
  
  <pre><code>@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();
          }
      }
    }
  }
  </code></pre>