Name

JMSPriority — Purpose: Routing

Messages may be assigned a priority by the message producer when they are delivered. The message servers may use message’s priority to order delivery of messages to consumers; messages with a higher priority are delivered ahead of lower priority messages.

The message’s priority is contained in the JMSPriority header, which is set automatically by the JMS provider. If not specified, the message priority is set to a default value of 4. The priority of messages can be declared by the JMS client using the setPriority() method on the MessageProducer (not the Message object!). The following code shows how this method is used by both the p2p and pub/sub message models:

// p2p setting the message priority to 9
QueueSender queueSender = QueueSession.createSender(someQueue);
queueSender.setPriority(9);

//pub/sub setting the message priority to 9
TopicPublisher topicPublisher = TopicSession.createPublisher(someTopic);
topicPublisher.setPriority(9);            

Once a priority is established on a message producer (QueueSender or TopicPublisher), that priority will be used for all messages delivered from that producer, unless it is explicitly overridden. The priority of a specific message can be overridden during the send or publish operation. The following code shows how to override the priority of a message during the send and publish operations. In both cases, the priority is set to 3:

// p2p setting the priority on the send operation QueueSender queueSender = QueueSession.createSender(someQueue); ...

Get Java Message Service, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.