JMX入門之StandardMBean HelloWord

來源:互聯網
上載者:User
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();     }}


相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。