从零开始玩转JMX(四)——Apache Commons Modeler & Dynamic MBean


欢迎支持笔者新作:《深入理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。


欢迎跳转到本文的原文链接:https://honeypps.com/java/jmx-quick-start-4-dynamic-mbean/

Apache Commons Modeler

前面的Model MBean的创建方式看上去特别复杂,一个简单功能的类ModelMBeanUtils 写了很多代码,那有木有简单点的方式呢,答案是肯定的,这里就引出了Apache Commons Modeler(使用这个需要在classpath中导入commons-modeler-2.0.1.jar以及modeler的依赖项目commons-logging-1.1.3.jar,下载地址:http://commons.apache.org/proper/commons-modeler/download_modeler.cgi),使用Apache的Moleler库创建Model MBean最大帮助是,我们不需要再写复杂的代码来创建ModelMBeanInfo对象了。只需要一个MBean描述符(实际上就是一个xml配置文件,Apache Commons Modeler将ModelMBeanUtils 复杂的创建过程转移到xml中来配置,然后自身模块创建对象代替ModelMBeanUtils 的功能,简化用户的操作)来对Model MBean进行描述,就可以轻松的创建Model MBean.

下面来讲前面的Hello.java和HelloAgent.java的例子采用Apache Commons Modele进行改造。

首先还是Hello.java,和Model MBean中的一样,没有implements任何接口。

package com.test.jmx.modeler;

public class Hello{
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name= name;
    }

    public void printHello() {
        System.out.println("Hello World, "+name);
    }

    public void printHello(String whoName) {
        System.out.println("Hello, "+whoName);
    }
}

接下去就是最关键的描述文件(mbeans-descriptors.xml)了:

<?xml version="1.0" encoding="UTF-8" ?>
<mbeans-descriptors>
    <mbean name="Hello" description="the hello bean" domain="MyMBean" group="helloGroup" type="com.test.jmx.modeler.Hello">
        <attribute name="name" description="a name attribute" type="java.lang.String" writeable="true"/>
        <operation name="printHello" description="public void printHello()" impact="ACTION" returnType="void"/>
        <operation name="printHello" description="public void printHello(String whoName)" impact="ACTION" returnType="void">
        	<parameter name="whoName" description="method parameter of printHello" type="java.lang.String"></parameter> 
        </operation>
    </mbean>
</mbeans-descriptors>

描述文件的名字可以随意,最主要的是要和下面的HelloAgent.java对应起来。
通过这个xml文件的定义就描述了Model MBean所需要的metadata信息和一个基本的ModelMBean实现。
关于这个xml文件有几个需要说明的地方:

<mbean>的属性classname,name,type:

  • name属性是每个MBean被Registry对象注册的对象名
  • type属性是真正被管理资源的全面(包括包名)
  • classname属性是用户扩展的用于实现代理功能的Model MBean的全名,如果不提供Modeler会使用BaseModelMBean;如果提供了代理的ModelMBean对象,在使用时可以使用如下的代码样本访问他所代理的资源对象。

其余的标签就比较好理解了。综述:上面所示代码声明了一个Model MBean, 唯一标示是“Hello”,该MBean负责管理的对象是com.test.jmx.modeler.Hello的实例。域是MyMBean。这个MBean暴露了一个属性name和两个方法printHello()和printHello(String whoName).

下面是新的HelloAgent.java的代码:

package com.test.jmx.modeler;

import com.sun.jdmk.comm.HtmlAdaptorServer;
import org.apache.commons.modeler.ManagedBean;
import org.apache.commons.modeler.Registry;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.modelmbean.ModelMBean;
import java.io.InputStream;

public class HelloAgent  {
    public static void main(String[] args) throws Exception {
// 需要将xml信息读入到Registry对象中
        Registry registry = Registry.getRegistry(null,null);
        InputStream stream = HelloAgent.class.getResourceAsStream("mbeans-descriptors.xml");
        registry.loadMetadata(stream);
        MBeanServer server = registry.getMBeanServer();

// 之前是:MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        ManagedBean managed = registry.findManagedBean("Hello");
        ObjectName helloName = new ObjectName(managed.getDomain()+":name=HelloWorld");
// 以前是Hello hello = new Hello(); 为什么要多个createMBean?因为现在的写法没有写MBean,所以才要动态生成一个,以前就直接
// 把new hello()注册到MBeanServer就可以了,server会自动找到它的HelloMBean接口文件。
        ModelMBean hello = managed.createMBean(new Hello());
        server.registerMBean(hello,helloName);

        ObjectName adapterName = new ObjectName(managed.getDomain()+":name = htmladapter,port=8082");
        HtmlAdaptorServer adapter = new HtmlAdaptorServer();
        server.registerMBean(adapter,adapterName);
        adapter.start();
    }
}

