Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMS message priority not working on Message

I need to set message priority so that High priority messages are consumed before Low priority messages by Receivers.

First I tried with message.setJMSPriority() method to set the priority but it was not working in HornetQ and ActiveMQ so finally I set the priority of the Message Producer using setPriority() method and it works fine now.

Why isn't Messsge.setJMSPriority() working in any of the JMS vendor implementations and why do we need to set the priority of the Producer not the message itself to set the priority of the message? What is the use of Messsge.setJMSPriority() method then?

Any suggestion or comment is appreciated.

like image 944
SmartSolution Avatar asked Jul 22 '11 09:07

SmartSolution


People also ask

How do I change message priority in JMS?

You can use message priority levels to instruct the JMS provider to deliver urgent messages first. You can set the priority level in either of two ways. You can use the setPriority method of the MessageProducer interface to set the priority level for all messages sent by that producer.

How does JMS priority work?

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.

What is priority messaging?

A category of precedence reserved for messages that require expeditious action by the addressee(s) and/or furnish essential information for the conduct of operations in progress when routine precedence will not suffice. See also precedence. Dictionary of Military and Associated Terms.


2 Answers

To answer this question all you need to do is read the API docs for the setJMSPriority method and it tells you why. Here's the relevant text.

Sets the priority level for this message.

JMS providers set this field when a message is sent. This method can be used to change the value for a message that has been received.

The JMS Provider (ActiveMQ, HornetMQ, etc) set the priority in the producer on send to either the default value of 4, or to whatever value you've set the producer to use, so setting the value before send on the message itself won't have any effect.

like image 128
Tim Bish Avatar answered Sep 19 '22 08:09

Tim Bish


msg.setJMSPriority(9);

In this code, the message priority is set to 9, indicating this is a high-priority message. However, when the message is sent, the message will have a priority of 4 (normal priority). The reason? Like the message expiration, the JMS provider will look at the message priority property on the message and invoke the setJMSPriority method prior to placing the message on the queue. Since the default message priority is 4 (normal priority), the message priority will not be set to a high priority message, as the developer had originally intended.

Like the message expiration, there are two ways of setting the message priority: you can invoke the setPriority() method on the MessageProducer (QueueSender or Topic Publisher) or set the message priority when sending the message:

//set the default message priority for all messages to 9 (high)
QueueSender qSender = qSession.createSender(requestQ);
qSender.setPriority(9);

qSender.send(msg1);
//this message is low priority
qSender.send(msg2, DeliveryMode.PERSISTENT, 1, 30000);

In this example, msg1 will be sent with a priority of 9 (high priority), whereas msg2 will be sent with a priority of 1 (low priority).

like image 29
angadpaul Avatar answered Sep 21 '22 08:09

angadpaul