ActiveMQ supports Broker plugins, which allows the default functionality to be extended, and new with version 5.3 of Apache ActiveMQ is a Statistics plugin, which enables statistics about the running broker, or Queues and Topics to be queried. To configure ActiveMQ to use the statistics plugin - just add the following to the ActiveMQ XML configuration:
...
<plugins>
<statisticsBrokerPlugin/>
</plugins>
The statistics plugin looks for messages sent to particular destinations. To query the running statistics of a the message broker, send an empty message to a Destination (Queue or Topic) named ActiveMQ.Statistics.Broker, and set the replyTo (jmsReplyTo if using JMS) field with the Destination you want to receive the result on. The statistics plugin will send a MapMessage filled with the statistics for the running ActiveMQ broker - e.g.
Queue replyTo = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(replyTo);
Queue query = session.createQueue("ActiveMQ.Statistics.Broker");
MessageProducer producer = session.createProducer(query);
Message msg = session.createMessage();
msg.setJMSReplyTo(replyTo);
producer.send(msg);
MapMessage reply = (MapMessage) consumer.receive();
for (Enumeration e = reply.getMapNames();e.hasMoreElements();) {
String name = e.nextElement().toString();
System.err.println(name+"="+reply.getObject(name));
}
You should see output like this:
vm=vm://localhost
memoryUsage=0
storeUsage=3330
tempPercentUsage=0
ssl=
openwire=tcp://localhost:50059
brokerId=ID:bigmac-50057-1253605065511-0:0
consumerCount=2
brokerName=localhost
expiredCount=0
dispatchCount=1
maxEnqueueTime=5.0
storePercentUsage=0
dequeueCount=0
inflightCount=1
messagesCached=0
tempLimit=107374182400
averageEnqueueTime=5.0
stomp+ssl=
memoryPercentUsage=0
size=10
tempUsage=0
producerCount=1
minEnqueueTime=5.0
dataDirectory=/Users/rajdavies/dev/projects/activemq/activemq-core/activemq-data
enqueueCount=10
stomp=
storeLimit=107374182400
memoryLimit=67108864
memoryUsage=0
storeUsage=3330
tempPercentUsage=0
ssl=
openwire=tcp://localhost:50059
brokerId=ID:bigmac-50057-1253605065511-0:0
consumerCount=2
brokerName=localhost
expiredCount=0
dispatchCount=1
maxEnqueueTime=5.0
storePercentUsage=0
dequeueCount=0
inflightCount=1
messagesCached=0
tempLimit=107374182400
averageEnqueueTime=5.0
stomp+ssl=
memoryPercentUsage=0
size=10
tempUsage=0
producerCount=1
minEnqueueTime=5.0
dataDirectory=/Users/rajdavies/dev/projects/activemq/activemq-core/activemq-data
enqueueCount=10
stomp=
storeLimit=107374182400
memoryLimit=67108864
Similarly, if you want to query the statistics on a Destination, send a message to the Destination name, prepended with ActiveMQ.Statistics.Destination. For example, to retrieve the statistics on a Queue named test.foo send an empty message to the Queue ActiveMQ.Statistics.DestinationTest.Foo.
e.g.
Queue replyTo = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(replyTo);
Queue testQueue = session.createQueue("Test.Queue");
MessageProducer producer = session.createProducer(null);
Queue query = session.createQueue("ActiveMQ.Statistics.Destination" + testQueue.getQueueName());
MessageConsumer consumer = session.createConsumer(replyTo);
Queue testQueue = session.createQueue("Test.Queue");
MessageProducer producer = session.createProducer(null);
Queue query = session.createQueue("ActiveMQ.Statistics.Destination" + testQueue.getQueueName());
Message msg = session.createMessage();
producer.send(testQueue,msg)
msg.setJMSReplyTo(replyTo);
producer.send(query,msg);
MapMessage reply = (MapMessage) consumer.receive();
assertNotNull(reply);
assertTrue(reply.getMapNames().hasMoreElements());
for (Enumeration e = reply.getMapNames();e.hasMoreElements();) {
String name = e.nextElement().toString();
System.err.println(name+"="+reply.getObject(name));
}
You should see:
memoryUsage=0
dequeueCount=0
inflightCount=0
messagesCached=0
averageEnqueueTime=0.0
destinationName=queue://Test.Queue
size=1
memoryPercentUsage=0
producerCount=0
consumerCount=0
minEnqueueTime=0.0
maxEnqueueTime=0.0
dispatchCount=0
expiredCount=0
enqueueCount=1
memoryLimit=67108864
dequeueCount=0
inflightCount=0
messagesCached=0
averageEnqueueTime=0.0
destinationName=queue://Test.Queue
size=1
memoryPercentUsage=0
producerCount=0
consumerCount=0
minEnqueueTime=0.0
maxEnqueueTime=0.0
dispatchCount=0
expiredCount=0
enqueueCount=1
memoryLimit=67108864
You can also use wildcards too, and receive a separate message for every destination matched.