title: JMS QueueBrowser hanging on getEnumeration()
body: <p>I'm fiddling around with a JMS Queue using ActiveMQ and I wanted to work out a Queue Browser. I've looked up some examples on the web but I'm having quite a bit of trouble getting it to work.</p>
  
  <p>Here is the relevant code:</p>
  
  <pre><code>   QueueBrowser browser = template.execute(new SessionCallback&lt;QueueBrowser&gt;() {
     public QueueBrowser doInJms(Session session) throws JMSException {
             return session.createBrowser(queue);
                      }
                  }, true);
  
                 Enumeration messages = browser.getEnumeration();
                 int num = 0;
                  while(messages.hasMoreElements()) {
                      messages.nextElement();
                      num +=1;
                  }
                  logger.info("Num = " + num);
  </code></pre>
  
  <p>I've injected in the template and queue, and using some log statements I have verified that they are the objects I want. I can even retrieve the queue from the QueueBrowser once it is instantiated. I've populated the queue with about 100 messages and am reading them off using a custom JmsMessageListener that has about a 1 sec delay between reads. </p>
  
  <p>However, the issue is that the code hangs around the <code>while(messages.hasMoreElements()</code> line. From debugging, it never even goes inside the loop. If I have that line of code in a logger to check the value the hanging also occurs.</p>
  
  <p>If I merely use the enumeration to <code>nextElement()</code> without looping, then I'm returned a null value. I cannot determine whether this means the queue or enumeration is empty. If so, shouldn't messages <code>hasMoreElements()</code> return a proper boolean value?</p>
  
  <p>Could the issue be that I'm reading messages with a JmsMessageListener? Am I using the template wrong? I can't really determine where something is going wrong because it appears that 90% of it is working, its just the last 10% that seems to be acting oddly.</p>
  
  <p>EDIT:</p>
  
  <p>Managed to get it working. My session was being closed before I ever called the browser to do anything. All I needed to do was move the browser code into the doInJms block as such:</p>
  
  <pre><code>            template.execute(new SessionCallback&lt;QueueBrowser&gt;() {
                  public QueueBrowser doInJms(Session session) throws JMSException {
                      QueueBrowser browser =  session.createBrowser(queue);
                      Enumeration messages = browser.getEnumeration();
                      int num = 0;
                      logger.info("" + messages.hasMoreElements());
                      while(messages.hasMoreElements()) {
                          messages.nextElement();
                          num +=1;
                      }
                      logger.info("Num = " + num);
                      return null;
                  }
              }, true);
  </code></pre>