Oracle AQ Prioritized JMS 消息以相反的优先顺序传递

Oracle AQ Prioritised JMS messages delivered in reverse priority order

我创建了一个像这样的 oracle 队列

execute dbms_aqadm.create_queue_table (
                        queue_table => 'priorityqueue',
                        sort_list => 'PRIORITY,ENQ_TIME',
                        queue_payload_type => 'sys.aq$_jms_map_message',
                        compatible => '8.1.0');
execute dbms_aqadm.create_queue (
                        queue_name => 'priorityqueue',
                        queue_table => 'priorityqueue');
execute dbms_aqadm.start_queue (
                        queue_name => 'priorityqueue');

然后我使用 spring JMSTemplate 发送消息,如下所示:

ConnectionFactory connectionFactory = AQjmsFactory.getQueueConnectionFactory(dataSource);
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
jmsTemplate.setExplicitQosEnabled(true);
MessageCreator messageCreator = new MessageCreator() {
        @Override
        public Message createMessage(Session session) throws JMSException {
            MapMessage message = session.createMapMessage();
            // ... set some properties
            message.setJMSPriority(priority);
            return message;
        }
};
jmsTemplate.setPriority(priority);
jmsTemplate.send("priorityqueue", messageCreator);

如果我 select 来自 priorityqueue 的所有内容,我可以看到消息以不同的优先级放在那里:

select q_name, msgid, priority, state from priorityqueue;
Q_NAME                         MSGID                              PRIORITY      STATE
PRIORITYQUEUE                  1297D3F58D41208FE05400215A97112C          1          0 
PRIORITYQUEUE                  1297D3F58D43208FE05400215A97112C          9          0 
PRIORITYQUEUE                  1297D3F58D45208FE05400215A97112C          4          0 

接下来我使用这些消息......没有什么花哨的 - 像这样:

ConnectionFactory connectionFactory = AQjmsFactory.getQueueConnectionFactory(oracleDataSource);

Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue loggerQueue = session.createQueue("priorityqueue");
MessageConsumer consumer = session.createConsumer(loggerQueue);

int count = 0;
while (true) {
    connection.start();
    Message message = consumer.receive(1000);
    if (message == null) {
        break;
    }
    count++;
    System.out.println(count + ".\tPriority [" + message.getJMSPriority() + "]");
}
connection.close();

但是输出看起来像这样:

  1. Priority [1]
  2. Priority [4]
  3. Priority [9]

什么时候真的应该是 9、4、1。

documentation for java.jms.Message 明确指出 [t]he JMS API defines ten levels of priority value, with 0 as the lowest priority and 9 as the highest.

我做错了什么?我研究了按优先级降序创建队列,但 the CREATE_QUEUE_TABLE documentationsort_list[t]he columns to be used as the sort key in ascending order 即它看起来不像降序是一个选项。

此外 - 发送时,我设置了两次优先级 - 一次在消息上,一次在 jmsTemplate 上。我真的必须这样做吗?

Spring 的 JmsTemplate 不支持每条消息的优先级。您需要按照本文所述对 JmsTemplate 进行子类化:https://aminsblog.wordpress.com/2010/04/27/publishing-qos-priority-enabled-messages-using-spring-jms-template-2/

public class QoSEnabledJmsTemplate extends JmsTemplate {
    protected void doSend(MessageProducer producer, Message message) throws JMSException {
       producer.send(message, getDeliveryMode(), message.getJMSPriority(), getTimeToLive());
    }
}