Tuesday, 13 September 2011

Monitor activemq queue-size with jruby jmx

The aim is to know when a certain threshold of queued messages is reached on certain destinations. For example, if new messages show up in DLQ (dead letter queue) it might be a hint of some problems.

First of all enable jmx for activemq on a free port (11099, for example). For activemq-5.5.0 modify /etc/default/activemq as follows:
ACTIVEMQ_SUNJMX_START="-Dcom.sun.management.jmxremote.port=11099 
ACTIVEMQ_SUNJMX_START="$ACTIVEMQ_SUNJMX_START -Dcom.sun.management.jmxremote.authenticate=false"
ACTIVEMQ_SUNJMX_START="$ACTIVEMQ_SUNJMX_START -Dcom.sun.management.jmxremote.ssl=false"
ACTIVEMQ_SUNJMX_START="$ACTIVEMQ_SUNJMX_START -Dcom.sun.management.jmxremote"

Restart activemq and confirm something is listening on port 11099:
$ sudo lsof -i :11099
COMMAND   PID     USER   FD   TYPE    DEVICE SIZE NODE NAME
java    23206 activemq   10u  IPv6 110495619       TCP *:11099 (LISTEN)

Install jmx gem:
sudo jruby -S gem install jmx

Then a simple script like this can query the size of the queue ActiveMQ.DLQ for further actions:
require 'java'
require 'rubygems'
require 'jmx'

client = JMX.connect(:port => 11099)

queue = client["org.apache.activemq:BrokerName=bacedifo,Type=Queue,Destination=ActiveMQ.DLQ"]
#puts queue.attributes

puts queue["QueueSize"]

No comments: