We will discuss JMS Prioritize messages in detail with examples. The JMSPriority header field of each message represents its priority. I have talked about prioritizing messages in Message Model Tutorial.

JMS has 10 priority levels, 0 to 9. 0 is the lowest priority and 9 is the highest priority. As per the JMS standard, a message with priority 0-4 is the normal priority and 5-9 is considered as an expedited priority.

JMS Priority example

In the below example, messages with different priorities are sent to the receiver. However, the one with the highest priority (9) arrives first and similarly, the least priority arrives in the end.

package lab01.message.model;

import labxx.common.settings.CommonSettings;
import javax.jms.*;

public class MessagePriorityTest {
  public static void main(String[] args) throws JMSException {

    ConnectionFactory connectionFactory = CommonSettings.getConnectionFactory();
    Queue queue = CommonSettings.getDefaultQueue();

    try (JMSContext jmsContext = connectionFactory.createContext()) {
      JMSProducer producer = jmsContext.createProducer();
      String[] messages = {"Msg One", "Msg two", "Msg three", "Msg four", "Msg five"};

      producer.setPriority(0).send(queue, messages[0]);
      producer.setPriority(9).send(queue, messages[1]);
      producer.setPriority(4).send(queue, messages[2]);
      producer.setPriority(2).send(queue, messages[3]);
      producer.send(queue, messages[4]);

      JMSConsumer consumer = jmsContext.createConsumer(queue);
      for (int i = 0; i < messages.length; i++) {
        Message message = consumer.receive();
        System.out.println("Priority: " + message.getJMSPriority() + " #### Message: " + message.getBody(String.class));
      }
    }
  }
}

Output

Priority: 9 #### Message: Msg two
Priority: 4 #### Message: Msg three
Priority: 2 #### Message: Msg four
Priority: 2 #### Message: Msg five
Priority: 0 #### Message: Msg One

JMS Message default priority

Even if you do not specify the message priority, JMS assigns a default priority to each message. Look at the below code example, you can see in the output that each message has priority 4.

package lab01.message.model;

import labxx.common.settings.CommonSettings;
import javax.jms.*;

public class MessageDefaultPriorityTest {
  public static void main(String[] args) throws JMSException {

    ConnectionFactory connectionFactory = CommonSettings.getConnectionFactory();
    Queue queue = CommonSettings.getDefaultQueue();

    try (JMSContext jmsContext = connectionFactory.createContext()) {
      JMSProducer producer = jmsContext.createProducer();
      String[] messages = {"Msg One", "Msg two", "Msg three", "Msg four", "Msg five"};

      producer.send(queue, messages[0]);
      producer.send(queue, messages[1]);
      producer.send(queue, messages[2]);
      producer.send(queue, messages[3]);
      producer.send(queue, messages[4]);

      JMSConsumer consumer = jmsContext.createConsumer(queue);
      for (int i = 0; i < messages.length; i++) {
        Message message = consumer.receive();
        System.out.println("Default Priority: " + message.getJMSPriority() + " #### Message: " + message.getBody(String.class));
      }
    }
  }
}

Output

Default Priority: 4 #### Message: Msg One
Default Priority: 4 #### Message: Msg two
Default Priority: 4 #### Message: Msg three
Default Priority: 4 #### Message: Msg four
Default Priority: 4 #### Message: Msg five

This is all as part of JMS Prioritize messages, you have learned how to assign custom priority to control the message delivery.