Saturday 16 June 2012

JMX with Spring



I was already impressed by Spring as it allows you to learn the complexities of enterprise world within few minutes. We don't have to bother about the application server's complexity of deployment. We don't have to frustrate ourselves with those non-understandable errors. While exploring it, I came across JMX integration using Spring. I had little idea what JMX can provide us, but hadn't dared  to create my own MBeans. And here, Spring gives one of the simplest ways to create your own MBean. Within 10 min, I could create my own MBean and also could observe it through JConsole. I wrote just a TestMbean class and the configuration XML as shown below. 

package com.test.spring.jmx;
public class TestMBean
{
    private int count;

    public TestMBean(final int count)
    {
        this.count = count;
    }

    public int getCount()
    {
        System.out.println(count);
        return count;
    }
}
 



And then tested the above with following test class - 
package com.test.spring.jmx;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* Unit test for simple App.
*/
public class AppTest
{

    final private static String SPRING_CONFIG_LOCATION = "applicationContext.xml";

    @Test
    public void test() throws Exception
    {

        new ClassPathXmlApplicationContext(SPRING_CONFIG_LOCATION);

        Thread.sleep(70000);
    }

}
  
 After this, I ran the JConsole for $JAVA_HOME/bin and could see the following -