standard|word
这个简单的Mbean只有一个属性和一个方法,StardBeanDemoMain 为调用类,main方法中调用下面三个方法,代码中都有详细的注释: sbean.doRegistBean(); sbean.doManageBean(); sbean.regHtmlAdaptor();
运行后,可以看到控制台打印的信息,register StandMbean sucess..Attribute:state -init valueAttribute:state 2 -new stateMy service start.....
由于启动了HtmlAdaptor的服务,所以程序一直处于运行状态,(HtmlAdaptor是sun jmx实现自带的一个适配器)测试可以在浏览器中访问:http://localhost:8082 就可以在浏览器中管理mbean了,在List of MBean operations下面按startService 按钮,可以看到控制台打出的信息,说明startService方法被调用了。
/** * Mbean接口 * StandBeanMBean.java * @author zmxj */public interface StandBeanMBean { public String getState() ; public void setState(String s) ; public void startService();}
/** * Mbean实现 * StandBean.java * @author zmxj */public class StandBean implements StandBeanMBean {
private String state = "init value";
public String getState() { return state; }
public void setState(String s) { state = s; }
public void startService() { System.out.println("My service start....."); }
}
import javax.management.Attribute;import javax.management.AttributeNotFoundException;import javax.management.InstanceNotFoundException;import javax.management.InvalidAttributeValueException;import javax.management.MBeanException;import javax.management.MBeanServer;import javax.management.MBeanServerFactory;import javax.management.MalformedObjectNameException;import javax.management.ObjectName;import javax.management.ReflectionException;
import com.sun.jdmk.comm.HtmlAdaptorServer;
/** * Agent类 * StardBeanDemoMain.java * @author zmxj */public class StardBeanDemoMain {
private MBeanServer mBserver = null; ObjectName mbeanObjectName = null; String domain = null; String mbeanName = "StandBean"; public static void main(String[] args) { StardBeanDemoMain sbean = new StardBeanDemoMain(); sbean.doRegistBean(); sbean.doManageBean(); sbean.regHtmlAdaptor(); } /** * 创建MBeanServer并注册一个Mbean */ private void doRegistBean() { //创建MBeanServer mBserver = MBeanServerFactory.createMBeanServer(); domain = mBserver.getDefaultDomain();
try { mbeanObjectName = new ObjectName(domain + ":type=" + mbeanName); /* //这里可以直接创建并且同时注册一个mbean到MBeanServer. server.createMBean(mbeanName,mbeanObjectName); */ StandBean bean = new StandBean(); mBserver.registerMBean(bean, mbeanObjectName); System.out.println("register StandMbean sucess.."); } catch(MalformedObjectNameException e) { e.printStackTrace(); System.exit(1); } catch(Exception e){ e.printStackTrace(); System.exit(1); } }
/* * 演示管理mbean,通过mbserver修改mbean的属性和执行mbean的方法 */ private void doManageBean() { try { ObjectName mbeanObjectName = new ObjectName(domain + ":type=" + mbeanName); System.out.println("Attribute:state -"+mBserver.getAttribute(mbeanObjectName,"State"));
//修改属性State Attribute stateAttribute = new Attribute("State","new state"); mBserver.setAttribute(mbeanObjectName, stateAttribute); //取得属性值 System.out.println("Attribute:state 2 -"+mBserver.getAttribute(mbeanObjectName,"State")); //调用方法startService mBserver.invoke(mbeanObjectName,"startService",null,null); } catch (Exception e) { e.printStackTrace(); } } /* * 注册HtmlAdaptor,注册后可以通过http://localhost:8082执行mbean管理操作 */ private void regHtmlAdaptor() { HtmlAdaptorServer html = new HtmlAdaptorServer(); ObjectName html_name = null; try { html_name = new ObjectName("Adaptor:name=html,port=8082"); mBserver.registerMBean(html, html_name); } catch(Exception e) { System.out.println("\t!!! Could not create the HTML adaptor !!!"); e.printStackTrace(); return; } html.start(); }}