java之JMX

來源:互聯網
上載者:User

標籤:art   pos   actor   any   百度百科   read   mod   code   lang   

java之JMX 有關JMX的定義和架構就不具體解釋了。見百度百科:


http://baike.baidu.com/link?

url=6QzGGEqphTmpft3ll5mXmDNVRdvLRZhkvGaqAWyO6EliwrHeIwt5bdMd188iMlzylxoxr7gRbtIWn2NQODBLZa


代碼執行個體:與建立一個普通的bean沒什麼差別:
package com.doctor.java.jmx;/** * @author sdcuike * * @time 2016年2月9日 下午9:47:04 *  * @see http://www.journaldev.com/1352/what-is-jmx-mbean-jconsole-tutorial *      The interface name must end with MBean */public interface SystemConfigMBean {    public void setThreadCount(int noOfThreads);    public int getThreadCount();    public void setSchemaName(String schemaName);    public String getSchemaName();    // any method starting with get and set are considered    // as attributes getter and setter methods, so I am    // using do* for operation.    public String doConfig();}



package com.doctor.java.jmx;/** * @author sdcuike * * @time 2016年2月9日 下午9:51:53 */public class SystemConfig implements SystemConfigMBean {    private int threadCount;    private String schemaName;    public SystemConfig(int threadCount, String schemaName) {        this.threadCount = threadCount;        this.schemaName = schemaName;    }    @Override    public void setThreadCount(int noOfThreads) {        this.threadCount = noOfThreads;    }    @Override    public int getThreadCount() {        return threadCount;    }    @Override    public void setSchemaName(String schemaName) {        this.schemaName = schemaName;    }    @Override    public String getSchemaName() {        return schemaName;    }    @Override    public String doConfig() {        return "No of Threads=" + this.threadCount + " and DB Schema Name=" + this.schemaName;    }}

然後我們建立一個測試範例:
package com.doctor.java.jmx;import java.lang.management.ManagementFactory;import java.util.concurrent.TimeUnit;import javax.management.InstanceAlreadyExistsException;import javax.management.MBeanRegistrationException;import javax.management.MBeanServer;import javax.management.MalformedObjectNameException;import javax.management.NotCompliantMBeanException;import javax.management.ObjectName;/** * @author sdcuike * * @time 2016年2月9日 下午9:56:27 */public class SystemConfigManagement {    private static final int DEFAULT_NO_THREADS = 10;    private static final String DEFAULT_SCHEMA = "default";    public static void main(String[] args) throws MalformedObjectNameException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, InterruptedException {        // Get the MBean server        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();        // register the MBean        SystemConfig systemConfig = new SystemConfig(DEFAULT_NO_THREADS, DEFAULT_SCHEMA);        ObjectName objectName = new ObjectName("com.doctor.java.jmx:type=SystemConfig");        mBeanServer.registerMBean(systemConfig, objectName);        do {            TimeUnit.SECONDS.sleep(3);            System.out.println("Thread Count=" + systemConfig.getThreadCount() + ":::Schema Name=" + systemConfig.getSchemaName());        } while (systemConfig.getThreadCount() != 0);    }}

執行:執行的時候,我們必須啟用虛擬機器選項-Dcom.sun.management.jmxremote


執行
如今我們用java工具jconsole改動變數:

改動變數值:





運行改動:

看看我們執行的程式輸出:
是不是改動後的屬性生效了。


java之JMX

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.