JMX学习笔记(二):MBean

1.Architecture of the JMX Technology

The JMX technology can be divided into three levels, as follows:

  • Instrumentation
  • JMX agent
  • Remote management

2.Introducing MBeans

The JMX specification defines five types of MBean:
  • Standard MBeans
  • Dynamic MBeans
  • Open MBeans
  • Model MBeans
  • MXBean
3.Standard MBeans
#step 1: MBean Interface
package jmx_demo;

public interface HelloMBean {
	public void sayHello();

	public int add(int x, int y);

	public String getName();

	public int getCacheSize();

	public void setCacheSize(int size);

}

#step 2:MBean Implementation
package jmx_demo;

public class Hello implements HelloMBean {
	private final String name = "Programmer";
	private int cacheSize = DEFAULT_CACHE_SIZE;
	private static final int DEFAULT_CACHE_SIZE = 200;

	@Override
	public void sayHello() {
		System.out.println("hello,world!");
	}

	@Override
	public int add(int x, int y) {
		return x + y;
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public int getCacheSize() {
		return this.cacheSize;
	}

	@Override
	public void setCacheSize(int size) {
		this.cacheSize = size;
		System.out.println("Cache size now " + this.cacheSize);
	}

}

#step 3:Create a JMX Agent to Manage a Resource:
package jmx_demo;

import java.lang.management.ManagementFactory;

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

public class Main {
	public static void main(String[] args) throws Exception{
		MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
		ObjectName name = new ObjectName("com.jmx:type=Hello");
		Hello mbean = new Hello();
		mbs.registerMBean(mbean, name);
		System.out.println("Waiting forever...");
		Thread.sleep(Long.MAX_VALUE);
	}
}

#step 4:Running the Standard MBean
start Jconsloe (location:java\jdk1.7.0_45\bin\jconsole.exe)

JMX学习笔记(二):MBean_第1张图片


你可能感兴趣的:(JMX学习笔记(二):MBean)