Nov 09

5 Minute Guide to Spring and JMX

Tag: java, jmx, springpmularien @ 10:30 am

I recently augmented a Spring-based project to expose some of the Spring-managed beans via JMX. Spring makes this very easy, and even if you’ve never used JMX before, this quick tutorial will let you set up your Spring beans to be viewed (and edited!) through a JMX console.

Prerequisites

Make sure you have a recent version of Spring (I used Spring 2.5 RC 1, but Spring 2.0.x will do). Make sure you have a recent version of Java, ideally Java 6. You should try to have an existing Spring application that you can follow along with, deployed in an app server that has built-in JMX support (i.e. has an MBean server that you can reuse). I use Tomcat 6 in this walk-through.

Cook-Along

Add the following to your Spring XML configuration file:

<bean id="exporter"
 class="org.springframework.jmx.export.MBeanExporter"
 lazy-init="false">
  <property name="autodetect" value="true"></property>
  <property name="namingStrategy" ref="namingStrategy"></property>
  <property name="assembler" ref="assembler"></property>
</bean>
<bean id="attributeSource"
 class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>
<bean id="assembler"
 class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
  <property name="attributeSource" ref="attributeSource"/>
</bean>
<bean id="namingStrategy"
 class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
  <property name="attributeSource" ref="attributeSource"/>
</bean>

This will set up a Spring-managed JMX configuration with the following attributes:

Using an existing MBean server Auto-detecting beans with the @ManagedResource annotation MBean naming defined by the “objectName” attribute of the @ManagedResource annotation

For a Java 5 deployment of Spring, these seem reasonable, and I’m surprised that they aren’t the default configuration for Spring 2.5.

Next, we’ll need to annotate our spring-managed bean with the @ManagedResource annotation. The simplest possible usage is as follows:

@ManagedResource(objectName = “bean:name=simpleBean”, description = “A sample JMX-managed bean”)

This annotation is done at the class level - Spring will auto-detect beans with this annotation and register them as MBeans with the MBean server. The next (and final) step in the tutorial is to select which attributes of our MBean that we’ll expose. We do this by annotating members (or getters and setters) with the @ManagedAttribute annotation. So our final Java code for a simple bean looks like this:

package com.mularien.jmx;
 
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedResource;
 
@ManagedResource(objectName = "spring:name=simpleBean", description = "A
sample JMX-managed bean")
public class SimpleBean {
 public String url;
 
 @ManagedAttribute
 public String getUrl() {
  return url;
 }
 public void setUrl(String url) {
  this.url = url;
 }
}

And now you’re done! Fire up jconsole and connect to the Tomcat server. You’ll see a JMX namespace called “spring” with the simpleBean MBean in it.

Hopefully this was a helpful introduction on how to use JMX with your Spring beans. Spring provides a whole lot of flexibility in this area, but I wanted to write this simple tutorial, as I didn’t see one that I liked already online. Enjoy (and please comment if you found this helpful/not helpful)!

Further Reading

Rob Harrop covers Spring and JMX integration well with a series of slides in his blog (warning: many spammy comments!).

The Spring documentation is a great resource (as always) with its extensive coverage of JMX and Spring.

Sun provides Essentials of the JMX API, which is a good, brief overview of JMX and MBeans.

2 Responses to “5 Minute Guide to Spring and JMX”

napyfab:blog» Blog Archive » links for 2007-11-15 says:

[…] It’s Only Software » 5 Minute Guide to Spring and JMX (tags: spring jmx java bean development howto integration introduction programming guide tutorial) […]

Vinny Carpenter’s blog » Daily del.icio.us for Nov 02 through Nov 23, 2007 says:

[…] It’s Only Software » 5 Minute Guide to Spring and JMX - I recently augmented a Spring-based project to expose some of the Spring-managed beans via JMX. Spring makes this very easy, and even if you?ve never used JMX before, this quick tutorial will let you set up your Spring beans to be viewed (and edited!) t […]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>




You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser