`

mbean的样例代码

 
阅读更多

可以发布一个Mbean后,通过html进行远程访问

package com.fan;

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

import com.fan.bean.Hello;
import com.sun.jdmk.comm.HtmlAdaptorServer;

public class TestMbeanAgent {
    public static void main(String[] args) throws Exception {
        MBeanServer server = MBeanServerFactory.createMBeanServer();
        ObjectName helloName = new ObjectName("chengang:name=HelloWorld");
        server.registerMBean(new Hello(), helloName);
        ObjectName adapterName = new ObjectName("HelloAgent:name=htmladapter,port=8082");
        HtmlAdaptorServer adapter = new HtmlAdaptorServer(8082);

        server.registerMBean(adapter, adapterName);
        adapter.start();
        System.out.println("start.....");

        // 使用http://localhost:8082/ 进行访问,可以修改HelloWorld的值
        // 使用HtmlAdaptorServer的目的是可以通过html来进行访问,需要添加jar包jmxtools
    }
}

 

也可以直接获得当前jvm内的信息

package com.fan;

import java.lang.management.ManagementFactory;

public class TestMbeanInfo {
    public static void main(String[] args) {

        System.out.println(ManagementFactory.getRuntimeMXBean().getName());
        System.out.println(ManagementFactory.getRuntimeMXBean().getBootClassPath());
    }

}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics