- 浏览: 371996 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
ytlviv:
利器深藏之而不用,非常时方现光芒
[JMX一步步来] 4、动态MBean:DynamicMBean -
fantaxy025025:
总结的不错!搜藏了。原有的链接里面内容css有问题。辛苦楼主! ...
[JMX一步步来] 1、JMX的Hello World(转) -
xuyb_0314:
非常不错~~
查看ORACLE表空间使用情况的SQL语句 -
elan1986:
谢谢 在你这里找到了
在winxp下使用bat文件运行java程序而不弹出dos窗口 -
lqw8668:
...
XP SP2下安装Rose 出现 “MEM_BAD_POINTER”错误的解决方法
一、前言
动态MBean是在运行期才定义它的属性和方法,也就是说它有什么属性和方法是可以动态改变的。动态MBean主要利用一 些辅助类(构造函数类MBeanConstructorInfo、属性类MBeanAttributeInfo、方法类 MBeanOperationInfo)来完成这个功能,所有的动态MBean必须实现DynamicMBean接口。DynamicMBean写好后, 使用方法和第一篇文章中普通的MBean一样。
给出一个动态MBean的实例,这个实例最初动态构了一个Name属性及一个print方法,当我们执行它的print方法之后,又给此MBean新增了一个print1方法。实例的代码如下:
二、实例
1、HelloDynamic类
说明:
* 实现于接口DynamicMBean
* 借助于各种辅助类完成一个类的构造。构造函数类MBeanConstructorInfo、属性类MBeanAttributeInfo、方法类MBeanOperationInfo
* 这里所有public方法是实现于DynamicMBean的。主要提供:setAttribute设置属性、 getAttribute取得属性、setAttributes设置一组属性、getAttributes取得一组属性、invoke方法调用、 getMBeanInfo MBeanServer由这个方法得到关键的MBean类的构造信息。
2、HelloAgent类
前面说了HelloDynamic和普通MBean的使用方法是一样的,因此HelloAgent和第一篇的HelloAgent基本一样,就是把Hello改成HelloDynamic而已。为了实例完整,也一并帖出来吧。
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类
- import java.lang.reflect.Constructor;
- import java.util.Iterator;
- import javax.management.Attribute;
- import javax.management.AttributeList;
- import javax.management.DynamicMBean;
- import javax.management.MBeanAttributeInfo;
- import javax.management.MBeanConstructorInfo;
- import javax.management.MBeanException;
- import javax.management.MBeanInfo;
- import javax.management.MBeanNotificationInfo;
- import javax.management.MBeanOperationInfo;
- import javax.management.MBeanParameterInfo;
- import javax.management.ReflectionException;
- /**
- * @author Sunny Peng
- * @author change by Chen.Gang, add a feature for dynamic add operation
- * @version 1.0
- */
- 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;
- public HelloDynamic() {
- init();
- buildDynamicMBean();
- }
- 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);
- //operate method 我们的操作方法是print
- 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);
- }
- //动态增加一个print1方法
- 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);
- }
- public Object getAttribute(String attribute_name) {
- if (attribute_name != null)
- return null;
- if (attribute_name.equals("Name"))
- return name;
- return null;
- }
- public void setAttribute(Attribute attribute) {
- if (attribute == null)
- return;
- String Name = attribute.getName();
- Object value = attribute.getValue();
- try {
- if (Name.equals("Name")) {
- // if null value, try and see if the setter returns any exception
- if (value == null) {
- name = null;
- // if non null value, make sure it is assignable to the attribute
- } else if ((Class.forName("java.lang.String")).isAssignableFrom(value.getClass())) {
- name = (String) value;
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public AttributeList getAttributes(String[] attributeNames) {
- if (attributeNames == null)
- return null;
- AttributeList resultList = new AttributeList();
- // if attributeNames is empty, return an empty result list
- if (attributeNames.length == 0)
- return resultList;
- for (int i = 0; i < attributeNames.length; i++) {
- try {
- Object value = getAttribute(attributeNames[i]);
- resultList.add(new Attribute(attributeNames[i], value));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return resultList;
- }
- public AttributeList setAttributes(AttributeList attributes) {
- if (attributes == null)
- return null;
- AttributeList resultList = new AttributeList();
- // if attributeNames is empty, nothing more to do
- if (attributes.isEmpty())
- return resultList;
- // for each attribute, try to set it and add to the result list if successfull
- 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 (Exception e) {
- e.printStackTrace();
- }
- }
- return resultList;
- }
- public Object invoke(String operationName, Object params[], String signature[]) throws MBeanException, ReflectionException {
- // Check for a recognized operation name and call the corresponding operation
- if (operationName.equals("print")) {
- //具体实现我们的操作方法print
- System.out.println("Hello, " + name + ", this is HellDynamic!");
- dynamicAddOperation();
- return null;
- } else if (operationName.equals("print1")) {
- System.out.println("这是动态增加的一方法print1");
- return null;
- } else {
- // unrecognized operation name:
- throw new ReflectionException(new NoSuchMethodException(operationName), "Cannot find the operation " + operationName + " in " + className);
- }
- }
- public MBeanInfo getMBeanInfo() {
- return mBeanInfo;
- }
- }
* 实现于接口DynamicMBean
* 借助于各种辅助类完成一个类的构造。构造函数类MBeanConstructorInfo、属性类MBeanAttributeInfo、方法类MBeanOperationInfo
* 这里所有public方法是实现于DynamicMBean的。主要提供:setAttribute设置属性、 getAttribute取得属性、setAttributes设置一组属性、getAttributes取得一组属性、invoke方法调用、 getMBeanInfo MBeanServer由这个方法得到关键的MBean类的构造信息。
2、HelloAgent类
前面说了HelloDynamic和普通MBean的使用方法是一样的,因此HelloAgent和第一篇的HelloAgent基本一样,就是把Hello改成HelloDynamic而已。为了实例完整,也一并帖出来吧。
- import javax.management.MBeanServerFactory;
- import javax.management.ObjectName;
- import com.sun.jdmk.comm.HtmlAdaptorServer;
- public class HelloAgent {
- public static void main(String[] args) throws Exception {
- MBeanServer server = MBeanServerFactory.createMBeanServer();
- ObjectName helloName = new ObjectName("chengang:name=HelloDynamic");
- HelloDynamic hello = new HelloDynamic();
- server.registerMBean(hello, helloName);
- ObjectName adapterName = new ObjectName("HelloAgent:name=htmladapter,port=8082");
- HtmlAdaptorServer adapter = new HtmlAdaptorServer();
- server.registerMBean(adapter, adapterName);
- adapter.start();
- System.out.println("start.....");
- }
- }
3、运行
先运行HelloAgent。再打开浏览器,输入网址:http://localhost:8082/。单击进入“name=HelloDynamic ”项,执行print方法后再回到上一页面你会发现又多了一个print1方法。
4、总结
动态MBean的代码稍显复杂,但对于一些特殊需求的情况,它将显示出强大威力。而且它还是模型MBeans(Model MBeans)的基础。不过在一般的项目中,动态MBean还是用得比较少,所谓利器深藏之而不用,非常时方现光芒。
发表评论
-
web.xml里<filter-mapping>中的<dispatcher>作用
2009-12-08 17:20 11165在2.4版本的servlet规范在部属描述符中新增加 ... -
(转)Taglib部署的三种方式
2009-12-04 13:07 0本周,由于项目的需要引入了jstl,但是在jsp页 ... -
Iframe页面请求跳转问题解决
2009-11-04 21:15 5141问题描述: 在父页面的一个Iframe页面中,提交请求后 ... -
JAF简介
2009-11-01 21:05 2511JAF全称是JavaBeans ... -
是 String , StringBuffer 还是 StringBuilder ?
2009-10-18 15:42 1377相信大家对 String 和 StringBuffer 的区别 ... -
选择Java接口还是抽象类
2009-10-17 16:23 3455关于java接口与 ... -
Java接口与Java抽象类的区别
2009-10-17 16:01 1372Java接口与Java抽象类的区别: 1. Java抽 ... -
为何不能通过返回值来区分重载方法?
2009-06-28 16:20 3012在java中,重载是其一个非常重要的特性。使用重载,我 ... -
怎么处理警告:编码 GBK 的不可映射字符
2009-04-17 01:31 3033在使用ant编译代码时提示 “编码 GBK 的不可映射字 ... -
既然认为它是好的,就要发挥到极限-系列之二单元测试
2009-03-29 17:07 917(原文)http://www.blogjava.net ... -
weblogic10 下对jVM性能调优设置(参考资料)
2009-03-25 12:36 2867对 JRockit JVM优化 Ta ... -
lazy initialization实现Singleton时synchronized的必要性
2009-03-19 21:48 2435首先了解一下Singleton模式通常的两种表现形式: 第一种 ... -
Guidelines, Patterns, and Code for End-to-End Java
2009-02-02 13:45 1035This document contains recomm ... -
poi即将提供对OOXML的支持
2008-11-06 23:18 9133POI是apache项目之一,最新版是3.2,它提 ... -
JSP在Servlet中的几个编码的作用及原理
2008-10-05 11:27 1585首先,说说JSP/Ser ... -
使用jconsole监控Apusic
2008-08-19 10:39 1564JDK 1.5+提供了jconsole工具,可以对JVM实例的 ... -
jconsole+tomcat配置说明(基于jdk5.0)
2008-08-18 23:18 8509最近需要参与一些java程 ... -
CruiseControl配置文档元素详解
2008-07-31 22:16 0<cruisecontrol> & ... -
抽象类和接口的区别
2008-07-26 20:19 1014在Java语言中, abstract class ... -
十个最好的Java性能故障排除工具
2008-07-21 13:00 1417推荐十个最好的Java性能故障排除工具: 1.jconsole ...
相关推荐
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每个层的详细代码和说明