第 13 章 计划消息
您可以尽早为发送消息指定时间。这可以通过在发送消息前设置 _AMQ_SCHED_DELIVERY 调度发送属性来完成。
指定的值必须是正 长,对应于消息被发送的时间(毫秒)。以下是使用 JMS API 发送调度消息的示例:
// Create a message to be delivered in 5 seconds
TextMessage message = session.createTextMessage("This is a scheduled message message that will be delivered in 5 sec.");
message.setLongProperty("_AMQ_SCHED_DELIVERY", System.currentTimeMillis() + 5000);
producer.send(message);
...
// The message will not be received immediately, but 5 seconds later
TextMessage messageReceived = (TextMessage) consumer.receive();
也可以使用核心 API 发送计划消息,在发送消息前设置 _AMQ_SCHED_DELIVERY 属性。