28.6. Message Counters

Message counters can be used to obtain information on queues over time as HornetQ keeps a history of queue metrics.
They can be used to show trends on queues. For example, using the management API, it would be possible to query the number of messages in a queue at regular intervals. You could also view this information using the JMX Console, or use the core API (org.hornetq.api.core.management.MessageCounterInfo) to extract the information.
However, this would not be enough to know if the queue is used. The number of messages can remain constant because nobody is sending or receiving messages from the queue or because there are as many messages sent to the queue as messages consumed from it. The number of messages in the queue remains the same in both cases but its use is different.
Message counters provide additional information about the queues:
count
The total number of messages added to the queue since the server was started.
countDelta
The number of messages added to the queue since the last message counter update.
depth
The current number of messages in the queue.
depthDelta
The overall number of messages added or removed from the queue since the last message counter update. For example, if depthDelta is equal to -10 this means that overall 10 messages have been removed from the queue.
lastAddTimestamp
The time stamp of the last time a message was added to the queue.
udpateTimestamp
The time stamp of the last message counter update.

28.6.1. Configuring Message Counters

Message counters are disabled by default as they could have a negative effect on memory.
To enable message counters, you can set it to true in <JBOSS_HOME>/jboss-as/server/<PROFILE>/deploy/hornetq/hornetq-configuration.xml:
<message-counter-enabled>true</message-counter-enabled>
Message counters keep a history of the queue metrics (10 days by default) and sample all the queues at regular intervals (10 seconds by default). If message counters are enabled, these values should be configured to suit your messaging use case in <JBOSS_HOME>/jboss-as/server/<PROFILE>/deploy/hornetq/hornetq-configuration.xml:
<!-- keep history for a week -->
<message-counter-max-day-history>7</message-counter-max-day-history>        
<!-- sample the queues every minute (60000ms) -->
<message-counter-sample-period>60000</message-counter-sample-period>
Message counters can be retrieved using the Management API. For example, to retrieve message counters on a JMS Queue using JMX:
// retrieve a connection to HornetQ's MBeanServer
MBeanServerConnection mbsc = ...
JMSQueueControlMBean queueControl = (JMSQueueControl)MBeanServerInvocationHandler.newProxyInstance(mbsc,
   on,
   JMSQueueControl.class,
   false);
// message counters are retrieved as a JSON String                                                                                                      
String counters = queueControl.listMessageCounter();
// use the MessageCounterInfo helper class to manipulate message counters more easily
MessageCounterInfo messageCounter = MessageCounterInfo.fromJSON(counters);         
System.out.format("%s message(s) in the queue (since last sample: %s)\n",
   counter.getDepth(),
   counter.getDepthDelta());