Monitoring Grails Apps part 1 – Custom JMX MBeans

DZone 's Guide to

Monitoring Grails Apps part 1 – Custom JMX MBeans

· Java Zone ·
Free Resource

This 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.

Custom MBean viewed through jconsole

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.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}