Hello JMX!
 
环境:
jdk-6u21-windows-i586.exe
jmx-1_2_1-ri.zip
网上流传一种说法是jdk5之后,在rt.jar中已经包含了JMX的参考实现,经本人验证,是谬传!jmx-1_2_1-ri.zip是SUN提供的JMX规范的一种参考实现。当然像JBOSS、WebLogic、WebSphere都有自己的实现方式。
 
下面结合JMX的规范,实现一个最简单的JMX应用。
 
一、定义MBean接口
 
/**
* MBean接口,实现此接口的类中方法都将是可以被管理的。
* MBean起名是有规范的,就是原类名后加上MBean字样
*
* @author Administrator 2010-12-6 17:54:50
*/

public interface HelloMBean {
  public void sayHello1();
  public void sayHello2(String someone);
  public void sayHello3(String msg);
}
 
二、定义MBean的实现类(被管理类)
 
/**
* MBean实现类,被管理类
*
* @author Administrator 2010-12-6 17:56:25
*/

public class Hello implements HelloMBean{
        private String saySomething;
        private String msg;

        public Hello() {
                this.saySomething = "Hello JMX!";
        }

        public Hello(String saySomething) {
                this.saySomething = saySomething;
        }

        public void sayHello1() {
                System.out.println("Hello.sayHello1()被调用:"+saySomething);
        }

        public void sayHello2(String someone) {
                System.out.println("Hello.sayHello2(String someone)被调用:Hello, " + someone+"! This is a JMX Application!");
        }

        public void sayHello3(String msg) {
                System.out.println("Hello.sayHello3(String msg)被调用:Hello, you input value="+msg);
        }

        public String getSaySomething() {
                return saySomething;
        }

        public void setSaySomething(String saySomething) {
                this.saySomething = saySomething;
        }

        public String getMsg() {
                return msg;
        }

        public void setMsg(String msg) {
                this.msg = msg;
        }
}
 
三、写JMX的代理进程类
 
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;

import com.sun.jdmk.comm.HtmlAdaptorServer;

/**
* JMX代理进程
* Agent只是一个Java进程,它包括这个MBeanServer和一系列附加的MbeanService。
* 当然这些Service也是通过MBean的形式来发布。
*
* @author Administrator 2010-12-6 18:02:26
*/

public class HelloAgent {
        public static void main(String[] args) throws Exception {
                //创建一个MBeanServer,用来做MBean的容器
                MBeanServer server = MBeanServerFactory.createMBeanServer();
                //创建一个ObjectName类的对象,表示MBean的对象名,此类的实例是不可变的。
                // 对象名的字符串表示形式格式为“域名:name=MBean名称”
                ObjectName helloName = new ObjectName("domainName:name=Hello");
                //将Hello这个类的对象注册到MBeanServer中
                server.registerMBean(new Hello(), helloName);

                //创建一个名为htmladapter的Adapter的ObjectName对象
                ObjectName adapterName = new ObjectName("HelloAgent:name=htmladapter,port=6666");
                //创建一个AdaptorServer,这个类将决定MBean的管理界面,这里用最普通的Html型界面
                //AdaptorServer其实也是一个MBean。
                HtmlAdaptorServer adapter = new HtmlAdaptorServer();
                server.registerMBean(adapter, adapterName);
                //设置adapter的访问端口
                adapter.setPort(6667);
                //启动代理进程
                adapter.start();
                System.out.println("start.....");
        }
}
 
四、启动代理进程,通过页面调用MBean方法