2014-12-11 07:17:29 -05:00
|
|
|
# Last-Value Queues
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
Last-Value queues are special queues which discard any messages when a
|
|
|
|
newer message with the same value for a well-defined Last-Value property
|
|
|
|
is put in the queue. In other words, a Last-Value queue only retains the
|
|
|
|
last value.
|
|
|
|
|
|
|
|
A typical example for Last-Value queue is for stock prices, where you
|
|
|
|
are only interested by the latest value for a particular stock.
|
|
|
|
|
2018-03-08 15:46:38 -05:00
|
|
|
## Configuration
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2018-03-08 15:46:38 -05:00
|
|
|
Last-Value queues can be statically configured via the `last-value`
|
|
|
|
boolean property:
|
2018-02-20 03:39:37 -05:00
|
|
|
|
|
|
|
```xml
|
2018-03-09 10:07:38 -05:00
|
|
|
<address name="foo.bar">
|
|
|
|
<multicast>
|
|
|
|
<queue name="orders1" last-value="true"/>
|
|
|
|
</multicast>
|
|
|
|
</address>
|
2018-02-20 03:39:37 -05:00
|
|
|
```
|
|
|
|
|
2018-03-09 10:07:38 -05:00
|
|
|
Specified on creating a queue by using the CORE api specifying the parameter
|
|
|
|
`lastValue` to `true`.
|
2018-02-20 03:39:37 -05:00
|
|
|
|
2018-03-09 10:07:38 -05:00
|
|
|
Or on auto-create when using the JMS Client by using address parameters when
|
|
|
|
creating the destination used by the consumer.
|
2018-02-20 03:39:37 -05:00
|
|
|
|
2018-03-09 10:07:38 -05:00
|
|
|
```java
|
|
|
|
Queue queue = session.createQueue("my.destination.name?last-value=true");
|
|
|
|
Topic topic = session.createTopic("my.destination.name?last-value=true");
|
|
|
|
```
|
2018-02-20 03:39:37 -05:00
|
|
|
|
2018-03-09 10:07:38 -05:00
|
|
|
Also the default for all queues under and address can be defaulted using the
|
|
|
|
`address-setting` configuration:
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2018-03-08 15:46:38 -05:00
|
|
|
```xml
|
|
|
|
<address-setting match="lastValueQueue">
|
|
|
|
<default-last-value-queue>true</default-last-value-queue>
|
|
|
|
</address-setting>
|
|
|
|
```
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2018-02-20 03:39:37 -05:00
|
|
|
By default, `default-last-value-queue` is false.
|
|
|
|
Address wildcards can be used to configure Last-Value queues
|
|
|
|
for a set of addresses (see [here](wildcard-syntax.md)).
|
|
|
|
|
2018-03-09 10:07:38 -05:00
|
|
|
Note that `address-setting` `last-value-queue` config is deprecated, please use
|
|
|
|
`default-last-value-queue` instead.
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2018-03-08 15:46:38 -05:00
|
|
|
## Last-Value Property
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2015-04-14 12:07:26 -04:00
|
|
|
The property name used to identify the last value is `"_AMQ_LVQ_NAME"`
|
2014-12-04 10:25:29 -05:00
|
|
|
(or the constant `Message.HDR_LAST_VALUE_NAME` from the Core API).
|
|
|
|
|
|
|
|
For example, if two messages with the same value for the Last-Value
|
|
|
|
property are sent to a Last-Value queue, only the latest message will be
|
|
|
|
kept in the queue:
|
|
|
|
|
2018-03-08 15:46:38 -05:00
|
|
|
```java
|
2014-12-11 07:17:29 -05:00
|
|
|
// send 1st message with Last-Value property set to STOCK_NAME
|
|
|
|
TextMessage message = session.createTextMessage("1st message with Last-Value property set");
|
2015-04-14 12:07:26 -04:00
|
|
|
message.setStringProperty("_AMQ_LVQ_NAME", "STOCK_NAME");
|
2014-12-11 07:17:29 -05:00
|
|
|
producer.send(message);
|
|
|
|
|
2015-01-23 09:28:07 -05:00
|
|
|
// send 2nd message with Last-Value property set to STOCK_NAME
|
2014-12-11 07:17:29 -05:00
|
|
|
message = session.createTextMessage("2nd message with Last-Value property set");
|
2015-04-14 12:07:26 -04:00
|
|
|
message.setStringProperty("_AMQ_LVQ_NAME", "STOCK_NAME");
|
2014-12-11 07:17:29 -05:00
|
|
|
producer.send(message);
|
2015-01-23 09:28:07 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
...
|
2015-01-23 09:28:07 -05:00
|
|
|
|
|
|
|
// only the 2nd message will be received: it is the latest with
|
2014-12-11 07:17:29 -05:00
|
|
|
// the Last-Value property set
|
|
|
|
TextMessage messageReceived = (TextMessage)messageConsumer.receive(5000);
|
|
|
|
System.out.format("Received message: %s\n", messageReceived.getText());
|
|
|
|
```
|
|
|
|
|
|
|
|
## Example
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2018-03-09 10:07:38 -05:00
|
|
|
See the [last-value queue example](examples.md#last-value-queue) which shows
|
|
|
|
how last value queues are configured and used with JMS.
|