`
tuhaitao
  • 浏览: 378749 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

JMX学习笔记(三)-MXBean

    博客分类:
  • java
阅读更多

在MBean中有只要遵循以下两个规则,我们就可以在jconsole中动态的改变MBean中的属性值

1. JMX中要定义接口必须以xxxMBean的规范定义

2. 得有类实现xxxMBean接口

 

例如:

 

package com.haitao.jmx.mxbean;

/**
 * Server Configure MBean
 * 
 * @author haitao.tu
 *
 */
public interface ServerConfigureMBean {

	public int getPort();

	public void setPort(int port);

	public String getHost();

	public void setHost(String host);

	public int getMaxThread();

	public void setMaxThread(int maxThread);

	public int getMinThread();

	public void setMinThread(int minThread);
	
}

 

 

接着实现这个MBean:

 

 

package com.haitao.jmx.mxbean;


/**
 * Server Configure
 * 
 * @author haitao.tu
 *
 */
public class ServerConfigure implements ServerConfigureMBean {

	private int port;
	
	private String host;
	
	private int maxThread;
	
	private int minThread;
	
	public ServerConfigure(int port, String host, int maxThread, int minThread) {
		this.port = port;
		this.host = host;
		this.maxThread = maxThread;
		this.minThread = minThread;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getMaxThread() {
		return maxThread;
	}

	public void setMaxThread(int maxThread) {
		this.maxThread = maxThread;
	}

	public int getMinThread() {
		return minThread;
	}

	public void setMinThread(int minThread) {
		this.minThread = minThread;
	}
	
}

 

测试:

 

package com.haitao.jmx.mxbean;

import java.lang.management.ManagementFactory;

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

/**
 * Server Test
 * 
 * @author haitao.tu
 *
 */
public class ServerTest {

	public static void main(String[] args) throws Exception {
		testServerConfigureMBean();
	}
	
	public static void testServerConfigureMBean() throws Exception {
		MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

		ServerConfigure serverConfigure = new ServerConfigure(8080,
				"test.haitao.com", 20, 100);

		ObjectName serverName = new ObjectName(
				"com.haitao.jmx.mxbean:type=ServerConfigure");
		mbs.registerMBean(serverConfigure, serverName);
		System.out.println("Waiting...");
		Thread.sleep(Long.MAX_VALUE);
	}

}

 

打开jconsole:

 

但是以上这些ServerConfigure的属性都是基本类型,包含在java.lang.*里,设想,如果说jconsole在类外一个远端的jvm上,如果这里的属性是一个自定义对象,远端的jvm可能不知道该类型的对象:

 

修改上边的ServerConfigre,使他成为一个聚合对象,去掉ServerConfigureMBean接口实现:

 

 

package com.haitao.jmx.mxbean;


/**
 * Server Configure
 * 
 * @author haitao.tu
 *
 */
public class ServerConfigure {

	private int port;
	
	private String host;
	
	private int maxThread;
	
	private int minThread;
	
	public ServerConfigure(int port, String host, int maxThread, int minThread) {
		this.port = port;
		this.host = host;
		this.maxThread = maxThread;
		this.minThread = minThread;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getMaxThread() {
		return maxThread;
	}

	public void setMaxThread(int maxThread) {
		this.maxThread = maxThread;
	}

	public int getMinThread() {
		return minThread;
	}

	public void setMinThread(int minThread) {
		this.minThread = minThread;
	}
	
}
 

新建一个ServerMBean接口:

 

package com.haitao.jmx.mxbean;

/**
 * Server MBean
 * 
 * @author haitao.tu
 *
 */
public interface ServerMBean {
	
	public ServerConfigure getServerConfigure();
	
	public void setServerConfigure(ServerConfigure serverConfigure);
	
	public void defaultServerConfigure();
	
}

 

实现ServerMBean并封装ServerConfigure类:

 

package com.haitao.jmx.mxbean;

/**
 * Server
 * 
 * @author haitao.tu
 *
 */
public class Server implements ServerMBean {
	
	/**
	 * 封装
	 */
	private ServerConfigure serverConfigure;
	
	public Server(ServerConfigure serverConfigure) {
		this.serverConfigure = serverConfigure;
	}
	
	@Override
	public ServerConfigure getServerConfigure() {
		synchronized (serverConfigure) {
			return this.serverConfigure;
		}
	}
	
	@Override
	public void setServerConfigure(ServerConfigure serverConfigure) {
		synchronized (serverConfigure) {
			this.serverConfigure = serverConfigure;
		}
	}
	
	@Override
	public void defaultServerConfigure() {
		synchronized (serverConfigure) {
			serverConfigure.setPort(80);
			serverConfigure.setHost("www.haitao.com");
			serverConfigure.setMinThread(10);
			serverConfigure.setMaxThread(500);
		}
	}
	
}

 

测试:

 

package com.haitao.jmx.mxbean;

import java.lang.management.ManagementFactory;

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

/**
 * Server Test
 * 
 * @author haitao.tu
 *
 */
public class ServerTest {

	public static void main(String[] args) throws Exception {
		testServerMBean();
	}
	
	public static void testServerMBean() throws Exception {
		MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

		ServerConfigure serverConfigure = new ServerConfigure(8080,
				"test.haitao.com", 20, 100);

		Server server = new Server(serverConfigure);

		ObjectName serverName = new ObjectName(
				"com.haitao.jmx.mxbean:type=Server");
		mbs.registerMBean(server, serverName);
		System.out.println("Waiting...");
		Thread.sleep(Long.MAX_VALUE);
	}

}

 

打开jconsole查看:

 

为了让与远端JVM认识该对象,我们需要将ServerMbean改为ServerMXBean,并使用java.beans.ConstructorProperties注解告诉JVM怎样组装该对象:

 

1. 使用java.beans.ConstructorProperties注解:

 

package com.haitao.jmx.mxbean;

import java.beans.ConstructorProperties;

/**
 * Server Configure
 * 
 * @author haitao.tu
 *
 */
public class ServerConfigure {

	private int port;
	
	private String host;
	
	private int maxThread;
	
	private int minThread;
	
	/**
	 * 加入@ConstructorPropertiesz注解
	 */
	@ConstructorProperties({"port", "host", "maxThread", "minThread"})
	public ServerConfigure(int port, String host, int maxThread, int minThread) {
		this.port = port;
		this.host = host;
		this.maxThread = maxThread;
		this.minThread = minThread;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getMaxThread() {
		return maxThread;
	}

	public void setMaxThread(int maxThread) {
		this.maxThread = maxThread;
	}

	public int getMinThread() {
		return minThread;
	}

	public void setMinThread(int minThread) {
		this.minThread = minThread;
	}
	
}

 2. 修改ServerMbean为ServerMXBean:

 

package com.haitao.jmx.mxbean;

/**
 * Server MXBean
 * 
 * @author haitao.tu
 *
 */
public interface ServerMXBean {
	
	public ServerConfigure getServerConfigure();
	
	public void setServerConfigure(ServerConfigure serverConfigure);
	
	public void defaultServerConfigure();
	
}
 

测试代码一样,就不再重复,最后运行测试,查看jconsole:

 

 

双击值,我们将会看到:

 

实际上MXBean的概念很简单,就是定义xxxMXBean接口,然后实现这个接口,与MBean类似,

 

为了阐述这个MXBean与MBean的不同才列出这么多代码,希望大家能理解:)

分享到:
评论
5 楼 u014689192 2017-12-20  
很不错
4 楼 AquariusM 2014-10-30  
奢华的JVM介绍!
3 楼 nbkangta 2014-02-27  
比外文资料容易理解多了~
2 楼 chigooo 2011-12-15  
讲的非常详细~赞!
1 楼 heavensay 2011-07-01  
    thanks,学习了

相关推荐

Global site tag (gtag.js) - Google Analytics