自动动手写原生态jmx,MbeanInfo,invoke

 

使用jmx的东东主要包括下面几个步骤了: 
发布jmxServer。 

注册jmxBean到MBeanServer上。 
使用连接jmx的client工具来连接jmxServer。 

一:前言 
MBeanServer以及连接MBean的Client多的数不清。这里我就挑选一种经常会用到的方式来, 
本片文章主要还是为下篇 “JBOSS里使用jmx来进行监控” 做好铺垫。 
MBeanServer就采用jdk自带的MBeanServer来实现。 
client采用jconsole来连接,其它的client原理都类似。 
只有server上有一些些区别。 

二:发布一个jmxServer 

MBeanServer server = ManagementFactory.getPlatformMBeanServer(); 

因为要采用修改vm参数的方式来运行MBeanServer所以传统的MBeanServerFactory.createMBeanServer();   
需要进行修改。 

 

 

 

三:创建自己的MBean,MBeanInfo并同时实现invoke,能够让jconsole里调用方法

 

 

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.DynamicMBean;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.ReflectionException;
import javax.management.modelmbean.ModelMBeanAttributeInfo;
import javax.management.modelmbean.ModelMBeanOperationInfo;

/**
 * @author guoliang
 * @version 创建时间:2010-11-13 下午11:22:05 类说明
 */

public class MyMBean implements DynamicMBean {

    private String testField = "initial field value";

    public String getTestField() {
        return testField;
    }

    public void setTestField(String testField) {
        this.testField = testField;
    }

    public String testMethod(String from) {
        System.out.println("hello world" + from);
        return "hello world" + from;
    }

    @Override
    public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
        /**
         * 属性操作在这里进行,invoke
         */
        Class cls = this.getClass();

        try {
            String methodName = getMethodNameByField(attribute);
            Method attributeGet = cls.getMethod("get" + methodName, null);
            return attributeGet.invoke(this, null);
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    private String getMethodNameByField(String attribute) {
        String methodName = attribute.substring(0, 1).toUpperCase() + attribute.substring(1, attribute.length());
        return methodName;
    }

    @Override
    public AttributeList getAttributes(String[] attributes) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public MBeanInfo getMBeanInfo() {
        // TODO Auto-generated method stub
        try {
            Class cls = this.getClass();

            Method attributeGet = cls.getMethod("getTestField", null);
            Method attributeSet = cls.getMethod("setTestField", new Class[] { String.class });

            ModelMBeanAttributeInfo attribute = new ModelMBeanAttributeInfo("testField", "description of field",
                    attributeGet, attributeSet);

            Method m = cls.getMethod("testMethod", new Class[] { String.class });

            /**
             * 添加一个mbean中的操作,用ModelMBeanOperationInfo来实现。
             */
            ModelMBeanOperationInfo testMethod = new ModelMBeanOperationInfo("testMethod", m);

            MBeanConstructorInfo mBeanConstructorInfo = new MBeanConstructorInfo("Constructor for ServerMonitor", cls
                    .getConstructor(null));

            MBeanInfo info = new MBeanInfo(cls.getName(), "my mbean server", new MBeanAttributeInfo[] { attribute },
                    new MBeanConstructorInfo[] { mBeanConstructorInfo }, new MBeanOperationInfo[] { testMethod }, null);

            System.out.println(info.getClassName() + this.hashCode());
            return info;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException,
            ReflectionException {
        /**
         * 这里用来处理jconsole里点击方法按钮调用后的操作,反射invoke过来。
         */

        System.out.println("invoking...." + actionName + "[]" + Arrays.toString(params));

        Class[] paraClass = new Class[params.length];

        for (int i = 0; i < params.length; i++) {
            paraClass[i] = params[i].getClass();
        }

        try {
            Method m = this.getClass().getMethod(actionName, paraClass);
            return m.invoke(this, params);
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException,
            MBeanException, ReflectionException {
        /**
         * 属性操作在这里进行,invoke
         */
        Class cls = this.getClass();

        try {
            String methodName = getMethodNameByField(attribute.getName());
            Method attributeSet = cls.getMethod("set" + methodName, new Class[] { attribute.getValue()
                    .getClass() });
            attributeSet.invoke(this, attribute.getValue());
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public AttributeList setAttributes(AttributeList attributes) {
        return null;
    }

}
 



四:注册MBean:

 

 

public class MyMBeanServer { 

    public static void main(String[] args) {
//        MBeanServer server = MBeanServerFactory.createMBeanServer();
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        try {
            ObjectName helloName = new ObjectName("beans:name=MyMbean");
            server.registerMBean(new MyMBean(), helloName);

            Thread.sleep(Integer.MAX_VALUE);
        } catch (InstanceAlreadyExistsException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MBeanRegistrationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NotCompliantMBeanException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedObjectNameException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NullPointerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
 

 

五:运行本程序。 

要想在jconsole里能看到本MBeanServer,还需要在启动的run里面加些参数,eclipse里面的为: 


自动动手写原生态jmx,MbeanInfo,invoke
 

启动过后,cmd命令行启动 jconsole 
选中本地的Main函数。 
在mbean栏目可以看到已经发布的MBean。 


自动动手写原生态jmx,MbeanInfo,invoke


ok!比较简单的了解下。


原生态的果然比较麻烦。不这样搞我都不知道内部是个啥东东。 

 

 

 

 

 

 

 

你可能感兴趣的:(eclipse,jdk,thread,jboss)