`
scanfprintf123
  • 浏览: 80265 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

JMX Dynamic MBean

阅读更多

    The second MBean I want to bring in is the Dynamic MBean, the main purpose of using Dynamic MBean is that your management interface varies very often, just think of a simplest case, we have a managed resource about the Person's information, and now we can set and get the firstName and lastName of a certain person thro jmx and it works fine, but one day, your boss tell you that we should provide the address info of the person too, if we're using the Standard MBean, what should we do? We have to modify the interface and add the implementations for added methods, don't we?It would be a mess of work when this kind of situation happens often, especially when the Standard MBean's management interface consists of many predefined interfaces which is prone to be changed later. And Dynamic MBean can ease this kind of situation(Although Dynamic MBean can ease this situation, it also has some limitations, which will be covered in Model MBean blog entry).

 

     Recall from the Standard MBean article, its management interface is made of some predefined interface the MBean implements, while Dynamic MBean forms its management interface by its metadata, a MBeanInfo.That's why the Dynamic MBean interface defines a 'getMBeanInfo()' method. Now let's take a look at the Dynamic MBean interface.



 

    There is a limitation about MBeas is that a MBean can not be both Standard and Dynamic, that is to say, a MBean can not implements the Standard interface and the Dynamic interface at the same time. As we previously described, the management interface is nothing but a group of attributes and operations, and that is why Dynamic MBean interface defines methods such as getAttribute(), setAttribute().

 

    Since we all know that the Daynamic MBean's management interface is defined at runtime, so, you would like to examine the MBeanInfo in Dynamic MBean, and see how it make up the management interface.

   


   MBeanFeatureInfo is the super class of all the other mbean metadatas. And from their names, we could easily infer their purposes, for example MBeanOperationInfo correponds to the operations defined in the manageable resource and the MBeanAttributeInfo corresponds to the attributes of your manageable resource. Although they are easy to understand, I still like to walk you through them one by one.

   Speaking of MBeanFeatureInfo, since it's the super class of all the other mbean metadatas, it only defines two attributes that need us to be awared of.

  • name->what does it stand for depends on the metadata type, if the metadata type is method-related, such as MBeanOperationInfo or MBeanConstructorInfo, then the name here indicates a method name, if the metadata type is attribute-related like MBeanParamenterInfo or MBeanAttributeInfo, then it indicates a attribute name.
  • description->a custom text which is used to describe what is this metadata for or something like that.

   MBeanParameterInfo, a parameter wrapper which is used in MBeanOperationInfo or MBeanConstructorInfo. You can create it in the following two ways:

 public MBeanParameterInfo(String name, String type,String description)

 public MBeanParameterInfo(String name,String type,String description,Descriptor descriptor)

    We'll avoid Descriptor class here, since it will be covered in Model MBean article. Here just think of it as an object that can hold some additional infos.

  •     type->the parameter type of course, for example, for parameter 'String name', its type is 'java.lang.String'

    MBeanOperationInfo, it encapsulates an operation exposed by an MBean, a method wrapper.The following are its two constructors:

   

public MBeanOperationInfo(String description, Method method);

public MBeanOperationInfo(String name,
			      String description,
			      MBeanParameterInfo[] signature,
			      String type,
			      int impact)
  • type->indicates the type of the return value of this method.
  • impact->there are four values for this argument,INFO,ACTION,ACTION_INFO and UNKNOWN.INFO means this operation can return information, ACTION indicates the operation could change the state of MBean without returning information, ACTION_INFO indicates the operation not only change the state of MBean but also return information, and UNKNOWN indicates an unknown operation.

   

    MBeanAttributeInfo, it can encapsulates an attribute of MBean, just like the other metadata, you can construct it in the following ways:

   

public MBeanAttributeInfo(String name,
			      String type,
			      String description,
			      boolean isReadable,
			      boolean isWritable,
			      boolean isIs)

 public MBeanAttributeInfo(String name,
			      String type,
			      String description,
			      boolean isReadable,
			      boolean isWritable,
			      boolean isIs,
                              Descriptor descriptor)
  • type->the type of attribute, like the one in MBeanParameterInfo.
  • isReadable->indicates whether it is readable or not.
  • isWritable->indicates whether it is writable or not.
  • isIs->indicates whether this attribute starts with 'is' or not, in the meantime, this attribute has to be a boolean type.

     MBeanConstructorInfo, a constructor wrapper, you can create it by using its two constructors:

  

public MBeanConstructorInfo(String description, Constructor constructor)

 public MBeanConstructorInfo(String name, 
			String description,
			MBeanParameterInfo[] signature)

  

   MBeanNotificationInfo, a metadata class that describes the notifications the MBean can emit. Nothing special and like the other metadata classes, it has two constructors:

  

public MBeanNotificationInfo(String[] notifTypes,
				 String name,
				 String description)

public MBeanNotificationInfo(String[] notifTypes,
			 String name,
			 String description,
                                 Descriptor descriptor)

 

   After examining the Dynamic MBean interface and its metadata classes, it is time for some concrete examples to combine them together. We often implement the Dynamic MBean by using javabean introspection, if you have experience of java reflection, you'll find that there are no fancy codes at all in the following codes.

  

public class DynamicImpl implements DynamicMBean {

	public Object getAttribute(String attribute)
			throws AttributeNotFoundException, MBeanException,
			ReflectionException {

		try {
			Field attr = this.getClass().getDeclaredField(attribute);
			// this make the private attribute accessible
			attr.setAccessible(true);
			return attr.get(this);
		} catch (Exception e) {
			// eatting all
		}
		return null;
	}

	public AttributeList getAttributes(String[] attributes) {
		AttributeList attrList = new AttributeList();
		try {
			Attribute attribute = null;
			for (String attr : attributes) {
				attribute = new Attribute(attr, getAttribute(attr));
				attrList.add(attribute);
			}
		} catch (Exception e) {
			// eatting all
		}
		return attrList;
	}

	public Object invoke(String actionName, Object[] params, String[] signature)
			throws MBeanException, ReflectionException {
		try {
			// first, constructing the param types
			Class[] paramTypes = new Class[signature.length];
			int i = 0;
			for (String sig : signature) {
				paramTypes[i++] = this.getClass().getClassLoader().loadClass(
						sig);
			}

			// find the method and invoke it
			Method md = this.getClass().getMethod(actionName, paramTypes);
			return md.invoke(this, params);
		} catch (Exception e) {
			// eatting all
		}
		return null;
	}

	public void setAttribute(Attribute attribute)
			throws AttributeNotFoundException, InvalidAttributeValueException,
			MBeanException, ReflectionException {
		try {
			Field attr = this.getClass().getDeclaredField(attribute.getName());
			// this make the private attribute accessible
			attr.setAccessible(true);
			attr.set(this, attribute.getValue());
		} catch (Exception e) {
			// eatting all
		}
	}

	public AttributeList setAttributes(AttributeList attributes) {
		try {
			Attribute attribute = null;
			for (int i = 0; i < attributes.size(); i++) {
				attribute = (Attribute) attributes.get(i);
				setAttribute(attribute);
			}
		} catch (Exception e) {
			// eatting all
		}
		return attributes;
	}

	public MBeanInfo getMBeanInfo() {
		// exposes the attribute name
		MBeanAttributeInfo attributeInfo = new MBeanAttributeInfo("name",
				"java.lang.String", "name exposed", true, true, false);

		// exposes the printName operation
		MBeanOperationInfo operationInfo = new MBeanOperationInfo("printName",
				"pring name info ", new MBeanParameterInfo[0], "void", 2);

		return new MBeanInfo("DynamicImpl",
				"The manageable resource is DynamicImpl",
				new MBeanAttributeInfo[] { attributeInfo },
				new MBeanConstructorInfo[0],
				new MBeanOperationInfo[] { operationInfo },
				new MBeanNotificationInfo[0]);
	}

	private String name;

	public void printName() {
		System.out.println("name->" + name);
	}

	public String getName() {
		return name;
	}

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

}

 

   When you finish your Dynamic MBean, register it to the MBean Server and enjoy it.

 

  • 大小: 18.2 KB
  • 大小: 17.7 KB
1
1
分享到:
评论
1 楼 hugo 2009-10-18  
非常清晰易懂,代码也可以直接用,多谢

相关推荐

    Java分布式应用学习笔记09JMX-MBean的介绍

    ### Java分布式应用学习笔记09JMX-MBean的介绍 #### MBean概念及作用 MBean,即Managed Bean,是在JMX(Java Management Extensions)框架中用于管理资源的一种特殊Java对象。通过MBean,可以方便地对应用程序进行...

    jmx 实例 rmi mbean

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

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

    2. **Dynamic MBean**: 动态MBean没有固定的管理接口,它的管理接口是在运行时动态定义的。它需要实现`MBeanInfo`接口来提供关于MBean的信息。 3. **Open MBean**: Open MBean提供了一种标准的数据类型系统,使得...

    JMX IN ACTION(五)

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

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

    2. **MBean Server**:这是JMX架构的核心,它负责注册和管理MBeans,处理MBean之间的交互,并提供给管理工具访问MBeans的接口。 3. **管理工具(Management Tools)**:这些工具可以是图形界面的,也可以是命令行的...

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

    JMX支持四种类型的MBean:Standard MBean、Dynamic MBean、Open MBean和Model MBean。 1. **Standard MBean**:是最简单、最常见的MBean类型,与普通的Java Bean类似,主要用于简单的管理和监控场景。例如,Java的`...

    java JMX概述

    MBean可以分为三类:Standard MBean、Dynamic MBean和Open MBean。Standard MBean遵循特定的接口规范,Dynamic MBean则可以通过实现`MBeanInfo`接口动态地定义其属性和操作,而Open MBean则提供了一种类型安全的方式...

    jmx_examples.rar_Java 8_jmx_jmx examples_jmx main_jmx_examples

    - 压缩包中的例子可能包括创建不同类型的MBean(Standard MBean、Dynamic MBean等)、注册MBean到MBean Server、暴露和操作MBean属性、执行MBean方法以及处理MBean通知。 - 每个示例通常会展示如何创建一个简单的...

    java利用JMX做出不一样的的JVM.docx

    MBean有四种类型:Standard MBean、Dynamic MBean、Open MBean和Model MBean。Standard MBean是最常见且最简单的形式,类似于普通的Java Bean。Dynamic MBean允许动态地获取和设置属性,适合于已存在的、不易转换为...

    jmx第一个学习例子

    JMX中有两种MBean类型:Standard MBean和Dynamic MBean。Standard MBean实现较为直接,适合开发阶段的项目;而Dynamic MBean则更加灵活,允许更精细的管理和监控能力,但实现相对复杂。 通过上述知识点的解析,我们...

    JMX初学资料 初学者入门教程

    MBean主要有三种:Standard MBean、Dynamic MBean和Open MBean。Standard MBean通过接口定义其管理属性和操作,Dynamic MBean更灵活,可以在运行时动态定义其管理接口,而Open MBean则是为了跨Java平台的兼容性和互...

    jmx-jvm配置

    MBean可以分为三种类型:Standard MBean、Dynamic MBean和Open MBean。 2. **MBean Server**: MBean Server是JMX框架的核心,它负责注册、管理和查询MBeans,以及执行MBean的操作。 3. **Management Interface**: ...

    JMX IN ACTION(七)

    【JMX IN ACTION(七)】章节探讨了JMX中的Model MBean,这是一种特殊类型的MBean,开发者无需编写MBean类。Model MBean是JMX规范定义的一部分,保证在所有符合JMX标准的代理中可用,它是一个通用的MBean,可以被实例...

    JMX in Action

    MBean有三种类型:Standard MBean、Dynamic MBean和Open MBean。Standard MBean通过接口定义其管理属性和操作;Dynamic MBean允许动态定义管理接口,灵活性更高;Open MBean则是为了支持更复杂的类型和数据结构而...

    《jmx技术介绍》配套源代码

    通过JMX连接器,外部客户端可以连接到MBean Server,获取MBean的信息或执行操作。 4. MBean Server: MBean Server是JMX的核心,它负责存储和管理MBeans。开发者可以自定义MBean Server,但通常使用Java提供的内置...

    jmx需要的jar包

    JMX支持三种类型的MBeans:Standard MBean、Dynamic MBean和Open MBean。 2. **MBean Server**:MBean Server是JMX架构中的核心组件,它负责注册MBeans、执行MBean操作、处理MBean之间的交互,并提供了一个集中式的...

    JMX、MXBean学习

    Standard MBeans是预定义了管理接口的类,Dynamic MBeans则可以在运行时动态定义其管理接口,而Model MBeans是一种抽象层,可以将任何Java对象转换为MBean。 MXBean是JMX中的一个特殊概念,它是标准MBean的扩展,...

    jmx学习资料

    MBeans分为三种类型:Standard MBean、Dynamic MBean和Open MBean。 2. **MBean Server**: MBean Server是JMX的核心组件,它负责注册MBeans、执行对MBeans的操作并处理MBeans之间的交互。它是管理域的中心,管理...

    JAVA JMX 学习资料

    JMX Connectors提供了连接到MBean Server的途径,有多种类型的连接器,如RMI(Remote Method Invocation)和JMXMP(JMX Message Protocol),用于远程管理。 6. **JConsole**: Java自带的JConsole是JMX的一个...

    jmx例子一则

    MBeans有三种类型:Standard MBean、Dynamic MBean和Open MBean,分别对应不同的定义和实现方式。 其次,JMX中的代理(MBeanServer)是管理的核心组件。它负责注册和管理MBeans,执行MBean的操作,并处理MBean之间...

Global site tag (gtag.js) - Google Analytics