Sunday, April 17, 2011

Java Management Extensions (JMX) and Spring

The Java Management Extensions (JMX) API is a standard for managing and monitoring applications and services. We will skip all the theory, and go right into developing a JMX bean that is partly exposed to JMX. Using JMX tools we can manage and monitor the bean.

This blog post shows how to create a bean, which has a normal interface and a custom restrictive interface for JMX.

The Java interface below defines a class with three methods. The first two methods are setters and getters of an attribute.


package com.javaeenotes;

public interface Example {
public String getAttribute();

public void setAttribute(String s);

public void hiddenOperation();
}


Take this interface and develop a class with some extra methods.


package com.javaeenotes;

public class ExampleImpl implements ExampleMBean, Example {
private String attribute = null;
private int attribute1 = 0;
private String attribute2 = null;

// Not exposed to JMX.
public String getAttribute() {
return attribute;
}

// Not exposed to JMX.
public void setAttribute(String s) {
attribute = s;
}

// Exposed to JMX.
public int getExampleAttribute1() {
return attribute1;
}

// Not exposed to JMX.
public void setExampleAttribute1(int i) {
attribute1 = i;
}

// Exposed to JMX.
public String getExampleAttribute2() {
return attribute2;
}

// Exposed to JMX.
public void setExampleAttribute2(String s) {
attribute2 = s;
}

// Not exposed to JMX.
public void hiddenOperation() {
;
}

// Exposed to JMX.
public void operation() {
;
}
}


Some of them are meant to be exposed to JMX, which means they can be monitored and manipulated. Now, define an interface to be used by JMX. The interface must follow the MBean conventions. This means we use getters and setters for attributes we want to expose. We leave out setters if we want to make the attribute read only. We also define operations for JMX.


package com.javaeenotes;

public interface ExampleMBean {
public int getExampleAttribute1();

public String getExampleAttribute2();

public void setExampleAttribute2(String s);

public void operation();
}


Lastly, we configure all of this in a Spring beans XML file. We use the class MBeanExporter to expose our bean to JMX. We also tell it to use the restrictive MBean interface for our bean.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="com.javaeenotes:name=exampleBean" value-ref="exampleBean" />
</map>
</property>
<property name="assembler">
<bean
class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler">
<property name="managedInterfaces">
<value>com.javaeenotes.ExampleMBean</value>
</property>
</bean>
</property>
<property name="autodetectModeName" value="AUTODETECT_MBEAN" />
</bean>

<bean id="exampleBean" class="com.javaeenotes.ExampleImpl">
<property name="exampleAttribute2" value="a value" />
</bean>
</beans>


To get all of this working as a demonstration, we're going to use the following Main class.


package com.javaeenotes;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {
new ClassPathXmlApplicationContext(new String[] { "beans.xml" });

try {
Thread.sleep(10000 * 100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}


Now, it's time to run this in our IDE. Or you can export it as runnable JAR, but make sure you place the beans.xml file in the same directory as the JAR-file.

After we run it, we're going to use the jconsole tool to lookup our bean. The tool can be found in the bin directory of your JDK installation directory. The screen below is presented to us, after we fire up jconsole.



Select our Main class and click on Connect, which will take us to the overview screen. Now select the MBeans tab, to find our bean.



Browse the directory tree to get the details of our bean. We can use it to view the values of the variables. We can even call the exposed methods of the bean. Try it!

This is a very, very short tutorial to get it working. Please use the following links to get more detailed information about JMX.

1 comment:

  1. Hi Cheng,
    Thanks for sharing your JMX information with us. Really you have represented very straight forward and easy example.

    Good for beginner.

    Thanks again.
    Binod Suman
    Bangalore, India

    ReplyDelete