JMX(二)HelloWorld

1.文件目录



 2.需要jdmkrt.jar附件可下载

 

3.创建 HelloMBean

 

 

package jmx.helloworld;

/**
 * HelloMBean定义方法及属性由Hello实现
 */
public interface HelloMBean {
	// operations

	public void sayHello();

	public int add(int x, int y);

	// attributes

	// a read-only attribute called Name of type String
	public String getName();

	// a read-write attribute called CacheSize of type int
	public int getCacheSize();

	public void setCacheSize(int size);
}
 

4.创建Hello实现HelloMBean接口,

 

package jmx.helloworld;

/**
 * 被监控的HelloBean
 * @author chenyw
 *
 */
public class Hello implements HelloMBean {

	private final String name = "Reginald";
	private static final int DEFAULT_CACHE_SIZE = 200;
	private int cacheSize = DEFAULT_CACHE_SIZE;

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

	public int add(int x, int y) {
		System.out.println("x+y="+(x+y));
		return x + y;
	}

	/*
	 * Getter for the Name attribute. The pattern shown here is frequent: the
	 * getter returns a private field representing the attribute value. In our
	 * case, the attribute value never changes, but for other attributes it
	 * might change as the application runs. Consider an attribute representing
	 * statistics such as uptime or memory usage, for example. Being read-only
	 * just means that it can't be changed through the management interface.
	 */
	public String getName() {
		return this.name;
	}

	/*
	 * Getter for the CacheSize attribute. The pattern shown here is frequent:
	 * the getter returns a private field representing the attribute value, and
	 * the setter changes that field.
	 */
	public int getCacheSize() {
		return this.cacheSize;
	}

	/*
	 *synchronized在多线程操作时能确保数据的唯一性
	 */
	public synchronized void setCacheSize(int size) {
		this.cacheSize = size;

		/*
		 * 
		 *
		 * 当设置CacheSize的值时控制台打印显示
		 * 
		 */
		System.out.println("Cache size now " + this.cacheSize);
	}

}
 

5.创建HelloAgent 

 

package jmx.helloworld;


import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import com.sun.jdmk.comm.HtmlAdaptorServer;

/**
 * 
 *该类是一个Agent类,说明: 先创建了一个MBeanServer,用来做MBean的容器
 * 将Hello这个类注入到MBeanServer中,注入需要创建一个ObjectName类
 * 创建一个AdaptorServer,这个类将决定MBean的管理界面,这里用最普通的Html型界面。 AdaptorServer其实也是一个MBean。
 * jmx.hello:name=Hello的名字是有一定规则的,格式为:“域名:name=MBean名称”,
 * 域名和MBean名称都可以任意取。 打开网页:http://localhost:8082/
 * 
 */
public class HelloAgent {
	public static void main(String[] args) throws Exception {
		// 创建一个MBeanServer,用来做MBean的容器
		MBeanServer mbs = MBeanServerFactory.createMBeanServer();
		// 将Hello这个类注入到MBeanServer中,注入需要创建一个ObjectName类
		ObjectName helloAdaptor = new ObjectName(
				"jmx.hello:name=Hello");
		mbs.registerMBean(new Hello(), helloAdaptor);
		ObjectName htmlAdaptorON = new ObjectName(
				"jmx.agent:name=HelloAgent,port=8082");
		// 创建一个AdaptorServer,这个类将决定MBean的管理界面,这里用最普通的Html型界面
		HtmlAdaptorServer htmlAdaptor = new HtmlAdaptorServer();
		mbs.registerMBean(htmlAdaptor, htmlAdaptorON);
		System.out.println("Starting the HtmlAdaptor....");
		htmlAdaptor.start();

	}
}
 

6.运行HelloAgent ,然后打开网页http://localhost:8082 如图所示



可以看到被监控HelloBean,点击进去可设置里面的值。如CacheSize 将value值设置为400,点击Apply

控制台打印为

Cache size now 400

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值