title: Activemq - Exceeded the maximum number of allowed client connections
body: <p>My acitvemq server always print error below :</p>
  
  <pre><code>2014-07-12 16:14:27,820 | ERROR | Could not accept connection : 
  org.apache.activemq.transport.tcp.ExceededMaximumConnectionsException:
  Exceeded the maximum number of allowed client connections.
  See the 'maximumConnections' property on the TCP transport configuration URI 
  in the ActiveMQ configuration file (e.g., activemq.xml) 
  | org.apache.activemq.broker.TransportConnector 
  | ActiveMQ Transport Server Thread Handler:
   tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireformat.maxFrameSize=104857600
  </code></pre>
  
  <p>When I restart the server it will be ok. But after a few days the error come out again.
  <strong>I don't why the connections always increase to 1000.</strong></p>
  
  <p>My server config:</p>
  
  <pre><code>&lt;!-- activeMQ --&gt;
  &lt;bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"&gt;
      &lt;property name="brokerURL" value="${jms.brokerURL}"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;!-- Spring Caching  --&gt;
  &lt;bean id="cachingConnectionFactory"
      class="org.springframework.jms.connection.CachingConnectionFactory"&gt;
      &lt;property name="targetConnectionFactory" ref="jmsConnectionFactory" /&gt;
      &lt;property name="sessionCacheSize" value="10" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Spring JMS Template --&gt;
  &lt;bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"&gt;
      &lt;property name="connectionFactory" ref="cachingConnectionFactory" /&gt;
      &lt;property name="explicitQosEnabled" value="true" /&gt;
      &lt;property name="priority" value="4" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="scoreQueue" class="org.apache.activemq.command.ActiveMQQueue"&gt;
      &lt;constructor-arg value="SCORE" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="scoreMessage" class="com.tt.score.mq.server.ScoreMessage"&gt;&lt;/bean&gt;
  
  &lt;bean id="scoreListener"
      class="org.springframework.jms.listener.DefaultMessageListenerContainer"&gt;
      &lt;property name="connectionFactory" ref="jmsConnectionFactory"&gt;&lt;/property&gt;
      &lt;property name="destination" ref="scoreQueue"&gt;&lt;/property&gt;
      &lt;property name="messageListener" ref="scoreMessage"&gt;&lt;/property&gt;
      &lt;property name="concurrentConsumers" value="10" /&gt;
      &lt;property name="maxConcurrentConsumers" value="100" /&gt;
      &lt;property name="sessionAcknowledgeModeName" value="CLIENT_ACKNOWLEDGE" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>My client config xml:
  
      
          
      </p>
  
  <pre><code>&lt;!-- Spring Caching --&gt;
  &lt;bean id="cachingConnectionFactory"
      class="org.springframework.jms.connection.CachingConnectionFactory"&gt;
      &lt;property name="targetConnectionFactory" ref="jmsConnectionFactory" /&gt;
      &lt;property name="sessionCacheSize" value="10" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Spring JMS Template --&gt;
  &lt;bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"&gt;
      &lt;property name="connectionFactory" ref="cachingConnectionFactory" /&gt;
      &lt;property name="explicitQosEnabled" value="true" /&gt;
      &lt;property name="priority" value="4" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="messageProducer" class="com.tt.score.mq.client.MessageProducer"&gt;
      &lt;property name="jmsTemplate" ref="jmsTemplate" /&gt;
      &lt;property name="scoreQueue" ref="scoreQueue" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="scoreQueue" class="org.apache.activemq.command.ActiveMQQueue"&gt;
      &lt;constructor-arg value="SCORE" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Other info:</p>
  
  <ul>
  <li>acitvemq server : 5.8.0 </li>
  <li>client acitvemq : 5.4.2 </li>
  <li>spring : 3.0.7</li>
  <li>spring-jms : 3.0.7</li>
  </ul>
  
  <p>We use transactionManager so the DefaultMessageListenerContainer's cachelevel will be set to none.</p>
  
  <p>---update add dao config----------------------------------</p>
  
  <pre><code>&lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
          &lt;property name="driverClassName"&gt;&lt;value&gt;${jdbc.driverClass}&lt;/value&gt;&lt;/property&gt;
          &lt;property name="username"&gt;&lt;value&gt;${jdbc.user}&lt;/value&gt;&lt;/property&gt;
          &lt;property name="url"&gt;&lt;value&gt;${jdbc.jdbcUrl}&lt;/value&gt;&lt;/property&gt;
          &lt;property name="password"&gt;
              &lt;bean class="com.tongbanjie.commons.util.EncryptDBPasswordFactory"&gt;
                  &lt;property name="password" value="${jdbc.password}" /&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
          &lt;property name="maxActive"&gt;&lt;value&gt;${jdbc.maxActive}&lt;/value&gt;&lt;/property&gt;
          &lt;property name="initialSize"&gt;&lt;value&gt;${jdbc.initialSize}&lt;/value&gt;&lt;/property&gt;
          &lt;property name="maxWait"&gt;&lt;value&gt;60000&lt;/value&gt;&lt;/property&gt;
          &lt;property name="maxIdle"&gt;&lt;value&gt;${jdbc.maxIdle}&lt;/value&gt;&lt;/property&gt;
          &lt;property name="minIdle"&gt;&lt;value&gt;5&lt;/value&gt;&lt;/property&gt;
          &lt;property name="removeAbandoned"&gt;&lt;value&gt;true&lt;/value&gt;&lt;/property&gt;
          &lt;property name="removeAbandonedTimeout"&gt;&lt;value&gt;180&lt;/value&gt;&lt;/property&gt;
          &lt;property name="timeBetweenEvictionRunsMillis"&gt;&lt;value&gt;60000&lt;/value&gt;&lt;/property&gt;
          &lt;property name="minEvictableIdleTimeMillis"&gt;&lt;value&gt;1800000&lt;/value&gt;&lt;/property&gt;
          &lt;property name="defaultAutoCommit" value="false" /&gt;
          &lt;property name="connectionProperties"&gt;
              &lt;value&gt;bigStringTryClob=true;clientEncoding=UTF-8;defaultRowPrefetch=50;serverEncoding=ISO-8859-1&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  
      &lt;bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"&gt;
          &lt;property name="dataSource"&gt;
              &lt;ref bean="dataSource"/&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- myBatis --&gt;
      &lt;bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&gt;
          &lt;property name="configLocation" value="classpath:META-INF/mybatis/score-configuration.xml" /&gt;
          &lt;property name="mapperLocations" value="classpath*:META-INF/mybatis/mapper/*.xml" /&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="commonSqlSessionDao" abstract="true"&gt;
          &lt;property name="sqlSessionFactory"&gt;
              &lt;ref bean="sqlSessionFactory" /&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p>-----post the code that we how to use the template now</p>
  
  <p>the jmsTemplate wrapped in a class </p>
  
  <pre><code>public class MessageProducer {
  
      private JmsTemplate   jmsTemplate;
      private ActiveMQQueue scoreQueue;
  
      public void sendScoreQueue(Map&lt;String, String&gt; userMap) {
          sendMessage(this.scoreQueue, userMap);
      }
  
      private void sendMessage(Destination destination, final Map&lt;String, String&gt; map) {
          this.jmsTemplate.send(destination, new MessageCreator() {
  
              public Message createMessage(Session session) throws JMSException {
                  MapMessage message = session.createMapMessage();
                  for (String key : map.keySet()) {
                      message.setStringProperty(key, (String) map.get(key));
                  }
                  return message;
              }
          });
      }
  ｝
  </code></pre>
  
  <p>And we use a thead to send call the <code>MessageProducer</code> class's <code>sendScoreQueue</code> method.
  As follows:</p>
  
  <pre><code> //the code is old and ugly.that is the original position we call the mq.
   ThreadUtils.execute(new Thread(new SendMsgThread(dycc, ScoreMQSendType.SEND_TYPE_SCORE)));
  </code></pre>
  
  <p>///</p>
  
  <pre><code>public class ThreadUtils {
  
      protected static ThreadPoolExecutor executor = null;
      public static Properties            Props    = null;
  
      public static void execute(Thread thread) {
          executor.execute(thread);
      }
  
    static {
          if (executor == null) 
              Integer corePoolSize = 5;
              Integer maximumPoolSize = 10;
              Integer keepAliveTime = 3000;
              executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MINUTES,
                                                new LinkedBlockingQueue());
      }
  }
  
  public class SendMsgThread implements Runnable {
  
      private Log                    log      = LogFactory.getLog(SendMsgThread.class);
  
      private Map&lt;String, String&gt;    map;
      private String                 type;
  
      private static MessageProducer producer = null;
  
      public SendMsgThread(Map&lt;String, String&gt; map, String type){
          this.type = type;
          this.map = map;
      }
  
      public void run() {
          try {
              if(type.equals(ScoreMQSendType.SEND_TYPE_SCORE) || type.equals(ScoreMQSendType.SEND_TYPE_REGISTER)) {
                  producer.sendMessage(map);
              }
          } catch (Exception e) {
              this.log.error("sendMsgThread sendScoreQueue error.", e);
          }
      }
  
   static {
          if (producer == null) producer = 
  (MessageProducer )SpringContextHolder.getBean(MessageProducer .class);
      }
  }
  </code></pre>