注意这里的Registry是指org.apache.commons.modeler.Registry,因为JMX自身也有一个Registry(java.rmi.registry.Registry)。通过Modeler组件提供的Registry对象,可以很方便的完成MBeanServer的创建。
运行效果和之前的一样,这里就不赘述了,有兴趣的小伙伴可以试一下。

Dynamic MBean

四种类型的MBean,前面所讲的都是常用的,现在还剩两种Open MBean就不讲述了,这里简单记录下Dynamic MBean。
Dynamic MBean不需要自定义MBean接口,只需要实现DynamicMBean接口即可,Dynamic MBean没有任何明显些在代码里的属性和方法,所有的属性和方法都是通过反射结合JMX提供的辅助元数据从而动态生成。
下面的代码中首先定义了一个属性name和一个方法print,之后在管理界面(localhost:8082)中点击print之后生成一个print1的方法。
Dynamic MBean的代码如下:

package com.test.jmx.DynamicMBean;

import javax.management.*;
import java.lang.reflect.Constructor;
import java.util.Iterator;

/**
 * Created by hidden on 2016/10/9.
 */
public class HelloDynamic implements DynamicMBean {

    private String name;
    private MBeanInfo mBeanInfo = null;
    private String className;
    private String description;
    private MBeanAttributeInfo[] attributes;
    private MBeanConstructorInfo[] constructors;
    private MBeanOperationInfo[] operations;
    MBeanNotificationInfo[] mBeanNotificationInfoArray;

    private void init(){
        className = this.getClass().getName();
        description = "Simple implementation of a dynamic MBean.";
        attributes = new MBeanAttributeInfo[1];
        constructors = new MBeanConstructorInfo[1];
        operations = new MBeanOperationInfo[1];
        mBeanNotificationInfoArray = new MBeanNotificationInfo[0];
    }

    private void buildDynamicMBean(){
        Constructor[] thisConstructors = this.getClass().getConstructors();
        constructors[0] = new MBeanConstructorInfo("HelloDynamic(): Constructs a HelloDynamic Object",thisConstructors[0]);

        attributes[0] = new MBeanAttributeInfo("name","java.lang.String","Name:name string.",true,true,false);

        MBeanParameterInfo[] params = null;
        operations[0] = new MBeanOperationInfo("print","print():print the name",params,"void",MBeanOperationInfo.INFO);
        mBeanInfo = new MBeanInfo(className,description,attributes,constructors,operations,mBeanNotificationInfoArray);
    }

    public HelloDynamic(){
        init();
        buildDynamicMBean();
    }

    private void dynamicAddOperation(){
        init();
        operations = new MBeanOperationInfo[2];
        buildDynamicMBean();
        operations[1] = new MBeanOperationInfo("print1","print1():print the name",null,"void",MBeanOperationInfo.INFO);
        mBeanInfo = new MBeanInfo(className,description,attributes,constructors,operations,mBeanNotificationInfoArray);
    }

    @Override
    public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
        if (attribute == null) {
            return null;
        }
        if (attribute.equals("Name")) {
            return name;
        }

