程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> java之JMX

java之JMX

編輯:JAVA綜合教程

java之JMX


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修改變量: \   修改變量值:   \   \     執行修改: \   看看我們運行的程序輸出: \ 是不是修改後的屬性生效了。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved