JMS Topic Selector Example 1

This example shows how messages can be consumed from a topic using Message Selectors.

Consumers (or Subscribers) will only consume messages routed to a topic that match the provided selector

Topics and selectors are a standard part of JMS, please consult the JMS 1.1 specification for full details.

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 = 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 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. Create one non-durable subscriber with a specific filter
  14.          MessageConsumer messageConsumer1 = session.createConsumer(topic, "someID=1", false);
            
  15. Create a second non-durable subscriber with a specific filter
  16.          MessageConsumer messageConsumer2 = session.createConsumer(topic, "someID=2", false);
            
  17. Create a third non-durable subscriber without any filters, and it should receive the complete set of messages
  18.          MessageConsumer messageConsumer3 = session.createConsumer(topic, "someID=2", false);
            
  19. Send 20 messages, 10 with someID=1, 10 with someID=2
  20. 
             for (int i = 1; i < 10; i++)
             {
                for (int someID = 1; someID <= 2; someID++)
                {
                   TextMessage message1 = session.createTextMessage("This is a text message " + i +
                                                                    " sent for someID=" +
                                                                    someID);
    
                   message1.setIntProperty("someID", someID);
    
                   producer.send(message1);
    
                   System.out.println("Sent message: " + message1.getText());
                }
             }
  21. We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started
  22.            connection.start();
            
  23. Consume the messages from MessageConsumer1, filtering out someID=2
  24. 
             for (;;)
             {
                TextMessage messageReceivedA = (TextMessage)messageConsumer1.receive(1000);
                if (messageReceivedA == null)
                {
                   break;
                }
    
                System.out.println("messageConsumer1 received " + messageReceivedA.getText() +
                                   " someID = " +
                                   messageReceivedA.getIntProperty("someID"));
             }
  25. Consume the messages from MessageConsumer2, filtering out someID=1
  26. 
            for (;;)
             {
                TextMessage messageReceivedB = (TextMessage)messageConsumer2.receive(1000);
                if (messageReceivedB == null)
                {
                   break;
                }
    
                System.out.println("messageConsumer2 received " + messageReceivedB.getText() +
                                   " someID = " +
                                   messageReceivedB.getIntProperty("someID"));
             }
  27. Consume the messages from MessageConsumer3, receiving the complete set of messages
  28. 
             for (;;)
             {
                TextMessage messageReceivedC = (TextMessage)messageConsumer3.receive(1000);
                if (messageReceivedC == null)
                {
                   break;
                }
                System.out.println("messageConsumer3 received " + messageReceivedC.getText() +
                                   " someID = " +
                                   messageReceivedC.getIntProperty("someID"));
             }
  29. Close the consumers
  30. subscriberA.close();
    subscriberB.close();
  31. Delete the subscriptions when you're done
  32. session.unsubscribe("sub-a1");
    session.unsubscribe("sub-a2");
  33. 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
  34.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }