欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JMX—标准MBean和模型MBean演示

程序员文章站 2022-05-24 08:46:50
...
一共有四种MBean:

    标准MBean(Standard MBeans)设计和实现是最简单的,这类MBean使用自己的方法名作为管理接口。

    动态MBean(Dynamic MBeans)必须实现一个指定的接口,由于动态MBeans在运行期间暴露它们的管理接口,因此更为灵活。

    开放MBean(Open MBeans)属于动态MBeans,这类MBean依靠基础数据类型来实现通用管理,并为友情用户进行自我声明。

    模型MBean(Model MBeans)同样也是动态MBeans,这类MBeans是完全可配置的,在运行期间进行自我声明;它们为资源动态工具提供一个一般性的,有默认行为的MBeans类。

下面将演示标准MBean和模型MBean的使用方法。

以下演示的源码地址:https://github.com/smallbug-vip/repo,除此之外自己在网上还搜索了一个动态MBean的Demo。
标准MBean

首先定义被管理的bean:
public class Bug implements BugMBean{

	private String name;
	private String food;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFood() {
		return food;
	}

	public void setFood(String food) {
		this.food = food;
	}

	public void eat(String eat) {
		System.out.println("the bug is eating " + eat + ".");
	}

	@Override
	public String toString() {
		return "Bug [name=" + name + ", food=" + food + "]";
	}

}

该Bean实现了一个名称以MBean结尾的接口,这个接口中声明的方法都是Bean对象要向外暴露的操作接口方法。
public interface BugMBean {
	/**
	 * 获取名字
	 * 
	 * @timestamp Feb 10, 2016 3:22:58 PM
	 * @return
	 */
	public String getName();

	/**
	 * 设置名字
	 * 
	 * @timestamp Feb 10, 2016 3:23:23 PM
	 * @param name
	 */
	public void setName(String name);

	/**
	 * 最喜欢的食物是什么
	 * 
	 * @timestamp Feb 10, 2016 3:23:37 PM
	 * @return
	 */
	public String getFood();

	/**
	 * 设置最喜欢的食物
	 * 
	 * @timestamp Feb 10, 2016 3:23:55 PM
	 * @param food
	 */
	public void setFood(String food);

	/**
	 * 吃
	 * 
	 * @timestamp Feb 10, 2016 3:24:08 PM
	 * @param eat
	 */
	public void eat(String eat);

	/**
	 * 查看状态
	 * 
	 * @timestamp Feb 10, 2016 3:24:15 PM
	 * @return
	 */
	public String toString();
}

