Below is my code to receive the message from active mq in java

```
public void main(){
                   try{ // Create a ConnectionFactory
  ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("myurl");

  // Create a Connection
  connection = connectionFactory.createConnection();
  connection.start();
  // System.out.println(connection.toString());

  //connection.setExceptionListener(this);

  // Create a Session
  session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);

  // Create the destination (Topic or Queue)
  Destination destination = session.createQueue("myqueuename");
  consumer = session.createConsumer(destination);
while (true)//never ending loop
  {
    message = consumer.receive(); //System would wait here to read the message from the queue
    if (message instanceof TextMessage)
    {
      TextMessage textMessage = (TextMessage) message;
      String text = textMessage.getText();

     System.out.println(text);

      message.acknowledge();
      session.commit(); 
    }

    }
}
finaly{
closeallconnection();
}

```

Since the while condition is true, the loop will continuously run and message.receive will wait until there is a new message. So, the problem here is I am running this from the apache tomcat and it was running successfully, but when I shutdown the tomcat this code is running behind even when I shutdown the tomcat.
If I start the tomcat again there will another service running with same even though existing code is there.
So, the help I need here is how can I receive the message continuously and wait for new messages and when I shutdown the tomcat how to shutdown this jms listener. Since, I dont know how many messages are going to be there in the queue and when I am going to get the message. Any help and suggestions would be great.