        return null;
    }

    @Override
    public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
        if (attribute == null) {
            return;
        }

        String Name = attribute.getName();
        Object value = attribute.getValue();

        try {
            if (Name.equals("Name")) {
                if (value == null) {
                    name=null;
                } else if (Class.forName("java.lang.String").isAssignableFrom(value.getClass())) {
                    name = (String) name;
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public AttributeList getAttributes(String[] attributes) {
        if (attributes == null) {
            return null;
        }
        AttributeList resultList = new AttributeList();
//        if (attributes.length == 0) {
//            return resultList;
//        }
        for(int i=0;i<attributes.length;i++){
            try {
                Object value = getAttribute(attributes[i]);
                resultList.add(new Attribute(attributes[i],value));
            } catch (AttributeNotFoundException e) {
                e.printStackTrace();
            } catch (MBeanException e) {
                e.printStackTrace();
            } catch (ReflectionException e) {
                e.printStackTrace();
            }
        }

        return resultList;
    }

    @Override
    public AttributeList setAttributes(AttributeList attributes) {
        if (attributes == null) {
            return null;
        }
        AttributeList resultList = new AttributeList();
        if(attributes.isEmpty()){
            return resultList;
        }
        for(Iterator i = attributes.iterator();i.hasNext();){
            Attribute attr = (Attribute) i.next();
            try {
                setAttribute(attr);
                String name = attr.getName();
                Object value = getAttribute(name);
                resultList.add(new Attribute(name,value));
            } catch (AttributeNotFoundException e) {
                e.printStackTrace();
            } catch (InvalidAttributeValueException e) {
                e.printStackTrace();
            } catch (MBeanException e) {
                e.printStackTrace();
            } catch (ReflectionException e) {
                e.printStackTrace();
            }

        }

        return resultList;
    }

    @Override
    public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException {
        if(actionName.equals("print")){
            System.out.println("Hello, "+name+",this is HelloDynamic!");
            dynamicAddOperation();
            return null;
        }else if(actionName.equals("print1")){
            System.out.println("这是动态增加的一个方法print1");
            return null;
        }else {
            throw new ReflectionException(new NoSuchMethodException(actionName),"Cannot find the operation "+actionName+" in "+className);
        }
    }

    @Override
    public MBeanInfo getMBeanInfo() {
        return mBeanInfo;
    }
}

通过Agent调用Dynamic MBean:

package com.test.jmx.DynamicMBean;

import com.sun.jdmk.comm.HtmlAdaptorServer;

import javax.management.*;
import java.lang.management.ManagementFactory;
import java.util.concurrent.TimeUnit;

public class HelloAgent {
    public static void main(String[] args) throws MalformedObjectNameException, NotCompliantMBeanException, InstanceAlreadyExistsException, MBeanRegistrationException {
        
    	MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        ObjectName helloName = new ObjectName("MyMBean:name=helloDynamic");
        HelloDynamic hello = new HelloDynamic();
        server.registerMBean(hello,helloName);

        ObjectName adapterName = new ObjectName("MyMBean:name=htmladapter");
        HtmlAdaptorServer adapter = new HtmlAdaptorServer();
        server.registerMBean(adapter,adapterName);
        adapter.start();
    }
}

运行效果图如下:

这里写图片描述

运行结果:

Hello, null,this is HelloDynamic!
这是动态增加的一个方法print1

wanna more?

  1. 从零开始玩转JMX(一)——简介和Standard MBean
  2. 从零开始玩转JMX(二)——Condition
  3. 从零开始玩转JMX(三)——Model MBean
  4. 从零开始玩转JMX(四)——Apache Commons Modeler & Dynamic MBean

参考资料

  1. JMX整理
  2. JMX简介
  3. http://blog.csdn.net/DryKillLogic/article/category/762777
  4. 用Apache的commons-modeler来辅助开发JMX

欢迎跳转到本文的原文链接:https://honeypps.com/java/jmx-quick-start-4-dynamic-mbean/


欢迎支持笔者新作:《深入理解Kafka:核心设计与实践原理》和《RabbitMQ实战指南》,同时欢迎关注笔者的微信公众号:朱小厮的博客。


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
JMX(Java Management Extensions)是Java平台的一个管理和监控API,可以让开发人员开发Mbean(Managed Bean)来管理和监控Java应用程序。Mbean是一种Java对象,通过JMX可以公开其属性和方法,以便对其进行管理和监控。 下面是一个简单的示例,演示如何使用JMX开发一个Mbean: 1. 定义一个实现了DynamicMBean接口的类,该类表示一个Mbean: ```java public class SimpleMBean implements DynamicMBean { private String name; public SimpleMBean(String name) { this.name = name; } @Override public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException { if (attribute.equals("Name")) { return name; } else { throw new AttributeNotFoundException(attribute); } } @Override public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { throw new AttributeNotFoundException(attribute.getName()); } @Override public AttributeList getAttributes(String[] attributes) { AttributeList result = new AttributeList(); for (String attr : attributes) { try { result.add(new Attribute(attr, getAttribute(attr))); } catch (Exception ex) { // ignore } } return result; } @Override public AttributeList setAttributes(AttributeList attributes) { return new AttributeList(); } @Override public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException { throw new ReflectionException(new NoSuchMethodException(actionName)); } @Override public MBeanInfo getMBeanInfo() { MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[] { new MBeanAttributeInfo("Name", "java.lang.String", "name", true, false, false) }; MBeanConstructorInfo[] ctors = new MBeanConstructorInfo[] { new MBeanConstructorInfo("SimpleMBean()", "Default constructor", new MBeanParameterInfo[0]) }; return new MBeanInfo(getClass().getName(), "SimpleMBean", attrs, ctors, null, null); } } ``` 2. 创建一个MBeanServer对象,并将Mbean注册到MBeanServer中: ```java MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName("com.example:type=SimpleMBean"); SimpleMBean mbean = new SimpleMBean("hello"); mbs.registerMBean(mbean, name); ``` 3. 使用JConsole或其他JMX客户端连接到MBeanServer,可以查看和修改Mbean的属性。 这是一个非常简单的示例,实际上,Mbean可以包含更复杂的逻辑和更多的属性。Mbean也可以使用注解来简化Mbean的开发和管理。JMX是一个强大的工具,可以帮助开发人员监控和管理Java应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值