# JMS Message Priority Example

    To run the example, simply type mvn verify from this directory,
    or mvn -PnoServer verify if you want to start and create the server manually.

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`

               InitialContext initialContext = getContext();

2. We look-up the JMS queue object from JNDI

               Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");

3. We look-up the JMS connection factory object from JNDI

               ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");

4. We create a JMS connection

               connection = cf.createConnection();

5. We create a JMS session. The session is created as non transacted and will auto acknowledge messages.

               Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

6. We create a JMS message producer on the session. This will be used to send the messages.

              MessageProducer messageProducer = session.createProducer(topic);

7. We Create a JMS Message Consumer.

               MessageConsumer redConsumer = session.createConsumer(queue);
               redConsumer.setMessageListener(new SimpleMessageListener());

8. We Create three messages.

               TextMessage[] sentMessages = new TextMessage[3];
               sentMessages[0] = session.createTextMessage("first message");
               sentMessages[1] = session.createTextMessage("second message");
               sentMessages[2] = session.createTextMessage("third message");

9. Send the Messages, each has a different priority.

               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());

10. We start the connection now.

               connection.start();

11. We wait for message delivery completion

               Thread.sleep(5000);

12. We wait for message delivery completion

               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;
                  }
               }

13. 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

               finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }