I want to subtract the last message from an ActiveMQ Artemis queue. I have a lot of messages in the queue and when I read one message, the rest just disappear, but the message counter in the broker console shows 1 less. How can I make it show one less message when refreshing the page (this works, but when I break the consumer session)
This is what happens after reading one message:
private void getMessage() throws JMSException, NamingException {
Queue queue = (Queue) initialContext.lookup("dynamicQueues/" + "TestQueue");
if (messageConsumer == null){
messageConsumer = queueSession.createConsumer(queue);
}
TextMessage message = (TextMessage) messageConsumer.receive();
String msgBody = ((TextMessage) message).getText();
System.out.println(msgBody);
}
When a core JMS consumer starts receiving messages the broker dispatches more than just one message to it. This is a performance optimization so that the client doesn't have to make repeated network round-trips to the broker to receive additional messages. This behavior can be tuned using the consumerWindowSize
URL parameter. See the documentation for more details on that.
In your case, even though the client has only processed a single message, all the other 6 messages in the queue have been dispatched to it. Messages in this dispatched-but-not-yet-acknowledged state are referred to as "in delivery" and can be tracked via the "Delivering Count" metric on the queue. Messages which are "in delivery" are still technically in the queue but they are not available to other consumers or queue browsers. Therefore, they do not show up on the web console although you can still list them via the listDeliveringMessages
or listDeliveringMessagesAsJSON
JMX operations on the QueueControl MBean. Once a message is acknowledged it is completely and permanently removed from the queue. If the consumer is closed before it acknowledges all of its "in delivery" messages then those messages will be put back onto the queue and made available to other consumers and queue browsers again.
If you want to disable this behavior so that the consumer only gets one message per network round-trip then you can set consumerWindowSize=0
on your client's connection URL. This will allow all the other messages in the queue to remain available to other consumers and queue browsers. However, keep in mind that this will have an adverse impact on the consumer's performance.