使用工具Adaptor测试代码:
try {
			helloName = new ObjectName("smallbug:name=Bug");
			server.registerMBean(new Bug(), helloName);

			ObjectName adapterName = new ObjectName("StandardAgent:name=htmladapter,port=8082");
			HtmlAdaptorServer adapter = new HtmlAdaptorServer();
			server.registerMBean(adapter, adapterName);
			adapter.start();
			System.out.println("start.....");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

之后可以在浏览器中输入:localhost:8082来管理Bug对象了。注意要使用HtmlAdaptorServer必须在pom.xml中引入:
<dependency>
			<groupId>com.sun.jdmk</groupId>
			<artifactId>jmxtools</artifactId>
			<version>1.2.1</version>
		</dependency>

也可以不使用Adapter:
private void test02() {
		try {
			helloName = new ObjectName("smallbug:name=Bug");
			server.registerMBean(new Bug(), helloName);

			changeProperties();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	private void changeProperties() {
		Attribute name = new Attribute("Name", "smallbug");
		Attribute food = new Attribute("Food", "leaves");
		try {
			server.setAttribute(helloName, name);
			server.setAttribute(helloName, food);

			System.out.println("the name is " + server.getAttribute(helloName, "Name"));
			Object[] params = { "beans" };
			String[] sign = { "java.lang.String" };
			server.invoke(helloName, "eat", params, sign);
			String s = (String) server.invoke(helloName, "toString", null, null);
			System.out.println(s);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

模板MBean
被管理Bean除了不实现接口,其他与上面Bug一样。然后定义BugMBean类,该类继承了BaseModelMBean:
public class BugMBean extends BaseModelMBean {

	public BugMBean() throws MBeanException, RuntimeOperationsException {
		super();
	}

	@Override
	public String getClassName() {

		return (this.resource.getClass().getName());

	}

	@Override
	public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException, ReflectionException {
		System.out.println("-------------------" + name);
		return super.getAttribute(name);
	}

	@Override
	public void setAttribute(Attribute attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
		System.out.println("+++++++++++++++++++" + attribute);
		super.setAttribute(attribute);
	}

}

对需要暴露的接口进行配置:
<?xml version="1.0"?>
<!DOCTYPE mbeans-descriptors PUBLIC
 "-//Apache Software Foundation//DTD Model MBeans Configuration File"
 "http://jakarta.apache.org/commons/dtds/mbeans-descriptors.dtd">

<mbeans-descriptors>

	<mbean name="myMBean" className="cn.smallbug.jdk.jmx._02.BugMBean"
		type="cn.smallbug.jdk.jmx._02.Bug">

		<attribute name="Name" type="java.lang.String" />
		<attribute name="Food" type="java.lang.String" />
		
		<operation name="eat" impact="ACTION" returnType="void">
			<parameter name="eat" type="java.lang.String" />
		</operation>
		
		<operation name="toString" impact="ACTION" returnType="java.lang.String">
			<parameter name="eat" type="java.lang.String" />
		</operation>
	</mbean>

</mbeans-descriptors>


测试:
public class ModelAgent {
	private Registry registry;
	private MBeanServer mBeanServer;

	public ModelAgent() {
		registry = createRegistry();
		try {
			mBeanServer = Registry.getServer();
		} catch (Throwable t) {
			t.printStackTrace(System.out);
			System.exit(1);
		}
	}

	public MBeanServer getMBeanServer() {
		return mBeanServer;
	}

	public Registry createRegistry() {
		Registry registry = null;
		try {
			URL url = ModelAgent.class.getResource("/jmx/bug-mbean.xml");
			InputStream stream = url.openStream();
			Registry.loadRegistry(stream);
			stream.close();
			registry = Registry.getRegistry();
		} catch (Throwable t) {
			System.out.println(t.toString());
		}
		return (registry);
	}

	public ModelMBean createModelMBean(String mBeanName) throws Exception {
		ManagedBean managed = registry.findManagedBean(mBeanName);
		if (managed == null) {
			System.out.println("ManagedBean null");
			return null;
		}
		ModelMBean mbean = managed.createMBean();
		ObjectName objectName = createObjectName();
		return mbean;
	}

	private ObjectName createObjectName() {
		ObjectName objectName = null;
		String domain = mBeanServer.getDefaultDomain();
		try {
			objectName = new ObjectName(domain + ":type=MyCar");
		} catch (MalformedObjectNameException e) {
			e.printStackTrace();
		}
		return objectName;
	}

	public static void main(String[] args) {
		ModelAgent agent = new ModelAgent();
		MBeanServer mBeanServer = agent.getMBeanServer();
		Bug bug = new Bug();
		System.out.println("Creating ObjectName");
		ObjectName objectName = agent.createObjectName();
		try {
			ModelMBean modelMBean = agent.createModelMBean("myMBean");
			modelMBean.setManagedResource(bug, "ObjectReference");
			mBeanServer.registerMBean(modelMBean, objectName);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		// manage the bean
		try {
			Attribute name = new Attribute("Name", "smallbug");
			Attribute food = new Attribute("Food", "leaves");
			mBeanServer.setAttribute(objectName, name);
			mBeanServer.setAttribute(objectName, food);
			String color = (String) mBeanServer.getAttribute(objectName, "Name");
			System.out.println("Name:" + color);

			Object[] params = { "beans" };
			String[] sign = { "java.lang.String" };
			mBeanServer.invoke(objectName, "eat", params, sign);
			String s = (String) mBeanServer.invoke(objectName, "toString", null, null);
			System.out.println(s);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

相关标签: java jmx MBean