- 浏览: 236404 次
- 性别:
- 来自: 北京
最新评论
-
Xgeeeeek:
讲的很细致,多谢~
JAVA RMI 实例 -
zontim:
...
JAVA RMI 实例 -
czl026:
简单明了,谢谢
[JMX一步步来] 1、JMX的Hello World -
manxisuo:
jtianguo 写道服务端和客户端在同一个包下,这句代码He ...
JAVA RMI 实例 -
manxisuo:
非常好,多谢!!
JAVA RMI 实例
一、前言
动态MBean是在运行期才定义它的属性和方法,也就是说它有什么属性和方法是可以动态改变的。动态MBean主要利用一些辅助类(构造函数类MBeanConstructorInfo、属性类MBeanAttributeInfo、方法类MBeanOperationInfo)来完成这个功能,所有的动态MBean必须实现DynamicMBean接口。DynamicMBean写好后,使用方法和第一篇文章中普通的MBean一样。
给出一个动态MBean的实例,这个实例最初动态构了一个Name属性及一个print方法,当我们执行它的print方法之后,又给此MBean新增了一个print1方法。实例的代码如下:
二、实例
1、HelloDynamic类
java 代码
- 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而已。为了实例完整,也一并帖出来吧。
java 代码
- 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还是用得比较少,所谓利器深藏之而不用,非常时方现光芒。
发表评论
-
Lucene全文检索样例(解决大文本建索引)
2008-01-31 10:05 6476建索引:package com.pccw; i ... -
txt读取字符串(为实习生作的例子)
2008-01-29 18:27 1928try { FileInputStream fi = n ... -
关于Lucene索引合并解决方法
2008-01-28 10:21 9278由于Clustor的问题造成无法对索引进行同步,脑子中马上浮现 ... -
Spring 2.0 AOP 与事务配置
2007-11-20 16:13 2760Spring 2.0 AOP 与事务配置 ********* ... -
关于Soeckt 流操作的数据转换工具方法
2007-11-02 10:50 2178java 代码 package com ... -
Java socket开发实例入门
2007-08-21 16:08 12191socket用中文意思就是我 ... -
不使用webwork标签直接用Jsp取得Action中的值
2007-05-22 14:23 3057今天突然一个同事问我webwork如果不用标签只用JSP ... -
23种模式趣味解释
2007-05-22 10:36 1942创建型模式 1、FACTORY ... -
关于领域逻辑的三个主要模式
2007-05-13 22:36 2674事务脚本 事务脚本比较好理解,简单来说,就是将大多是事务,以过 ... -
又谈领域模型
2007-04-29 09:58 2222昨天,突然和阿敏谈起领域模型,发现自己的理解还是 ... -
简单Socket编程,来理解Socket
2007-02-27 09:34 2212Server端: java 代码 p ... -
[JMX一步步来] 9、基于JBoss来写MBean
2007-02-02 13:31 13817前面都是用JDK自带的JMX ... -
[JMX一步步来] 8、编写程序来连接MBean
2007-02-02 13:18 5922前面用Html、jconsole等方法连接上了MBeanSer ... -
[JMX一步步来] 7、用JDK5.0的JConsole来连接MBean
2007-02-02 13:09 8612前面所有看效果都是通过Html网页来看的。JDK5.0自带了一 ... -
[JMX一步步来] 6、模型Bean:Model Bean
2007-02-02 13:06 5723在上一节是用apache的commons-modeler来开发 ... -
[JMX一步步来] 5、用Apache的commons-modeler来辅助开发JMX
2007-02-02 12:59 6960一、前言 每一个MBean都要有一个接口,比如前面的H ... -
[JMX一步步来] 3、Notification的使用
2007-02-02 12:36 6193一、简介 Mbean之间的通信是必不可少的, ... -
[JMX一步步来] 2、JMX简介
2007-02-02 11:25 8771一、JMX简介 JMX是一 ... -
[JMX一步步来] 1、JMX的Hello World
2007-02-02 11:23 10109一、JMX简介 什么是JMX?在一篇网文中是这样说的: ... -
JAVA RMI 实例
2007-02-02 10:51 39285JAVA RMI 快速入门实例 本实例为参考多篇文章写就而成 ...
相关推荐
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每个层的详细代码和说明