[译]JMS 消息优先级

我们将通过示例详细讨论 JMS 消息优先级。每条消息的标头字段代表其优先级。我在消息模型教程JMSPriority中讨论过消息优先级。

JMS 有 10 个优先级,从 0 到 9。0 是最低优先级,9 是最高优先级。根据 JMS 标准,优先级为 0-4 的消息为正常优先级,优先级为 5-9 的消息被视为加急优先级。

JMS 优先级示例

在下面的例子中,具有不同优先级的消息被发送给接收方。但是,优先级最高的消息(9)最先到达,同样,优先级最低的消息最后到达。

package lab01.message.model;

import labxx.common.settings.CommonSettings;
import javax.jms.*;

public class MessagePriorityTest {
  public static void main(String[] args) throws JMSException {

    ConnectionFactory connectionFactory = CommonSettings.getConnectionFactory();
    Queue queue = CommonSettings.getDefaultQueue();

    try (JMSContext jmsContext = connectionFactory.createContext()) {
      JMSProducer producer = jmsContext.createProducer();
      String[] messages = {"Msg One", "Msg two", "Msg three", "Msg four", "Msg five"};
  
      producer.setPriority(0).send(queue, messages[0]);
      producer.setPriority(9).send(queue, messages[1]);
      producer.setPriority(4).send(queue, messages[2]);
      producer.setPriority(2).send(queue, messages[3]);
      producer.send(queue, messages[4]);
  
      JMSConsumer consumer = jmsContext.createConsumer(queue);
      for (int i = 0; i < messages.length; i++) {
        Message message = consumer.receive();
        System.out.println("Priority: " + message.getJMSPriority() + " #### Message: " + message.getBody(String.class));
      }
    }
  }
}

输出

Priority: 9 #### Message: Msg two
Priority: 4 #### Message: Msg three
Priority: 2 #### Message: Msg four
Priority: 2 #### Message: Msg five
Priority: 0 #### Message: Msg One

JMS 消息默认优先级

即使您不指定消息优先级,JMS 也会为每条消息分配一个默认优先级。查看下面的代码示例,您可以在输出中看到每条消息的优先级为 4。

package lab01.message.model;

import labxx.common.settings.CommonSettings;
import javax.jms.*;

public class MessageDefaultPriorityTest {
  public static void main(String[] args) throws JMSException {

    ConnectionFactory connectionFactory = CommonSettings.getConnectionFactory();
    Queue queue = CommonSettings.getDefaultQueue();

    try (JMSContext jmsContext = connectionFactory.createContext()) {
      JMSProducer producer = jmsContext.createProducer();
      String[] messages = {"Msg One", "Msg two", "Msg three", "Msg four", "Msg five"};

      producer.send(queue, messages[0]);
      producer.send(queue, messages[1]);
      producer.send(queue, messages[2]);
      producer.send(queue, messages[3]);
      producer.send(queue, messages[4]);

      JMSConsumer consumer = jmsContext.createConsumer(queue);
      for (int i = 0; i < messages.length; i++) {
        Message message = consumer.receive();
        System.out.println("Default Priority: " + message.getJMSPriority() + " #### Message: " + message.getBody(String.class));
      }
    }
  }
}

输出

Default Priority: 4 #### Message: Msg One
Default Priority: 4 #### Message: Msg two
Default Priority: 4 #### Message: Msg three
Default Priority: 4 #### Message: Msg four
Default Priority: 4 #### Message: Msg five

这些都是 JMS 优先级消息的一部分,您已经了解了如何分配自定义优先级来控制消息传递。

原文链接:JMS Prioritize Messages

Share this post:

Related content