How to Export Spring Beans into JMX Console?

In spring container we might have defined many objects , how can we expose those beans in JMX console. JMX console is the utility provided by java so that we can monitor and access run time parameters. Lets create sample spring class that is going to registered as MBean.

package com;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedOperationParameter;
import org.springframework.jmx.export.annotation.ManagedOperationParameters;
import org.springframework.jmx.export.annotation.ManagedResource;

public class SpringJMXExample {

public String getMessage() {
  return "Hello";

@ManagedResource annotation tells the container this class going to act as Mbean. Now we have Mbean class which returns as message Hello, we need to register this class in MBean server. In order to register this class we need to create following entries in spring context xml.

<bean id="mbeanExporter"
     class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter" />

Above class register all the MBean class into Mbean server. In order to access your MBean through JConsole we have to pass some JVM arguments when we run our program. In web based application we can set JRE arguments as like below.

set JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8855 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

Now we are ready with configuration start your server. Go to command prompt type jconsole.If your java home is set properly we can see following jconsole window.


 Select local process if your server is running in locally or select remote proceess. Now type the following url No need to give user name and password click connect. As soon as connected we can see the class name is getting displayed. Now when we expand the class name it will give attributes and operations as options. Here operations means methods we defined in Mbean class,now if you click getMessage method it will return and says Hello.


Post a Comment

Powered by Blogger.