JMS消息优先级不在Message上工作

Sma*_*ion 15 activemq-classic hornetq

我需要设置消息优先级,以便接收方在低优先级消息之前消耗高优先级消息.

首先,我尝试使用message.setJMSPriority()方法来设置优先级,但它在HornetQ和ActiveMQ中不起作用,所以最后我使用setPriority()方法设置Message Producer的优先级,现在它工作正常.

为什么Messsge.setJMSPriority()不能在任何JMS供应商实现中工作?为什么我们需要设置Producer的优先级而不是消息本身来设置消息的优先级?那么Messsge.setJMSPriority()方法的用途是什么?

任何建议或评论表示赞赏.

Tim*_*ish 18

要回答这个问题,您需要做的就是阅读setJMSPriority方法的API文档,它会告诉您原因.这是相关的文字.

设置此消息的优先级.

发送消息时,JMS提供程序会设置此字段.此方法可用于更改已接收消息的值.

JMS提供程序(ActiveMQ,HornetMQ等)将生成器中的优先级设置为发送到默认值4,或者设置为生成器要使用的任何值,因此在发送消息之前设置值将赢得没有任何影响.


小智 8

msg.setJMSPriority(9);
Run Code Online (Sandbox Code Playgroud)

在此代码中,消息优先级设置为9,表示这是一个高优先级消息.但是,当发送消息时,消息的优先级将为4(正常优先级).原因?与消息过期一样,JMS提供程序将查看消息上的消息优先级属性,并在将消息放入队列之前调用setJMSPriority方法.由于默认消息优先级为4(普通优先级),因此消息优先级不会像开发人员最初预期的那样设置为高优先级消息.

像邮件过期,也有设置消息优先级的方法有两种:您可以调用的MessageProducer上(QueueSender或主题发布者)的setpriority()可方法或发送消息时设置消息优先级:

//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);
Run Code Online (Sandbox Code Playgroud)

在此示例中,msg1将以优先级9(高优先级)发送,而msg2将以优先级1(低优先级)发送.


Cle*_*nic 5

这是JMS规范要求.

您应该更改Message Producer的优先级.