我像这样创建了一个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]
- 优先顺序[4]
- 优先顺序[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
documentation表示sort_list
是[t]he columns to be used as the sort key in ascending order
,即它看起来不像降序是一种选择。
另外 - 发送时,我将优先级设置为两次 - 一次在消息上,一次在jmsTemplate
上。我真的应该这样做吗?
答案 0 :(得分:1)
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());
}
}