Monitoring Grails Apps part 1 – Custom JMX MBeans

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.
Update: To enable remote JMX connections to Tomcat see this post.

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.

About these ads

3 responses to “Monitoring Grails Apps part 1 – Custom JMX MBeans

  1. Pingback: Monitoring Grails Apps part 1 – Custom JMX MBeans

  2. Pingback: Monitoring Grails Apps part 2 – data collection | Lean Java Engineering

  3. If you are looking for a hosted service that can provide visualization and alerting using JMX you can try metrics.librato.com.

    There’s a JMX Beans tap script here:
    https://github.com/librato/librato-metrics-taps
    ..that will connect to a JMX service and pull monitoring attributes from a configured set of MBeans. The values of these MBean attributes are pumped into Librato’s Metric Service.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s