This example shows you how to configure and use last-value queues.
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.
The example will send 3 messages with the same Last-Value property to a to a Last-Value queue.
We will browse the queue and see that only the last message is in the queue, the first two messages have been discarded.
We will then consume from the queue the last message.
Last-Value queues are defined in the configuration file activemq-configuration.xml:
<address-setting match="jms.queue.lastValueQueue">
<last-value-queue>true</last-value-queue>
</address-setting>
To run the example, simply type mvn verify -Pexample
from this directory
InitialContext initialContext = getContext();
Queue queue = (Queue) initialContext.lookup("/queue/lastValueQueue");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
STOCK_NAME
TextMessage message = session.createTextMessage("1st message with Last-Value property set");
message.setStringProperty("_AMQ_LVQ_NAME", "STOCK_NAME");
producer.send(message);
System.out.format("Sent message: %s\n", message.getText());
The Last-Value key is defined in ActiveMQ's MessageImpl class. Its value is "_AMQ_LVQ_NAME"
STOCK_NAME
message = session.createTextMessage("2nd message with Last-Value property set");
message.setStringProperty("_AMQ_LVQ_NAME", "STOCK_NAME");
producer.send(message);
System.out.format("Sent message: %s\n", message.getText());
STOCK_NAME
message = session.createTextMessage("3rd message with Last-Value property set");
message.setStringProperty("_AMQ_LVQ_NAME", "STOCK_NAME");
producer.send(message);
System.out.format("Sent message: %s\n", message.getText());
When the 2nd message was sent to the queue, the 1st message was discarded.
Similarly, when the 3rd message was sent to the queue, the 2nd message was discarded.
Only the 3rd message remains in the queue.
QueueBrowser browser = session.createBrowser(queue);
Enumeration enumeration = browser.getEnumeration();
while (enumeration.hasMoreElements())
{
TextMessage messageInTheQueue = (TextMessage)enumeration.nextElement();
System.out.format("Message in the queue: %s\n", messageInTheQueue.getText());
}
browser.close();
We will now consume the message on the queue
MessageConsumer messageConsumer = session.createConsumer(queue);
connection.start();
TextMessage messageReceived = (TextMessage)messageConsumer.receive(5000);
System.out.format("Received message: %s\n", messageReceived.getText());
receive
method will timeout after 5 seconds
messageReceived = (TextMessage)messageConsumer.receive(5000);
System.out.format("Received message: %s\n", messageReceived);
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}