JMS QueueBrowser Example

This example shows you how to use a JMS QueueBrowser with ActiveMQ.
Queues are a standard part of JMS, please consult the JMS 1.1 specification for full details.
A QueueBrowser is used to look at messages on the queue without removing them. It can scan the entire content of a queue or only messages matching a message selector.

The example will send 2 messages on a queue, use a QueueBrowser to browse the queue (looking at the message without removing them) and finally consume the 2 messages

Example step-by-step

To run the example, simply type mvn verify -Pexample from this directory

  1. First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the client-jndi.properties file in the directory ../common/config
  2.            InitialContext initialContext = getContext();
            
  3. We look up the JMS queue object from JNDI
  4.            Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
            
  5. We look up the JMS connection factory object from JNDI
  6.            ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
            
  7. We create a JMS connection
  8.            connection = cf.createConnection();
            
  9. We create a JMS session. The session is created as non transacted and will auto acknowledge messages.
  10.            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
  11. We create a JMS message producer on the session. This will be used to send the messages.
  12.           MessageProducer messageProducer = session.createProducer(topic);
           
  13. We create 2 JMS text messages that we are going to send.
  14.             TextMessage message_1 = session.createTextMessage("this is the 1st message");
                TextMessage message_2 = session.createTextMessage("this is the 2nd message");
            
  15. We send messages to the queue
  16.            messageProducer.send(message_1);
               messageProducer.send(message_2);
            
  17. We create a JMS QueueBrowser.
    We have not specified a message selector so the browser will enumerate the entire content of the queue.
  18.            QueueBrowser browser = session.createBrowser(queue);
            
  19. We browse the queue and display all the messages' text
  20.             Enumeration messageEnum = browser.getEnumeration();
                while (messageEnum.hasMoreElements())
                {
                   TextMessage message = (TextMessage)messageEnum.nextElement();
                   System.out.println("Browsing: " + message.getText());
                }
            
  21. We close the browser once we have finished to use it
  22.             browser.close();
            

    The messages were browsed but they were not removed from the queue. We will now consume them.

  23. We create a JMS Message Consumer to receive the messages.
  24.            MessageConsumer messageConsumer = session.createConsumer(queue);
            
  25. We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started
  26.            connection.start();
            
  27. The 2 messages arrive at the consumer
  28.            TextMessage messageReceived = (TextMessage)messageConsumer.receive(5000);
               System.out.println("Received message: " + messageReceived.getText());
               messageReceived = (TextMessage)messageConsumer.receive(5000);
               System.out.println("Received message: " + messageReceived.getText());
            
  29. And finally, always remember to close your JMS connections and resources after use, in a finally block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
  30.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }