Monitoring Grails Apps part 1 – Custom JMX MBeans
Join the DZone community and get the full member experience.
Join For Freethis article gives an introduction to using custom jmx mbeans within grails.
jmx (java management extension) was an early jsr specification ( jsr-3 ) and the first final release was in 2000 with subsequent maintenance releases in 2002 & 2006. additional capabilities such as remoting support ( jmx-rmi ) were added through subsequent jsrs.
the jmx spec in essence allows you to check values of attributes on
an mbean (management bean), set attribute values, invoke methods (e.g.
shutdown) and it also caters for notifications. for this article we’ll
focus on the first of those.
in my experience not many java application developers are familiar with jmx though i’ve seen (and encouraged) more usage of custom mbeans on spring powered applications. the spring framework makes it exceptionally easy to export a bean as an mbean and to register it with an mbeanserver.
a custom mbean
let’s start with a ‘hello world’ example…
/**
* simple jmx mbean example.
*
* @author robin bramley
*/
public class simplembean {
public string gethelloworld() {
return 'hello world'
}
}
i’ve created this in src/groovy/com/rbramley/jmx/simplembean.groovy (so obviously it has “package com.rbramley.jmx” at the top).
spring config
open up
grails-app/conf/spring/resources.groovy
firstly, we need to declare our bean:
simplembean(com.rbramley.jmx.simplembean) {}
having set up a bean, we need an mbean server so that we can register our mbean. luckily the spring framework helps us out here. the mbeanserverfactorybean will attempt to locate an existing mbean server (this behaviour can be turned off).
mbeanserver(org.springframework.jmx.support.mbeanserverfactorybean) {
locateexistingserverifpossible = true
}
spring provides a number of ways of exposing beans as mbeans, the simplest is the mbeanexporter (for other exporters & assemblers see the spring framework jmx docs ) – this will expose our bean to the mbeanserver:
mbeanexporter(org.springframework.jmx.export.mbeanexporter) {
server = ref('mbeanserver')
registrationbehaviorname = 'registration_replace_existing'
beans = ['rbramley:name=simplembean':ref('simplembean')]
}
accessing jmx
now we’ve exposed some information via jmx, how can we access it?
first up is jconsole , this tool has been shipped with the sun jdk for a while now. it can access local apps by process id and remote applications by url.
here i’ve accessed the grails local process, selected the ‘mbeans’ tab and navigated to the ‘helloworld’ attribute. notice how the mbean is in the ‘rbramley’ namespace – this was achieved with the map key in the ‘beans’ property of the mbeanexporter bean.
if you’re using tomcat, the manager application provides a jmx proxy servlet – see the manager docs for more details.
we can also access jmx from nagios-based monitoring systems such as opsview using check_jmx – but more on that later on in the series when we pull it all together.
from http://leanjavaengineering.wordpress.com/2011/03/29/monitoring-grails-apps-part-1-custom-jmx-mbeans/
Opinions expressed by DZone contributors are their own.
Comments