title: How does the MessageListener running in JMS? Can it load messages in the queue in real time?
body: <p>I want to load the JMS messages in a queue in real time, if is a consumer.setMessageListener(myListener) viable and how it works?</p>
  
  <p>I'm not sure how a listener working. I wrote a listener like below.</p>
  
  <pre><code>public class JmsMessageListenerExample {
  public static void main(String[] args) throws URISyntaxException, Exception {
      BrokerService broker = BrokerFactory.createBroker(new URI(
              "broker:(tcp://localhost:61616)"));
      broker.start();
      Connection connection = null;
      try {
          // Producer
          ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                  "tcp://localhost:61616");
          connection = connectionFactory.createConnection();
          Session session = connection.createSession(false,
                  Session.AUTO_ACKNOWLEDGE);
          Queue queue = session.createQueue("customerQueue");
          String payload = "Important Task";
          Message msg = session.createTextMessage(payload);
          MessageProducer producer = session.createProducer(queue);
          System.out.println("Sending text '" + payload + "'");
          producer.send(msg);
  
          // Consumer
          MessageConsumer consumer = session.createConsumer(queue);
          consumer.setMessageListener(new ConsumerMessageListener("Consumer"));
          connection.start();
      } finally {
          if (connection != null) {
              connection.close();
          }
          broker.stop();
      }
  }
  </code></pre>
  
  <p>After I run the JmsMessageListenerExample, it completed quickly and I didn't received any messages. If a listener should keep on running until I stop it?   </p>