title: spring jms listener consumer size doesn't go down
body: <p><a href="https://i.stack.imgur.com/AWDsH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AWDsH.png" alt="enter image description here"></a>I have below spring jms listener configuration to handle large number of messages. when there are more than 200 message arrives, it's able to process all message w/o any error and while looking at JMX console, it shows around 13560 consumer active to process message. However after all messages are handled (i.e.processing is done), the number of consumer doesn't go down, it shows same number.  Can someone tell me what could be the reason? Am i missing any configuration or code?</p>
  
  <pre><code>&lt;bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate"&gt;
          &lt;property name="environment"&gt;
              &lt;props&gt;
                  &lt;prop key="java.naming.factory.initial"&gt;org.jnp.interfaces.NamingContextFactory&lt;/prop&gt;
                  &lt;prop key="java.naming.provider.url"&gt;${PROVIDER.URL}&lt;/prop&gt;
                  &lt;prop key="java.naming.factory.url.pkgs"&gt;org.jboss.naming:org.jnp.interfaces&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
          &lt;property name="jndiTemplate" ref="jndiTemplate" /&gt;
          &lt;property name="jndiName" value="/ConnectionFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jmsQueueConnectionFactory"
          class="org.springframework.jms.connection.CachingConnectionFactory" &gt;
          &lt;property name="targetConnectionFactory"&gt;
              &lt;ref bean="connectionFactory" /&gt;
          &lt;/property&gt;
          &lt;property name="sessionCacheSize" value="5"/&gt;
          &lt;!--  &lt;property name="reconnectOnException" value="true" /&gt; --&gt;
      &lt;/bean&gt;
  
  
  
      &lt;bean id="jmsDestinationResolver"
          class="org.springframework.jms.support.destination.JndiDestinationResolver"&gt;
          &lt;property name="jndiTemplate" ref="jndiTemplate" /&gt;
          &lt;property name="cache" value="true" /&gt;
      &lt;/bean&gt;
  
      &lt;jms:listener-container connection-factory="jmsQueueConnectionFactory"
              destination-type="queue" 
              destination-resolver="jmsDestinationResolver" 
              error-handler="errorHandler"
              acknowledge="auto"
              concurrency="10-50"
              cache="consumer"&gt;
              &lt;jms:listener destination="${REMOTE.REQUEST.QUEUE}"
                  ref="reportGenerationMessageDelegate"
                  selector="messageType = '${PDF.REPORT.MESSAGE.TYPE}' OR 
                            messageType = '${CSV.REPORT.MESSAGE.TYPE}'" /&gt;
          &lt;/jms:listener-container&gt;
  </code></pre>
  
  <p>So current design is as below:
  1. i have Jboss esb service class, which sends message on RequestQueue and waiting for response on ResponseQueue. Here is configuration of the class </p>
  
  <pre><code>&lt;bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate"&gt;
      &lt;property name="environment"&gt;
          &lt;props&gt;
              &lt;prop key="java.naming.factory.initial"&gt;org.jnp.interfaces.NamingContextFactory&lt;/prop&gt;
              &lt;prop key="java.naming.provider.url"&gt;${JMS.server.URL}&lt;/prop&gt;
              &lt;prop key="java.naming.factory.url.pkgs"&gt;org.jboss.naming:org.jnp.interfaces&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
      &lt;property name="jndiTemplate" ref="jndiTemplate" /&gt;
      &lt;property name="jndiName" value="/ConnectionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="jmsDestinationResolver"
      class="org.springframework.jms.support.destination.JndiDestinationResolver"&gt;
      &lt;property name="jndiTemplate" ref="jndiTemplate" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"&gt;
      &lt;property name="connectionFactory" ref="connectionFactory" /&gt;
      &lt;property name="destinationResolver" ref="jmsDestinationResolver" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>The listener class config is as below:</p>
  
  <p>
      
          <br>
      
      
  
  </p>
  
  <p>SOAReportGenreation class extends AbstractRemoteRequestListener class which receives message </p>
  
  <pre><code>    public abstract class AbstractRemoteRequestListener {
  
          /**
       * Method processMessage.
       * @param message ObjectMessage
       * @return String
       * @throws JMSException
       * @throws Exception
       */
      public abstract String processMessage(ObjectMessage message) throws JMSException, Exception;
  
      /**
       * Method handleMessage.
       * @param message ObjectMessage
       * @return String
       * @throws JMSException
       * @throws Exception
       */
      @SuppressWarnings("unchecked")
      public String consumeMessage(ObjectMessage message) throws JMSException, Exception{
        try {
          if (message != null) {
              l.info("Message received for messageType= "+ message.getStringProperty("messageType"));
              Map&lt;String, Object&gt; map = (HashMap&lt;String, Object&gt;) message.getObject();
              return processMessage(message);
          }
      } catch (Exception e) {
          L.error(e);
  }
        return null;
      }
  }
  </code></pre>
  
  <h2>SOAReportGeneration.java</h2>
  
  <pre><code>public class SOAReportGeneration extends AbstractRemoteRequestListener{
      private static final Logger L = Logger.getLogger(SOAReportGeneration.class);
      private static ApplicationContext context;
  
      @Override
      public String processMessage(ObjectMessage message) throws JMSException, Exception {
          if(context == null){
              context = new ClassPathXmlApplicationContext("serviceContext.xml");
          }else{
              L.info("using already initialized spring context");
          }
          //business logic. i am loading service context to get bean definition
          //and instantiate certain beans for business processing.
          return null;
      }
  </code></pre>