Search code examples
jms

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 with any JMS broker I've tried (e.g. HornetQ and ActiveMQ) so finally I set the priority of the MessageProducer 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?


Solution

  • 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.