`
zhengxuezhou
  • 浏览: 152683 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

[JMX一步步来] 4、动态MBean:DynamicMBean

    博客分类:
  • j2se
阅读更多
一、前言
    动态MBean是在运行期才定义它的属性和方法,也就是说它有什么属性和方法是可以动态改变的。动态MBean主要利用一 些辅助类(构造函数类MBeanConstructorInfo、属性类MBeanAttributeInfo、方法类 MBeanOperationInfo)来完成这个功能,所有的动态MBean必须实现DynamicMBean接口。DynamicMBean写好后, 使用方法和第一篇文章中普通的MBean一样。

    给出一个动态MBean的实例,这个实例最初动态构了一个Name属性及一个print方法,当我们执行它的print方法之后,又给此MBean新增了一个print1方法。实例的代码如下:

二、实例
1、HelloDynamic类
1.import java.lang.reflect.Constructor; 
2.import java.util.Iterator; 
3.import javax.management.Attribute; 
4.import javax.management.AttributeList; 
5.import javax.management.DynamicMBean; 
6.import javax.management.MBeanAttributeInfo; 
7.import javax.management.MBeanConstructorInfo; 
8.import javax.management.MBeanException; 
9.import javax.management.MBeanInfo; 
10.import javax.management.MBeanNotificationInfo; 
11.import javax.management.MBeanOperationInfo; 
12.import javax.management.MBeanParameterInfo; 
13.import javax.management.ReflectionException; 
14. 
15./**
16. * @author Sunny Peng
17. * @author change by Chen.Gang, add a feature for dynamic add operation
18. * @version 1.0
19. */ 
20.public class HelloDynamic implements DynamicMBean { 
21.    //这是我们的属性名称 
22.    private String name; 
23.    private MBeanInfo mBeanInfo = null; 
24.    private String className; 
25.    private String description; 
26.    private MBeanAttributeInfo[] attributes; 
27.    private MBeanConstructorInfo[] constructors; 
28.    private MBeanOperationInfo[] operations; 
29.    MBeanNotificationInfo[] mBeanNotificationInfoArray; 
30. 
31.    public HelloDynamic() { 
32.        init(); 
33.        buildDynamicMBean(); 
34.    } 
35. 
36.    private void init() { 
37.        className = this.getClass().getName(); 
38.        description = "Simple implementation of a dynamic MBean."; 
39.        attributes = new MBeanAttributeInfo[1]; 
40.        constructors = new MBeanConstructorInfo[1]; 
41.        operations = new MBeanOperationInfo[1]; 
42.        mBeanNotificationInfoArray = new MBeanNotificationInfo[0]; 
43.    } 
44. 
45.    private void buildDynamicMBean() { 
46.        //设定构造函数 
47.        Constructor[] thisconstructors = this.getClass().getConstructors(); 
48.        constructors[0] = new MBeanConstructorInfo("HelloDynamic(): Constructs a HelloDynamic object", thisconstructors[0]); 
49.        //设定一个属性 
50.        attributes[0] = new MBeanAttributeInfo("Name", "java.lang.String", "Name: name string.", true, true, false); 
51.        //operate method 我们的操作方法是print 
52.        MBeanParameterInfo[] params = null;//无参数 
53.        operations[0] = new MBeanOperationInfo("print", "print(): print the name", params, "void", MBeanOperationInfo.INFO); 
54.        mBeanInfo = new MBeanInfo(className, description, attributes, constructors, operations, mBeanNotificationInfoArray); 
55.    } 
56. 
57.    //动态增加一个print1方法 
58.    private void dynamicAddOperation() { 
59.        init(); 
60.        operations = new MBeanOperationInfo[2];//设定数组为两个 
61.        buildDynamicMBean(); 
62.        operations[1] = new MBeanOperationInfo("print1", "print1(): print the name", null, "void", MBeanOperationInfo.INFO); 
63.        mBeanInfo = new MBeanInfo(className, description, attributes, constructors, operations, mBeanNotificationInfoArray); 
64.    } 
65. 
66.    public Object getAttribute(String attribute_name) { 
67.        if (attribute_name != null) 
68.            return null; 
69.        if (attribute_name.equals("Name")) 
70.            return name; 
71.        return null; 
72.    } 
73. 
74.    public void setAttribute(Attribute attribute) { 
75.        if (attribute == null) 
76.            return; 
77.        String Name = attribute.getName(); 
78.        Object value = attribute.getValue(); 
79.        try { 
80.            if (Name.equals("Name")) { 
81.                // if null value, try and see if the setter returns any exception 
82.                if (value == null) { 
83.                    name = null; 
84.                    // if non null value, make sure it is assignable to the attribute 
85.                } else if ((Class.forName("java.lang.String")).isAssignableFrom(value.getClass())) { 
86.                    name = (String) value; 
87.                } 
88.            } 
89.        } catch (Exception e) { 
90.            e.printStackTrace(); 
91.        } 
92.    } 
93. 
94.    public AttributeList getAttributes(String[] attributeNames) { 
95.        if (attributeNames == null) 
96.            return null; 
97.        AttributeList resultList = new AttributeList(); 
98.        // if attributeNames is empty, return an empty result list 
99.        if (attributeNames.length == 0) 
100.            return resultList; 
101.        for (int i = 0; i < attributeNames.length; i++) { 
102.            try { 
103.                Object value = getAttribute(attributeNames[i]); 
104.                resultList.add(new Attribute(attributeNames[i], value)); 
105.            } catch (Exception e) { 
106.                e.printStackTrace(); 
107.            } 
108.        } 
109.        return resultList; 
110.    } 
111. 
112.    public AttributeList setAttributes(AttributeList attributes) { 
113.        if (attributes == null) 
114.            return null; 
115.        AttributeList resultList = new AttributeList(); 
116.        // if attributeNames is empty, nothing more to do 
117.        if (attributes.isEmpty()) 
118.            return resultList; 
119.        // for each attribute, try to set it and add to the result list if successfull 
120.        for (Iterator i = attributes.iterator(); i.hasNext();) { 
121.            Attribute attr = (Attribute) i.next(); 
122.            try { 
123.                setAttribute(attr); 
124.                String name = attr.getName(); 
125.                Object value = getAttribute(name); 
126.                resultList.add(new Attribute(name, value)); 
127.            } catch (Exception e) { 
128.                e.printStackTrace(); 
129.            } 
130.        } 
131.        return resultList; 
132.    } 
133. 
134.    public Object invoke(String operationName, Object params[], String signature[]) throws MBeanException, ReflectionException { 
135.        // Check for a recognized operation name and call the corresponding operation 
136.        if (operationName.equals("print")) { 
137.            //具体实现我们的操作方法print 
138.            System.out.println("Hello, " + name + ", this is HellDynamic!"); 
139.            dynamicAddOperation(); 
140.            return null; 
141.        } else if (operationName.equals("print1")) { 
142.            System.out.println("这是动态增加的一方法print1"); 
143.            return null; 
144.        } else { 
145.            // unrecognized operation name: 
146.            throw new ReflectionException(new NoSuchMethodException(operationName), "Cannot find the operation " + operationName + " in " + className); 
147.        } 
148. 
149.    } 
150. 
151.    public MBeanInfo getMBeanInfo() { 
152.        return mBeanInfo; 
153.    } 
154.} 
说明:
    * 实现于接口DynamicMBean
    * 借助于各种辅助类完成一个类的构造。构造函数类MBeanConstructorInfo、属性类MBeanAttributeInfo、方法类MBeanOperationInfo
    * 这里所有public方法是实现于DynamicMBean的。主要提供:setAttribute设置属性、 getAttribute取得属性、setAttributes设置一组属性、getAttributes取得一组属性、invoke方法调用、 getMBeanInfo MBeanServer由这个方法得到关键的MBean类的构造信息。

2、HelloAgent类
   前面说了HelloDynamic和普通MBean的使用方法是一样的,因此HelloAgent和第一篇的HelloAgent基本一样,就是把Hello改成HelloDynamic而已。为了实例完整,也一并帖出来吧。

1.import javax.management.MBeanServerFactory; 
2.import javax.management.ObjectName; 
3.import com.sun.jdmk.comm.HtmlAdaptorServer; 
4.public class HelloAgent { 
5.    public static void main(String[] args) throws Exception { 
6.        MBeanServer server = MBeanServerFactory.createMBeanServer(); 
7.        ObjectName helloName = new ObjectName("chengang:name=HelloDynamic"); 
8.        HelloDynamic hello = new HelloDynamic(); 
9.        server.registerMBean(hello, helloName); 
10.        ObjectName adapterName = new ObjectName("HelloAgent:name=htmladapter,port=8082"); 
11.        HtmlAdaptorServer adapter = new HtmlAdaptorServer(); 
12.        server.registerMBean(adapter, adapterName); 
13.        adapter.start(); 
14.        System.out.println("start....."); 
15.    } 
16.} 

3、运行
   先运行HelloAgent。再打开浏览器,输入网址:http://localhost:8082/。单击进入“name=HelloDynamic ”项,执行print方法后再回到上一页面你会发现又多了一个print1方法。

4、总结
   动态MBean的代码稍显复杂,但对于一些特殊需求的情况,它将显示出强大威力。而且它还是模型MBeans(Model MBeans)的基础。不过在一般的项目中,动态MBean还是用得比较少,所谓利器深藏之而不用,非常时方现光芒。
分享到:
评论

相关推荐

    jmx一步步来 jmx快速上手指南

    2. **监控与诊断**:JMX提供了一种机制来收集有关应用程序的运行时信息,包括但不限于内存使用情况、线程统计信息等,有助于开发者进行故障排查。 3. **自动化管理**:利用JMX,可以编写脚本或者工具来自动生成报告...

    面试官问我 JMX 了解不,我说:什么? - 知乎1

    JMX不仅用于JVM监控,还可以用于日志级别的动态修改,例如log4j支持通过JMX来动态调整日志级别。此外,许多监控工具,如Spring Boot Actuator、JConsole、VisualVM等,都利用了JMX来收集和展示应用的运行时信息。 ...

    JMX以RMI方式连接的场景示例

    JMX以RMI方式连接的场景示例 JMX(Java Management Extensions)是一种Java技术,用于管理...JMX框架提供了一种灵活、可扩展和高效的方式来管理和监控应用程序,而RMI连接方式允许远程客户端访问MBean提供的管理功能。

    JMX官方文档 - 概览,入门,规范

    Java Management Extensions(JMX)是Java平台上的一个标准技术,用于管理和监控应用程序、操作系统和网络设备等资源。在JDK1.6版本中,JMX已经成熟并被广泛使用。本篇文章将深入探讨JMX的基本概念、核心组件、功能...

    基于Spring+JMX+Tomcat实现资源动态管理

    总之,"基于Spring+JMX+Tomcat实现资源动态管理"是一个强大的技术组合,它为开发者提供了强大的工具来监控和调整应用程序和服务器的运行状态。通过了解和掌握这些技术,我们可以构建出更加灵活、可扩展且易于维护的...

    jmx 实例 rmi mbean

    在本实例中,我们重点关注的是如何使用Remote Method Invocation(RMI)来实现JMX的MBean管理。RMI是一种在Java平台上进行远程调用的技术,使得一个Java对象的方法可以在不同的Java虚拟机(JVM)之间被调用。结合JMX...

    亲测可用 com.sun.jmx. jmxri-1.2.1.jar

    Description Resource Path Location Type Missing artifact com.sun.jmx:jmxri:jar:1.2.1 pom.xml /eshop-storm line 2 Maven Dependency Problem

    JMX实用例子详解(包括各种Mbean)

    Java Management Extensions(JMX)是Java平台上的一个标准管理框架,它允许开发人员创建、注册和管理名为MBeans(Managed Beans)的对象,这些对象代表了系统、应用程序或服务的可管理资源。通过JMX,我们可以远程...

    JMX(一)-------MBean server

    总的来说,JMX和MBean Server提供了一种标准、灵活的管理方式,使得Java应用的管理和监控变得更加便捷。通过结合Spring框架,开发者可以更轻松地在应用中集成JMX功能,实现对应用的全方位管理。

    hellombean.zip_zip

    3. 开放MBean:开放MBean是一种特殊的动态MBean,它的属性和操作都遵循一种标准的数据类型,这使得它们可以被任何支持JMX的管理工具所识别和操作。 在JMX中,MBean的注册和管理通常由MBean服务器(MBeanServer)...

    jmxri-1.2.1.jar+jmxtools-1.2.1.jar

    Java Management Extensions (JMX) 是Java平台上的一个标准技术,用于管理和监控应用程序、操作系统和网络设备。`jmxri-1.2.1.jar` 和 `jmxtools-1.2.1.jar` 是与JMX相关的两个核心库文件,它们在Java应用程序中扮演...

    JMX IN ACTION(五)

    在本章"JMX IN ACTION(五)"中,我们将探讨如何使用动态MBean(Dynamic MBean)来管理那些经常变化或具有不确定接口的资源。动态MBean是针对那些管理接口需要在运行时自定义和调整的情况的理想选择。与标准MBean不同...

    jmx简单实例,附带jar包完整项目

    在这个“jmx简单实例”中,我们将会探讨JMX的基本概念,以及如何使用所提供的jar包来构建和运行一个简单的JMX示例。 1. **JMX基础** JMX由三部分组成:MBeans(Managed Beans)、Servers和Agents。MBeans代表管理...

    论文研究-基于JMX的网络管理系统.pdf

    4. 模型MBean:允许在运行时动态地添加、删除和修改属性和操作。 一个MBean实例通常与一个MBean服务器一起工作,MBean服务器作为MBean的容器,负责管理这些MBean实例的生命周期,以及管理这些MBean实例与外界(例如...

    jmx osgi 实例

    MBean可以是任何Java对象,它暴露了一组操作和属性,允许管理者通过JMX代理进行交互。MBeans可以通过MBeanServer注册,然后可以通过JMX连接器进行访问,实现对应用程序的监控和管理。 OSGi则是一个动态模块系统,它...

    使用jmx监控service接口

    NULL 博文链接:https://jonerxq.iteye.com/blog/1990872

    jmx 入门文档,附有开发实例文档

    2. **动态MBean**:动态MBean不强制实现特定的MBean接口,而是实现`DynamicMBean`接口,它提供了运行时动态暴露属性和操作的能力。 3. **模型MBean**:模型MBean通过元数据描述其属性、操作和通知,这样可以更灵活...

    Java管理扩展指南之MBean简介

    MBean是一个被管理的Java对象,就像Javabean组件一样,但是它遵从JMX规范的设计模式。MBean可以表示设备、应用或者任何需要被管理的资源。MBeans暴露如下管理接口:1.一组可读和可写属性,或者两者兼而有之。2.一组...

    jmxri.jar包

    - 使用MBeanserver:MBean服务器是JMX的核心,负责管理MBean并提供API来操作它们。 - 连接和交互:通过JMX连接器,客户端可以连接到MBean服务器,并通过查询、调用操作或设置属性来管理MBean。 在实际应用中,JMX...

    JMX学习——一步步来

    【JMX学习——一步步来】 JMX,全称为Java Management Extensions,是一个用于植入管理功能到Java应用程序的框架。它提供了一套标准的接口和服务,使得开发者可以在任何Java应用中实现管理和监控。对于初学者来说,...

Global site tag (gtag.js) - Google Analytics