码迷,mamicode.com
首页 > 其他好文 > 详细

JMX入门开发

时间:2014-06-18 09:05:39      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   http   

什么是JMX?或者是JMX是做什么的?我的理解是:可以远程管理/编辑JAVA对象。如图:

bubuko.com,布布扣

上面的SchemaName属性就是可以动态修改的,那么是如何做到的哪?下面咱们逐步分析。

一、首先假设咱们有个对象SystemConfig,包含两个属性threadCount和schemaName

package study;

public class SystemConfig implements SystemConfigMBean {
    private int threadCount;
    private String schemaName;

    public SystemConfig(int numThreads, String schema) {
        this.threadCount = numThreads;
        this.schemaName = schema;
    }

    @Override
    public void setThreadCount(int noOfThreads) {
        this.threadCount = noOfThreads;
    }

    @Override
    public int getThreadCount() {
        return this.threadCount;
    }

    @Override
    public void setSchemaName(String schemaName) {
        this.schemaName = schemaName;
    }

    @Override
    public String getSchemaName() {
        return this.schemaName;
    }

    @Override
    public String doConfig() {
        return "No of Threads=" + this.threadCount + " and DB Schema Name=" + this.schemaName;
    }
}
package study;

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();
}

二、需要实现远程动态修改这个对象,那么就需要将这个类注册到JMX里面,代码如下:

package study;

import java.lang.management.ManagementFactory;

import javax.management.MBeanServer;
import javax.management.ObjectName;

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 Exception {

        // Get the MBean server
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        // register the MBean
        SystemConfig mBean = new SystemConfig(DEFAULT_NO_THREADS, DEFAULT_SCHEMA);
        ObjectName name = new ObjectName("xman:type=SystemConfig");
        mbs.registerMBean(mBean, name);
        do {
            Thread.sleep(3000);
            System.out.println("Thread Count=" + mBean.getThreadCount() + ":::Schema Name=" + mBean.getSchemaName());
        } while (mBean.getThreadCount() != 0);
    }

}

三、想要支持远程管理,就需要配置JMX参数,如图:

bubuko.com,布布扣

bubuko.com,布布扣

四、在本地运行jconsole链接上来,然后修改Count的值:

bubuko.com,布布扣

五、最后再看结果:

bubuko.com,布布扣

实现了上面的内容,那么自然就会想能否程序自动调用修改,而不用jconsole,下面就可以了:

package study;

import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class Client {

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://127.0.0.1:8888/jmxrmi");
    JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
    
    MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
    
    ObjectName mbeanName = new ObjectName("xman:type=SystemConfig");
    
    SystemConfigMBean mbeanProxy = JMX.newMBeanProxy(mbsc, mbeanName, SystemConfigMBean.class, true);
    int threadCount = mbeanProxy.getThreadCount();
    System.out.println("Current ThreadCount: " + threadCount);
    mbeanProxy.setThreadCount(100);
    threadCount = mbeanProxy.getThreadCount();
    System.out.println("Changed ThreadCount: " + threadCount);
    
    Thread.sleep(3);
    jmxc.close();
    }

}

上面的程序不需要我怎么解释了吧,远程连接,修改内容。

JMX入门开发,布布扣,bubuko.com

JMX入门开发

标签:style   class   blog   code   java   http   

原文地址:http://www.cnblogs.com/liqiu/p/3793373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!