title: ActiveMQ Consume all messages from queue on demand
body: <p>I have REST service which consumes message from queue. I want to consume message on demand - Give me all messages which are available in queue when rest service is getting called.</p>
  
  <p>I'm using ActiveMQ with Spring. Below are the code which i'm using below code to get the message from queue. First time when i hit the service, i'm getting all the messages which are available in queue but if i further publish few more messages, even though if i'm not hitting service, message are getting subscribed. What could be the reason for this ?</p>
  
  <pre><code>while (true) {
          try {
              message = jmsTemplate.receive("TestQ");
              if (message instanceof TextMessage) {
                  try {
                      System.out.println(((TextMessage)     message).getText());
                      msg = ((TextMessage) message).getText();
                  } catch (JMSException ex) {
                      throw new RuntimeException(ex);
                  }
              } else {
                  throw new IllegalArgumentException("Message must be of type TextMessage");
              }
  
          } catch (Exception ex) {
              break;
          }
      }
  </code></pre>