JMS Message Priority Example

This example shows how messages with different priorities are delivered in different orders.

The Message Priority property carries the delivery preference of sent messages. It can be set by the message's standard header field 'JMSPriority' as defined in JMS specification version 1.1. The value is of type integer, ranging from 0 (the lowest) to 9 (the highest). When messages are being delivered, their priorities will effect their order of delivery. Messages of higher priorities will likely be delivered before those of lower priorities. Messages of equal priorities are delivered in the natural order of their arrival at their destinations. Please consult the JMS 1.1 specification for full details.

In this example, three messages are sent to a queue with different priorities. The first message is sent with default priority (4), the second is sent with a higher priority (5), and the third has the highest priority (9). At the receiving end, we will show the order of receiving of the three messages. You will see that the third message, though last sent, will 'jump' forward to be the first one received. The second is also received ahead of the message first sent, but behind the third message. The first message, regardless of its being sent first, arrives last.

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 a JMS Message Consumer.
  14.            
               MessageConsumer redConsumer = session.createConsumer(queue);
               redConsumer.setMessageListener(new SimpleMessageListener());
               
            
  15. We Create three messages.
  16.            
               TextMessage[] sentMessages = new TextMessage[3];
               sentMessages[0] = session.createTextMessage("first message");
               sentMessages[1] = session.createTextMessage("second message");
               sentMessages[2] = session.createTextMessage("third message");
               
            
  17. Send the Messages, each has a different priority.
  18.            
               producer.send(sentMessages[0]);
               System.out.println("Message sent: " + sentMessages[0].getText() + " with priority: " + sentMessages[0].getJMSPriority());
               producer.send(sentMessages[1], DeliveryMode.NON_PERSISTENT, 5, 0);
               System.out.println("Message sent: " + sentMessages[1].getText() + "with priority: " + sentMessages[1].getJMSPriority());
               producer.send(sentMessages[2], DeliveryMode.NON_PERSISTENT, 9, 0);
               System.out.println("Message sent: " + sentMessages[2].getText() + "with priority: " + sentMessages[2].getJMSPriority());
               
            
  19. We start the connection now.
  20.            
               connection.start();
               
            
  21. We wait for message delivery completion
  22.            
               Thread.sleep(5000);
               
            
  23. We wait for message delivery completion
  24.            
               for (int i = 0; i < 3; i++)
               {
                  TextMessage rm = msgReceived.get(i);
                  if (!rm.getText().equals(sentMessages[2-i].getText()))
                  {
                     System.err.println("Priority is broken!");
                     result = false;
                  }
               }
               
            
  25. 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
  26.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }