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.
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.
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With