Name

JMSPriority — Purpose: Routing

Synopsis

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. The priority of messages can be declared by the JMS client using the setPriority( ) method on the producer. 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 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 operation. The following code shows how to override the priority of a message during the send operation. In both cases, the priority is set to 3:

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

queueSender.send(message,DeliveryMode.PERSISTENT, 3, 0); // pub/sub setting the priority on the ...

Get Java Message Service 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.