本文共 4007 字,大约阅读时间需要 13 分钟。
package mbean.modelbean;
import mbean.standard.HelloMBean;
public class Hello implements HelloMBean { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void printHello() { System.out.println("Hello World, " + name); } public void printHello(String whoName) { System.out.println("Hello , " + whoName); } }
package mbean.modelbean;
import java.io.InputStream; import javax.management.MBeanServer; import javax.management.ObjectName; import javax.management.modelmbean.ModelMBean; import org.apache.commons.modeler.ManagedBean; import org.apache.commons.modeler.Registry; import com.sun.jdmk.comm.HtmlAdaptorServer;
public class HelloAgent { public static void main(String[] args) throws Exception { //基于xml中的信息构建一个Registry Registry registry = Registry.getRegistry(null, null); InputStream stream = HelloAgent.class.getResourceAsStream("Mbeans-descriptors.xml"); registry.loadMetadata(stream); stream.close(); //由Registry得到一个MBeanServer MBeanServer server = registry.getMBeanServer();
//得到Hello在描述文件中的信息类,对应于xml文件标签的name属性。 ManagedBean managed = registry.findManagedBean("Hello"); //创建ObjectName ObjectName helloName = new ObjectName(managed.getDomain() + ":name=HelloWorld"); //得到ModelMBean ModelMBean hello = managed.createMBean(new Hello()); //注册MBean server.registerMBean(hello, helloName);
ObjectName adapterName = new ObjectName("HelloAgent:name=htmladapter,port=8082"); HtmlAdaptorServer adapter = new HtmlAdaptorServer(); server.registerMBean(adapter, adapterName); adapter.start(); System.out.println("start....."); } }
Registry registry = Registry.getRegistry(null, null); InputStream stream = HelloAgent.class.getResourceAsStream("Mbeans-descriptors.xml"); registry.loadMetadata(stream); stream.close();
作者简介
陈刚,广西桂林人,著作有《Eclipse从入门到精通》
您可以通过其博客了解更多信息和文章:转载地址:http://dmagx.baihongyu.com/