這個簡單的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(); }}