JMS message priority not working on Message

2019-04-04 08:31发布

问题:

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.

回答1:

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.



回答2:

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



回答3:

This is a JMS Specification requirement.

You should change the priority on the Message Producer.



回答4:

You can read JmsTemplate http://static.springsource.org/spring/docs/3.0.6.RELEASE/spring-framework-reference/html/jms.html

Some JMS providers allow the setting of default QOS values administratively through the configuration of the ConnectionFactory. Check isExplicitQosEnabled property.