Static Message Selector Example

This example shows you how to configure a ActiveMQ queue with static message selectors (filters) (to configure a static selector directly on a JMS queue, please see the static-selector-jms example).

Static message selectors are ActiveMQ's extension to message selectors as defined in JMS spec 1.1. Rather than specifying the selector in the application code, static message selectors are defined in one of ActiveMQ's configuration files, activemq-configuration.xml, as an element called 'filter' inside each queue definition, like


      <queues>     
      	<queue name="jms.queue.selectorQueue">
      	    <address>jms.queue.selectorQueue</address>
      	    <filter string="color='red'"/>
      	</queue>
      </queues>
     

Once configured the queue 'selectorQueue' only delivers messages that are selected against the filter, i.e., only the messages whose 'color' properties are of 'red' values can be received by its consumers. Those that don't match the filter will be dropped by the queue and therefore will never be delivered to any of its consumers.

In the example code, five messages with different 'color' property values are sent to queue 'selectorQueue'. One consumer is created to receive messages from the queue. Of the five sent messages, two are of 'red' color properties, one is 'blue', one is 'green' and one has not the 'color' propery at all. The result is that the consumer only gets the two 'red' messages.

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 queue object from JNDI, this is the queue that has filter configured with it.
  4.            Queue queue = (Queue) initialContext.lookup("/queue/selectorQueue");
            
  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 producer = session.createProducer(queue);
           
  15. We create a JMS message consumer that receives 'red' messages. The message listener will check the 'color' property on each received message.
  16.            
             MessageConsumer redConsumer = session.createConsumer(queue);
             redConsumer.setMessageListener(new SimpleMessageListener("red"));
               
             
  17. We reate five messages with different 'color' properties.
  18.           
             TextMessage redMessage1 = session.createTextMessage("Red-1");
             redMessage1.setStringProperty("color", "red");
             TextMessage redMessage2 = session.createTextMessage("Red-2");
             redMessage2.setStringProperty("color", "red");
             TextMessage greenMessage = session.createTextMessage("Green");
             greenMessage.setStringProperty("color", "green");
             TextMessage blueMessage = session.createTextMessage("Blue");
             blueMessage.setStringProperty("color", "blue");
             TextMessage normalMessage = session.createTextMessage("No color");
             
            
  19. We send those messages.
  20.           
             producer.send(redMessage1);
             System.out.println("Message sent: " + redMessage1.getText());
             producer.send(greenMessage);
             System.out.println("Message sent: " + greenMessage.getText());
             producer.send(blueMessage);
             System.out.println("Message sent: " + blueMessage.getText());
             producer.send(redMessage2);
             System.out.println("Message sent: " + redMessage2.getText());
             producer.send(normalMessage);
             System.out.println("Message sent: " + normalMessage.getText());
              
            
  21. Waiting for the message listener to check the received messages.
  22.           
              Thread.sleep(5000);
             
            
  23. 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
  24.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }