Robin Bramley is a hands-on Architect who has spent the last decade working with Java, mobile & Open Source across sectors including Financial Services & High Growth / start-ups. Prior to that he helped UK Police Forces with MIS /reporting & intelligence systems. He has contributed to a wide variety of Open Source projects including adding Open ID support to Spring Security. Robin is a DZone MVB and is not an employee of DZone and has posted 24 posts at DZone. You can read more from them at their website. View Full User Profile

Monitoring Grails Apps part 1 – Custom JMX MBeans

06.01.2011
| 4517 views |
  • submit to reddit

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/

Published at DZone with permission of Robin Bramley, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Bernardo Gomez ... replied on Thu, 2011/06/09 - 9:12pm

You can add components as MBeans through the

import org.springframework.jmx.export.annotation.ManagedResource //annotation too
//e.g.
@ManagedResource(objectName = "bean:name=theNameOFtheBean,type=special",
   currencyTimeLimit = 15, persistPolicy = "OnUpdate", persistPeriod = 60)
class MySprinbBeanClass {
}
with conf. in resources.groovy
beans {
    annotationJmxAttributeSource(AnnotationJmxAttributeSource)

    jmxAnnotationsExporter(AnnotationMBeanExporter) {
        server = { org.springframework.jmx.support.MBeanServerFactoryBean sfb ->
            locateExistingServerIfPossible = true
        }
        autodetect = true
        autodetectMode = MBeanExporter.AUTODETECT_ASSEMBLER
        registrationBehavior = MBeanRegistrationSupport.REGISTRATION_IGNORE_EXISTING
        assembler = { MetadataMBeanInfoAssembler mbeanIA ->
            attributeSource = ref(annotationJmxAttributeSource)
        }
        namingStrategy = { MetadataNamingStrategy ns ->
            attributeSource = ref(annotationJmxAttributeSource)
        }
    }
}

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.