1 写一个接口,名字以MBean结尾
package test;
public interface HelloMBean {
public void test() ;
}
2 写一个类实现上面的接口,名字是接口的名字去掉MBean
package test;
public class Hello implements HelloMBean {
public Hello()
{
System.out.println("mbean is created...") ;
}
public void test() {
System.out.println("test created...") ;
}
}
3测试
try {
//创建MBeanServer
MBeanServer server = MBeanServerFactory.createMBeanServer() ;
//注册MBean需要ObjectName
ObjectName name = new ObjectName("mbean:name=htmladapter,port=8082") ;
//用户管理MBean的页面
HtmlAdaptorServer adaptor = new HtmlAdaptorServer() ;
server.registerMBean(adaptor, name) ;
adaptor.start() ;
/**
* 你访问http://localhost:8082 就可以管理MBean了
* 如注册Hello时,控制台就显示mbean is created...
*
* 当然还可以在管理界面对删除,查询等操作……
*/
} catch (MalformedObjectNameException e) {
e.printStackTrace();
} catch (InstanceAlreadyExistsException e) {
e.printStackTrace();
} catch (MBeanRegistrationException e) {
e.printStackTrace();
} catch (NotCompliantMBeanException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}