Quickly Exposing Spring Beans as JMX MBeans
Recently I decided to use the Spring Framework to help me add JMX support to my application. Java standards run the gamut. JSR 1 defines the real time specification for Java. JSR 170 defines a Java content repository. The Java Enterprise Edition 5 meta standard (JSR 244) itself references 40 other standards (see related documents appendix of the JSR 244 final release). Very few of us can know everything in every standard. Programming in all of these standards is nearly impossible to consider. The Spring Framework helps Java programmers simplify working with many of these Java standards. In my case, I wanted help in adding JMX support without needing to learn the entire JMX standard.
My first stop was the Spring Framework Reference Manual. I searched the table of contents for JMX. This took me to Chapter 19 for Spring 1.2 (I was not able to use Spring 2.0 for the project I was working on). The first example in section 19.2 showed defining a single POJO (plain old java object), declaring that bean in a Spring bean definition file and then defining a second JMX Wrapper bean. Here's the bean definition file example from section 19.2.
Your next step is to connect to your JMX MBean. I originally tried using the Tomcat Admininstration Web Application, but was not able to find my MBean. Instead I used jconsole. In order to use jconsole you must start Tomcat with certain Java system properties. You can follow this link for the gory details, or look at the example below for the simplified version. (If you are not using Tomcat you might need to define an additional MBeanServerFactoryBean declaration in your beans file)
CATALINA_OPTS="-Dcom.sun.management.jmxremote.port=18080 -Dcom.sun.management.jmxremote.authenticate=false"
At this point you should be able to start jconsole and connect to your application. Next, click on the mbeans table and locate your bean. You can view any attributes and run any methods on your original POJO.
Some methods or attributes may not be appropriate to expose through a JMX console. For instance if you have an attribute that is a complex type like a database connection, it will be difficult for you to set this using only characters in a text field. Spring offers a variety of ways to limit the methods and attributes you expose. I chose a simple method of listing all the methods I wanted to expose. Here's an example exposing 2 attributes in read / write mode (name, interval), 1 boolean attribute in read only mode (alive) and 2 operations (start,stop).
getName,setName,getInterval,setInterval,isAlive,start,stop
I am very happy with the result. With one additional bean definition and no coding I was able to expose an existing spring bean as a JMX MBean. Add a couple -D system properties and I was able to manipulate my application in ways that were not part of the original requirements. I believe this is a trick every Spring developer should know.
---- Cris J H
- Cris Holdorph's blog
- Login or register to post comments
