JMX : Standard MBean

來源:互聯網
上載者:User

Standard MBean由一個介面和一個實作類別組成。

命名規範 :

介面 : XXXMBean

實作類別 : XXX

按照約定,MBean的name為XXX。自定。

介面中的每個方法定義了一個attribute或operation

以get或set開頭的方法 : 如果符合getter或setter的模式,就是定義了一個attribute,類型為getter的傳回值類型或setter參數類型。

其他方法 : 定義了一個operation。

樣本:

package test.xue.mbean;public interface HelloMBean {public void sayHello();public int add(int x, int y);public String getName();public int getCacheSize();public void setCacheSize(int size);public void setText();public void settitle(String title);}

HelloMBean介面定義了3個attribute和3個operation :

attributes:

name : readonly

cacheSize : readable and writable

title : writable

operations:

sayHello

add

setText : 不符合setter模式。


實作類別:

package test.xue.mbean;public class Hello implements HelloMBean {private final String name = "My HelloMBean";private int cacheSize = 1024;private String title;@Overridepublic void sayHello() {System.out.println("hello, my HelloMBean");}@Overridepublic int add(int x, int y) {return x+y;}@Overridepublic String getName() {return name;}@Overridepublic int getCacheSize() {return cacheSize;}@Overridepublic void setCacheSize(int size) {this.cacheSize = size;System.out.println("cacheSize changed : " + size);}@Overridepublic void setText() {System.out.println("setText called");}@Overridepublic void settitle(String title) {this.title = title;System.out.println("title changed : " + title);}}

以上,HelloMBean和Hello就定義了一個MBean,通過這個MBean的operation和attribute,就可以管理特定的資源。

現在,就可以建立一個JMX agent來通過MBean管理資源了。

package test.xue.mbean.management;import java.lang.management.ManagementFactory;import javax.management.InstanceAlreadyExistsException;import javax.management.MBeanRegistrationException;import javax.management.MBeanServer;import javax.management.MalformedObjectNameException;import javax.management.NotCompliantMBeanException;import javax.management.ObjectName;import test.xue.mbean.Hello;public class HelloAgent {public static void main(String[] args) throws MalformedObjectNameException,NullPointerException, InstanceAlreadyExistsException,MBeanRegistrationException, NotCompliantMBeanException,InterruptedException {MBeanServer server = ManagementFactory.getPlatformMBeanServer();ObjectName name = new ObjectName("test.xue.mbean:type=Hello"); // any name//ObjectName name = new ObjectName("test.xue.mbean22:type=Hello2"); // also worksHello mbean = new Hello();server.registerMBean(mbean, name);System.out.println("waiting here for remote management...");Thread.sleep(Long.MAX_VALUE);}}

如上,每個MBean都需要一個ObjectName,ObjectName的命名規範:

domain:(key=value)+

例子中的domain=test.xue.mbean,type=Hello,可任意指定。


可通過JConsole來測試此JMX agent

運行 : jconsole

串連以後,


可以嘗試修改attributes,調用operations,同時注意控制台輸出。


如果出現javax.management.NotCompliantMBeanException :

Exception in thread "main" javax.management.NotCompliantMBeanException: MBean class test.xue.mbean.Hello2 does not implement DynamicMBean, neither follows the Standard MBean conventions (javax.management.NotCompliantMBeanException: Class test.xue.mbean.Hello2 is not a JMX compliant Standard MBean) nor the MXBean conventions (javax.management.NotCompliantMBeanException: test.xue.mbean.Hello2: Class test.xue.mbean.Hello2 is not a JMX compliant MXBean)at com.sun.jmx.mbeanserver.Introspector.checkCompliance(Unknown Source)at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(Unknown Source)at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(Unknown Source)at test.xue.mbean.management.HelloAgent.main(HelloAgent.java:24)

可能的原因是:

介面和實作類別的名字不符合命名規範: XXXMBean, XXX。

聯繫我們

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

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.