JMX MBean Tutorial

JMX MBean Tutorial

JMX (Java Management Extension) is a pure java technology to manage system objects, service oriented network, monitor and manage application where these resource are represented by MBean (Managed Bean) dynamically managed at run time. This got introduced JAVA 5 release.

To manage resources you will have to create MBean first and register it with MBean server. To manage running MBean on server JMX needs to connect MBean server. Java comes with connector called JConsole and its location is JDK_HOME/bin director. Below is example:

  • First create interface JMXMBean.java (Please be careful when creating MBean where you interface name should be ended with MBean)
package com.javahonk;

public interface JMXMBean {
	
	public void sayHello(); 
    public int add(Integer x, Integer y); 
    
    public String getName(); 
     
    public int getCacheSize(); 
    public void setCacheSize(int size); 
	
}
  • Implementation class name should be JMX (Please note: According to JXM specificaiton name should be Interface name without MBean keyword)
package com.javahonk;

public class JMX implements JMXMBean {
	
	public void sayHello() { 
        System.out.println("JMX MBean Sample"); 
    } 
     
    public int add(Integer x, Integer y) { 
        return x + y; 
    } 
     
    public String getName() { 
        return this.name; 
    }  
     
    public int getCacheSize() { 
        return this.cacheSize; 
    } 
     
    public synchronized void setCacheSize(int size) {
        
    	this.cacheSize = size; 
        System.out.println("Cache size now " + this.cacheSize); 
    } 
    
    private final String name = "Java Honk"; 
    private int cacheSize = DEFAULT_CACHE_SIZE; 
    private static final int DEFAULT_CACHE_SIZE = 200;
	
}
  • JMXMBeanAgent.java: Main class to start MBean and test it:
package com.javahonk;

import java.lang.management.ManagementFactory;

import javax.management.MBeanServer;
import javax.management.ObjectName;

public class JMXMBeanAgent {
	
	MBeanServer mBeanServer = null;
	ObjectName objectName = null;
	JMX jmx = null;

	public JMXMBeanAgent(){		

		try {
			// Give name of MBean
			objectName = new ObjectName("com.javahonk:type=JMXMBean");
			mBeanServer = ManagementFactory.getPlatformMBeanServer();
			jmx = new JMX();
			mBeanServer.registerMBean(jmx, objectName);			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws InterruptedException {
		
		JMXMBeanAgent jmxmBeanAgent = new JMXMBeanAgent();
		jmxmBeanAgent.callJMXMethod();
		System.out.println("Thread is going to sleep forever..."); 
        Thread.sleep(Long.MAX_VALUE); 		

	}
	
	public void callJMXMethod() {
		try {
			System.out.println("Cache size: "+jmx.getCacheSize());
			System.out.println("Name: "+jmx.getName());
			System.out.println("Add value 10=20: "+jmx.add(10, 20));
			
		} catch (Exception e) {
			e.printStackTrace();
		}		

	}	

}
  • To run this you will have to pass below VM parameter:

-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=1649
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false

  • In eclipse pass as below:

JMX MBean Tutorial

  • To run: Right click JMXMBeanAgent.java Run As –> Java Application. As you see the parameter above we have started JMX at port number 1649. To connect remotely to this server we need to give system ip address and port number. Now to connect it through JConsole. Go to %JAVA_HOME%\bin and double click jconsole.exe:

JMX MBean Tutorial

  • You will see JConsole will find find running agent and its pid. Select that pid then click connect:

2015-04-20_2354_001

  • If you see below warning click insecure:

JMX MBean Tutorial

  • JConsole will open as below. Go to MBean tab there you will see our registered MBean:

JMX MBean Tutorial

  • For more details visit MBean oracle documentation here

Leave a Reply

Your email address will not be published. Required fields are marked *