Java EE MDB Message Selector Example

This example shows you how to send a message to an MDB that is configured to use a message selector

The example will send deploy a simple MDB and demonstrate sending a message and the MDB consuming only the message that matches the message selector.

The example leverages the JBoss Arquillian framework to run a WildFly instance and deploy the MDB.

Example step-by-step

download WildFly 8.0.0.Final from here and install.

set the JBOSS_HOME property to point to the WildFly install directory

type mvn verify from the example directory to run

  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 jndi.properties file in the directory config
  2.            final Properties env = new Properties();
    
               env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
    
               env.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
    
               initialContext = new InitialContext(env);
            
  3. We look up the JMS queue object from JNDI
  4.            Queue queue = (Queue)initialContext.lookup("jms/queues/testQueue");
            
  5. We look up the JMS connection factory object from JNDI
  6.            ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/jms/RemoteConnectionFactory");
            
  7. We create a JMS connection
  8.            connection = cf.createConnection("guest", "password");
            
  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(queue);
           
  13. We create a JMS text messages that we are going to send.
  14.             TextMessage blueMessage = session.createTextMessage("This is a text message");
            
  15. Set the color property on the message to 'BLUE'.
  16.             blueMessage.setStringProperty("color", "BLUE");
            
  17. We send messages to the queue
  18.            messageProducer.send(blueMessage);
            
  19. We create a second JMS text message that we are going to send.
  20.             TextMessage redMessage = session.createTextMessage("This is a text message");
            
  21. Set the color property on the message to 'RED'.
  22.             redMessage.setStringProperty("color", "RED");
            
  23. We send messages to the queue
  24.            messageProducer.send(redMessage);
            
  25. The MDB receives the message
    We know the message is a TextMessage so we cast to it.
  26.            TextMessage tm = (TextMessage)message;
            
  27. We get the color property to check it
  28.            String color = textMessage.getStringProperty("color");
            
  29. The MDB gets the text and color and print it
  30.             String text = tm.getText();
                System.out.println("message " + text + " received color=" + color);
                
            
  31. 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
  32.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }