title: Spring JMS listener-container concurrency attribute not working
body: <p>Hi I am learning Spring JMS with ActiveMQ. 
  In my example scenario is Producer application sends around 50 messages in queue and when I start Consumer application it starts to  consume those messages.</p>
  
  <p>Now I want multiple consumer threads to consume messages from queue.
  I am using <strong>JMS listener-container</strong>. When I googled for that I found there is a <strong>concurrency</strong> attribute.</p>
  
  <p>According to <a href="http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/jms.html#jms-namespace" rel="nofollow">Spring JMS doc</a> concurrency attribute specifies</p>
  
  <blockquote>
    <p>The number of concurrent sessions/consumers to start for each listener. Can either be a simple number indicating the maximum number (e.g. "5") or a range indicating the lower as well as the upper limit (e.g. "3-5"). Note that a specified minimum is just a hint and might be ignored at runtime. Default is 1; keep concurrency limited to 1 in case of a topic listener or if queue ordering is important; consider raising it for general queues.</p>
  </blockquote>
  
  <p>But in my configuration I am setting this attribute to 5 but it seems it fails to start 5 concurrent listeners.</p>
  
  <p>Configuration for listener:</p>
  
  <p><strong>consumer-applicationContext.xml</strong></p>
  
  <pre><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;bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" p:brokerURL="tcp://localhost:61616" /&gt;
  
      &lt;bean id="listener" class="com.jms.example.MyMessageListener"&gt;&lt;/bean&gt;
  
      &lt;jms:listener-container container-type="default" concurrency="5"
          connection-factory="connectionFactory"&gt;
          &lt;jms:listener destination="MyQueue" ref="listener"
              method="onMessage"&gt;&lt;/jms:listener&gt;
      &lt;/jms:listener-container&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>And If I used bean <strong>DefaultMessageListenerContainer</strong> instead of <strong>jms:listener-container</strong> with properties:</p>
  
  <pre><code>&lt;bean id="msgListenerContainer" 
          class="org.springframework.jms.listener.DefaultMessageListenerContainer"
          p:connectionFactory-ref="connectionFactory"
          p:destination-ref="destination"
          p:messageListener-ref="listener"
          p:concurrentConsumers="10"
          p:maxConcurrentConsumers="50" /&gt;
  </code></pre>
  
  <p>Then in ActiveMQ console I could see 10 consumers but in reality it starts 3 consumers simultaneously or sometimes 6 or only 1 consumer.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Consumer code:</p>
  
  <pre><code>public class MyMessageListener implements MessageListener{
  
  
      public void onMessage(Message m) {
          TextMessage message=(TextMessage)m;
          try{
              System.out.println("Start = " + message.getText());
              Thread.sleep(5000);
              System.out.println("End = " + message.getText());
          }catch (Exception e) {e.printStackTrace();  }
      }
  }
  </code></pre>
  
  <p>I am printing consumed messages on console whose output is explained in scenarios below:</p>
  
  <p><strong>OBSERVATION:</strong></p>
  
  <p>I observed some weird behavior. 
  My producer and consumer are two independent applications. </p>
  
  <p><strong>Scenario - 1:</strong></p>
  
  <ol>
  <li>I start producer and send messages(Meanwhile consumer is NOT running)</li>
  <li>Then I start consumer to consume messages.</li>
  </ol>
  
  <p>Here problem is it does not loads all 10 consumers. Sometimes it loads 3 OR 1.</p>
  
  <pre><code>Start = hello jms 1 // consumer 1 started 
  Start = hello jms 2 // consumer 2 started 
  Start = hello jms 3 // consumer 3 started 
  End = hello jms 1  //  consumer 1 ended
  Start = hello jms 4 // consumer 4 started and hence always 3 consumers and not 10
  End = hello jms 2
  Start = hello jms 5
  End = hello jms 3
  Start = hello jms 6
  </code></pre>
  
  <p><strong>Scenario - 2:</strong></p>
  
  <ol>
  <li>I start producer and send messages(Meanwhile consumer is running)</li>
  <li>Since the consumer is in running state it starts to consume them.</li>
  </ol>
  
  <p>So it does load all 5 consumers properly as expected. so the output is:</p>
  
  <pre><code>Start = hello jms 1 // consumer 1 started 
  Start = hello jms 2 // consumer 2 started 
  Start = hello jms 3 // consumer 3 started 
  Start = hello jms 4 // consumer 4 started 
  Start = hello jms 5 // consumer 5 started 
  Start = hello jms 6 // consumer 6 started 
  Start = hello jms 7 // consumer 7 started 
  Start = hello jms 8 // consumer 8 started 
  Start = hello jms 9 // consumer 9 started 
  Start = hello jms 10 // consumer 10 started. Hence all them started at same time as expected.
  End = hello jms 1
  Start = hello jms 11
  End = hello jms 2
  Start = hello jms 12
  End = hello jms 3
  Start = hello jms 13
  </code></pre>
  
  <p>Why is this happening. It is really eating my brain.
  I don't want to keep consumer to be running forever. I want to keep both detached. </p>
  
  <p>Please help.</p>