- 浏览: 148131 次
- 性别:
- 来自: 北京
最新评论
-
pandengzhegt:
好牛!正需要!谢谢了!
JPA 2.0 中的动态类型安全查询 -
yanlp:
万分的感谢。
仿google 的输入提示框 -
huangwenji6111:
可谓良师,在此拜谢!受益匪浅!
hibernate lazy -
jwx0925:
不错!mark!
hibernate对象状态 -
leftstick:
大有裨益,谢了!
hibernate lazy
一、前言
动态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还是用得比较少,所谓利器深藏之而不用,非常时方现光芒。
动态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还是用得比较少,所谓利器深藏之而不用,非常时方现光芒。
发表评论
-
java实现Tree
2012-07-10 09:59 836/****************************** ... -
Java正则表达式应用总结
2012-05-25 12:23 1041一、概述 正则表达式是Java处理字符串、文本的重要工具。 ... -
JdbcTemplate与事务
2012-05-17 17:42 1090JdbcTemplate与事务 上例中的JdbcTemplat ... -
Java编程中“为了性能”尽量要做到的一些地方
2012-05-09 17:59 886最近的机器内存又爆满 ... -
jconsole远程监控Java进程
2012-05-07 11:44 1049JDK中的工具jconsole可以很好地监控Java进程及其运 ... -
spring集成quartz
2012-04-16 15:56 2188首先,让spring框架运转起来,可以参看一下:ht ... -
JMX RMI 访问
2011-09-02 10:46 4423RMI(Remote Method Invocation) R ... -
采用开发框架quartz调度管理Job
2011-07-11 10:03 19201.所需要的第三方包:quartz-1.5.2.jarcom ... -
java类型转换
2011-05-20 17:13 881string和int之间的转换? 字符串转换成数据 ... -
java整型数与网络字节序的 byte[] 数组转换关系
2011-05-05 10:47 3744因工作需要在java和c/c++之间进行socket通信,而 ... -
线程安全总结(二)
2010-11-12 10:34 825站内很多人都问我,所谓线程的“工作内存”到底是个什么东西? ... -
java线程安全总结
2010-11-12 10:33 799java线程安全总结(二 ... -
ora-02289问题解决
2010-10-19 12:35 1599<id name="id" type ... -
JDBC的批处理操作三种方式 pstmt.addBatch();
2010-09-25 15:58 8643SQL批处理是JDBC性能优化的重要武器,经本人研究总结,批处 ... -
log4j输出多个自定义日志文件
2010-05-12 10:28 1483<转>http://hi.baidu.com/ ... -
spring任务调度
2010-04-28 09:48 1379概述 在JDK 1.3以后的版本中,Java通过java.ut ... -
JDK线程池的使用
2010-04-07 16:35 1413一、简介 线程池类为 j ... -
Java文件操作
2010-02-06 15:29 847本文汇集常用文件操作方法,包括文件的建立/检查与删除,目录的建 ... -
[JMX一步步来] 6、Model Bean
2009-12-21 11:46 1171在上一节是用apache的commons-modeler来 ... -
[JMX一步步来] 5、用Apache的commons-modeler来辅助开发JMX
2009-12-21 11:45 1052一、前言 每一个MBean都要有一个接口,比如前面的Hello ...
相关推荐
JMX一步步来JMX一步步来JMX一步步来JMX一步步来
jmx快速上手 jmx快速上手 jmx快速上手 jmx快速上手
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 实例 rmi mbean,采用rmi方式进行jmx的mbean管理,通过这个实例能够了解jmx的应用
NULL 博文链接:https://jonerxq.iteye.com/blog/1990872
MBean是一个被管理的Java对象,就像Javabean组件一样,但是它遵从JMX规范的设计模式。MBean可以表示设备、应用或者任何需要被管理的资源。MBeans暴露如下管理接口:1.一组可读和可写属性,或者两者兼而有之。2.一组...
配置文件是一个 JSON 结构,它定义了 JMX 连接、要收集的 MBean 属性以及边界度量定义。 它需要指定边界 api 字符串 email:api-key。 需要边界高级帐户以及 JMX 的工作知识和边界指标 API 的理解: 使用说明 编辑 ...
JMX是指Java管理扩展,通过jmx可以监控java应用服务器的各项性能指标,jvm的性能指标等
Java分布式应用学习笔记09JMX-MBean的介绍
最新的hdfs namenode主备安装文档,详细,命令只需要copy执行即可
JMX 1.2.1 Reference Implementation(重点) Include com.sun.jdmk.comm.HtmlAdaptorServer class http://www.sun.com/software/jdmk/ Downloads, sun-jdmk-runtime-5.1-b34.2.zip/SUNWjdmk/5.1/lib/jdmkrt.jar
NULL 博文链接:https://jasonhan-sh-hotmail-com.iteye.com/blog/1171904
jmx-mbeans 创建了自定义Mbean类,以接收一些简单操作(如求和操作)的度量。 创建了MBeanServer并注册了自定义Mbean类。 使用jconsole工具连接到我的MBeanServer。 在jconsole中,能够查看自定义Mbean属性,...
Could not resolve dependencies for project ****:jar:0.0.1-SNAPSHOT: The following artifacts could not be resolved: com.sun.jdmk:jmxtools:jar:1.2.1, com.sun.jmx:jmxri:jar:1.2.1: Could not transfer ...
Commons-JMX 实用程序库,可帮助开发通过JMX监视的应用程序。 特征 MBeans帮助程序使您可以轻松注册/注销MBean。 使用构建器可以轻松透明地自定义MBean的名称。 装饰器可以通过JMX轻松显示您的集合状态(地图,...
用于JMX性能的性能测试套件这是一个简单的Web应用程序,它在上下文根目录/jolokia/下公开Jolokia代理,并注册一个MBean以便在JMX子系统上执行性能测量可用测试所有测试都通过在MBean jolokia:type=Performance,name=...
基于Spring+JMX+Tomcat实现资源动态管理
Could not resolve dependencies for project ****:jar:0.0.1-SNAPSHOT: The following artifacts could not be resolved: com.sun.jdmk:jmxtools:jar:1.2.1, com.sun.jmx:jmxri:jar:1.2.1: Could not transfer ...
JMX到Prometheus导出器:一个收集器,该收集器可以可配置地抓取和公开JMX目标的mBean。 该导出程序旨在作为Java代理运行,公开HTTP服务器并提供本地JVM的度量。 它也可以作为独立的HTTP服务器运行,并刮擦远程JMX...
JMX以RMI方式连接的场景示例,从“服务器A远程获取服务器B的历史IP访问记录”这一场景出发,将JMX框架的各层简单的串了一遍。其中包括了MBean、Agent、Client每个层的详细代码和说明