JMS Topic Selector Example 2

This example shows you how to selectively consume messages using message selectors with topic consumers.

Message selectors are strings with special syntax that can be used in creating consumers. Message consumers that are thus created only receive messages that match its selector. On message delivering, the ActiveMQ Server evaluates the corresponding message headers of the messages against each selector, if any, and then delivers the 'matched' messages to its consumer. Please consult the JMS 1.1 specification for full details.

In this example, three message consumers are created on a topic. The first consumer is created with selector 'color=red', it only receives messages that have a 'color' string property of 'red' value; the second is created with selector 'color=green', it only receives messages who have a 'color' string property of 'green' value; and the third without a selector, which means it receives all messages. To illustrate, three messages with different 'color' property values are created and sent.

Example step-by-step

To run the example, simply type mvn verify 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 topic object from JNDI
  4.            Topic topic = (Topic) initialContext.lookup("/topic/exampleTopic");
            
  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 start the connection
  10.            connection.start();
            
  11. We create a JMS session. The session is created as non transacted and will auto acknowledge messages.
  12.            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
  13. We create a JMS message producer on the session. This will be used to send the messages.
  14.           MessageProducer messageProducer = session.createProducer(topic);
           
  15. We create two selectors.
  16.            
               String redSelector = "color='red'";
               String greenSelector = "color='green'";
               
             
  17. We Create a JMS Message Consumer that receives 'red' messages.
  18.           
              MessageConsumer redConsumer = session.createConsumer(topic, redSelector);
              redConsumer.setMessageListener(new SimpleMessageListener("red"));
             
            
  19. We Create a second JMS Message Consumer that receives 'green' messages.
  20.           
              MessageConsumer greenConsumer = session.createConsumer(topic, greenSelector);
              greenConsumer.setMessageListener(new SimpleMessageListener("green"));
             
            
  21. We Create another JMS Message Consumer that receives all messages.
  22.           
              MessageConsumer allConsumer = session.createConsumer(topic);
              allConsumer.setMessageListener(new SimpleMessageListener("all"));
             
            
  23. We Create three messages, each has a different color property.
  24.            
               TextMessage redMessage = session.createTextMessage("Red");
               redMessage.setStringProperty("color", "red");
               TextMessage greenMessage = session.createTextMessage("Green");
               greenMessage.setStringProperty("color", "green");
               TextMessage blueMessage = session.createTextMessage("Blue");
               blueMessage.setStringProperty("color", "blue");
               
            
  25. We send the messages to the topic
  26.            
               producer.send(redMessage);
               System.out.println("Message sent: " + redMessage.getText());
               producer.send(greenMessage);
               System.out.println("Message sent: " + greenMessage.getText());
               producer.send(blueMessage);
               System.out.println("Message sent: " + blueMessage.getText());
               
            
  27. 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
  28.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }