title: durable subscription in topics using activeMQ + Spring: javax.jms.InvalidClientIDException: Client: already connected
body: <p>I'm trying to configure a topic with durable subscriptions using the integration of activeMQ and Spring 3.1. My jms-context is:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jms="http://www.springframework.org/schema/jms"
      xmlns:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd"&gt;
  
      &lt;!-- A JMS connection factory for ActiveMQ --&gt;
          &lt;bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"
          p:brokerURL="${broker.url}" /&gt;
  
      &lt;!-- A destination in ActiveMQ --&gt;
      &lt;bean id="destination" class="org.apache.activemq.command.ActiveMQTopic"&gt;
          &lt;constructor-arg value="${broker.topic}" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- A cached connection to wrap the ActiveMQ connection --&gt;
      &lt;bean id="cachedConnectionFactory"
          class="org.springframework.jms.connection.CachingConnectionFactory"
          p:targetConnectionFactory-ref="connectionFactory" p:sessionCacheSize="10" /&gt;
  
      &lt;!-- A JmsTemplate instance that uses the cached connection and destination --&gt;
      &lt;bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate"
          p:connectionFactory-ref="cachedConnectionFactory" 
          p:defaultDestination-ref="destination" 
          p:pubSubDomain="true"/&gt;
  
      &lt;jms:listener-container 
          container-type="default" destination-type="durableTopic" concurrency="1" client-id="${broker.topic.durableId}"&gt;
          &lt;jms:listener ref="myListener" destination="${broker.topic}"
              subscription="${broker.topic.durableId}" method="handleMessage" /&gt;
      &lt;/jms:listener-container&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>In my JUnit tests works perfectly, and I don't become any exceptions. But when I try it on my web application (using jetty) although I recieve the messages correctly I keep getting this exception:</p>
  
  <pre><code>javax.jms.InvalidClientIDException: Broker: localhost - Client: subscribtionDurableName already connected from xxxx
      at org.apache.activemq.broker.region.RegionBroker.addConnection(RegionBroker.java:243) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.broker.BrokerFilter.addConnection(BrokerFilter.java:92) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.advisory.AdvisoryBroker.addConnection(AdvisoryBroker.java:90) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.broker.BrokerFilter.addConnection(BrokerFilter.java:92) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.broker.BrokerFilter.addConnection(BrokerFilter.java:92) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.broker.MutableBrokerFilter.addConnection(MutableBrokerFilter.java:97) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.broker.TransportConnection.processAddConnection(TransportConnection.java:733) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.broker.jmx.ManagedTransportConnection.processAddConnection(ManagedTransportConnection.java:79) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.command.ConnectionInfo.visit(ConnectionInfo.java:139) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.broker.TransportConnection.service(TransportConnection.java:292) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.broker.TransportConnection$1.onCommand(TransportConnection.java:149) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.transport.MutexTransport.onCommand(MutexTransport.java:50) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.transport.WireFormatNegotiator.onCommand(WireFormatNegotiator.java:113) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.transport.AbstractInactivityMonitor.onCommand(AbstractInactivityMonitor.java:270) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.transport.TransportSupport.doConsume(TransportSupport.java:83) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.transport.tcp.TcpTransport.doRun(TcpTransport.java:214) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.transport.tcp.TcpTransport.run(TcpTransport.java:196) ~[activemq-core-5.7.0.jar:5.7.0]
      at java.lang.Thread.run(Unknown Source) ~[na:1.6.0_45]
  12:53:54.195 [ActiveMQ InactivityMonitor ReadCheckTimer] TRACE o.a.a.t.AbstractInactivityMonitor - A receive is in progress
  12:53:54.195 [ActiveMQ InactivityMonitor WriteCheckTimer] TRACE o.a.a.t.AbstractInactivityMonitor - xxxx message sent since last write check, resetting flag
  12:53:54.208 [org.springframework.jms.listener.DefaultMessageListenerContainer#0-1] WARN  o.s.j.l.DefaultMessageListenerContainer - Could not refresh JMS Connection for destination 'Topic' - retrying in 5000 ms. Cause: Broker: localhost - Client: subscribtionDurableName  already connected from xxx
  javax.jms.InvalidClientIDException: Broker: localhost - Client: subscribtionDurableName  already connected from xxx
      at org.apache.activemq.broker.region.RegionBroker.addConnection(RegionBroker.java:243) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.broker.BrokerFilter.addConnection(BrokerFilter.java:92) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.advisory.AdvisoryBroker.addConnection(AdvisoryBroker.java:90) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.broker.BrokerFilter.addConnection(BrokerFilter.java:92) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.broker.BrokerFilter.addConnection(BrokerFilter.java:92) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.broker.MutableBrokerFilter.addConnection(MutableBrokerFilter.java:97) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.broker.TransportConnection.processAddConnection(TransportConnection.java:733) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.broker.jmx.ManagedTransportConnection.processAddConnection(ManagedTransportConnection.java:79) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.command.ConnectionInfo.visit(ConnectionInfo.java:139) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.broker.TransportConnection.service(TransportConnection.java:292) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.broker.TransportConnection$1.onCommand(TransportConnection.java:149) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.transport.MutexTransport.onCommand(MutexTransport.java:50) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.transport.WireFormatNegotiator.onCommand(WireFormatNegotiator.java:113) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.transport.AbstractInactivityMonitor.onCommand(AbstractInactivityMonitor.java:270) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.transport.TransportSupport.doConsume(TransportSupport.java:83) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.transport.tcp.TcpTransport.doRun(TcpTransport.java:214) ~[activemq-core-5.7.0.jar:5.7.0]
      at org.apache.activemq.transport.tcp.TcpTransport.run(TcpTransport.java:196) ~[activemq-core-5.7.0.jar:5.7.0]
      at java.lang.Thread.run(Unknown Source) ~[na:1.6.0_45]
  </code></pre>
  
  <p>I tried:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jms="http://www.springframework.org/schema/jms"
      xmlns:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd"&gt;
  
      &lt;!-- A JMS connection factory for ActiveMQ --&gt;
          &lt;bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"
          p:brokerURL="${broker.url}" /&gt;
  
      &lt;!-- A destination in ActiveMQ --&gt;
      &lt;bean id="destination" class="org.apache.activemq.command.ActiveMQTopic"&gt;
          &lt;constructor-arg value="${broker.topic}" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- A JmsTemplate instance that uses the cached connection and destination --&gt;
      &lt;bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate"
          p:connectionFactory-ref="connectionFactory" 
          p:defaultDestination-ref="destination" 
          p:pubSubDomain="true"/&gt;
  
      &lt;jms:listener-container 
          container-type="default" destination-type="durableTopic" concurrency="1" client-id="${broker.topic.durableId}"&gt;
          &lt;jms:listener ref="myListener" destination="${broker.topic}"
              subscription="${broker.topic.durableId}" method="handleMessage" /&gt;
      &lt;/jms:listener-container&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>with the same result.</p>
  
  <p>Can anyone help me? Thanx!!</p>