最新值队列是一种特殊的队列。当一个新消息到达一个最新值队列时,它会将所有与该消息定义的Last-Value相同的旧消息 抛弃。换句话说,只有最新的消息被保留下来。
一个典型的用例是股价信息,通常你只关心一支股票的最新价格。
最新值队列的配置在address-setting内:
<address-setting match="jms.queue.lastValueQueue"> <last-value-queue>true</last-value-queue> </address-setting>
默认的last-value-queue值是false。可以使用通配符来匹配地址。 (参见 Chapter 13, 了解 HornetQ 通配符的语法)。
用来标识最新值的参数名是"_HQ_LVQ_NAME" (相当于核心API中定义的常量Message.HDR_LAST_VALUE_NAME)。
如果两个消息具有相同的Last-Value值,那么较新的消息就会保留,另外一个被丢弃:
// send 1st message with Last-Value property set to STOCK_NAME TextMessage message = session.createTextMessage("1st message with Last-Value property set"); message.setStringProperty("_HQ_LVQ_NAME", "STOCK_NAME"); producer.send(message); // send 2nd message with Last-Value property set to STOCK_NAME message = session.createTextMessage("2nd message with Last-Value property set"); message.setStringProperty("_HQ_LVQ_NAME", "STOCK_NAME"); producer.send(message); ... // only the 2nd message will be received: it is the latest with // the Last-Value property set TextMessage messageReceived = (TextMessage)messageConsumer.receive(5000); System.out.format("Received message: %s\n", messageReceived.getText());
参见Section 11.1.23, “最新值队列”。它展示的是在JMS应用中来配置和使用 最新值队列。