java之JMX

Posted cxchanpin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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改动变量:
技术分享

改动变量值:

技术分享

技术分享


运行改动:
技术分享

看看我们执行的程序输出:
技术分享
是不是改动后的属性生效了。





























以上是关于java之JMX的主要内容,如果未能解决你的问题,请参考以下文章

Zabbix3.2.6之通过JMX监控Tomcat

理解JMX之介绍和简单使用

性能监控之 JMX 监控 Docker 容器中的 Java 应用

195 - JMX的组态和服务URL

195 - JMX的组态和服务URL

195 - JMX的组态和服务URL