Since Model MBean is much more flexible and is easy to decouple your manageable resouce from the mbean implementation details, people like to use it as the basic MBean. After we examine the Model MBean, let's see how to write our own Model MBean. First, let's review the Model MBean class diagram showed previously.
And we can see from the above, a complete Model MBean is much more complicated but also much more powerful. It can do a lot things, like persisting its state to a local file or remote db, or notifying other components it works with to do some corresponding tasks when some attributes of it change or some specific operations occur. But as I said in the Model MBean blog entry, the main purpose of Model MBean is to make creating the MBeanInfo and the manageable resource outside of the MBean system being possible.So we can leave the persisting ability and the notifying ability out for this moment. And the simple Model MBean should be like the below:
And according to the purpose of the Model MBean, the ModelMBean interface should at least contains two operations, one for holding the MBeanInfo metadata and the other for holding your manageable resource, which maybe like the below codes show:
public interface ModelMBean extends DynamicMBean
{
public void setMBeanInfo(MBeanInfo mbeanInfo) ;
public void setManagedResource(Object mr);
}
And it is straight, after that, let's try to finish our Model MBean, first of all, we need to find a better way to construct our MBeanInfo, you know, constructing a MBeanInfo metadata is a tedious, repetitive work and you wouldn't like to do that again if you did that before. This is also the problem of Dynamic MBean. Some people may come up with an idea of configuring them in a file, yeah, it is the traditional way to do it. And using annotations will be another way to handle this situation.
Now let's start with the traditional way, configuring them in a file. And it's no doubt the xml file will be our first choice. And then we need to map each of the mbean meatadata to its corresponding xml element. Recall from the Dynamic MBean, we got 5 MBean metadatas, MBeanConstructorInfo, MBeanOperationInfo, MBeanAttributeInfo, MBeanParameterInfo and MBeanNotificationInfo. You might find out the relationship btw the mbean metadata and its corresponding xml element from the below xml.
<mbean name="test mbean" domain="Test"
className="com.telenav.cserver.framework.management.jmx.support.test.Person">
<attr name="name" type="java.lang.String" description="name of person"
readable="true" writable="true" is="false" />
<op name="printHouseInfos" type="java.lang.String" description="only print infos">
<param name="msg" type="java.lang.String" description="the msg will be used to print" />
<param name="age" type="java.lang.Integer" description="only for test, useless" />
</op>
<con name="Person" description="the person constructor"/>
<notification name="test.person.notification" description="the person notification" notifyTypes="test.Person"/>
</mbean>
If you examine each xml element with the constructor of its corresponding mbean metadata, you will find that the content of xml file above is easy to understand. Like <attr> element, it refers to a MBeanAttributeInfo mbean metadata and its attributes like name, type etc are actually the arguments of the MBeanAttributeInfo constructor.
public MBeanAttributeInfo(String name,
String type,
String description,
boolean isReadable,
boolean isWritable,
boolean isIs)
And the rest is similar. The general idea of this is that when we meet with a xml element, we will create a corresponding mbean metadata, for example, when we meet with a <attr> element, we will create a MBeanAttributeInfo by using the attributes of <attr> element, but I dont want to construct a MBeanAttributeInfo directly, since I want to take more control over it. Think of the case, as we know, most of our java beans are readable and writable, thus we can simplify our <attr> element without specifying its readable,writable attribute, that is to say, they are true by default. If we create a MBeanAttributeInfo directly when we meet with a <attr> element, we have to do this kind of thing in the parsing xml logic, bad smell, isn't it? And if one day, we want to build our application on another jmx framework, as opposed to sun's jmx framework, then it seems that we have to do a lot of work to
get used to a new jmx framework. So, the better way is to build our own model, which will wrap a corresponding mbean metadata. And the following is a big picture of the above model.
Notice that all our Model implements a common interface, ICreateMBeanFeature interface, which only exposes a createMBeanFeature() method:
public interface ICreateMBeanFeature<T extends MBeanFeatureInfo> {
public T createMBeanFeature();
}
And from the above picture, we can see that the createMBeanFeature() is used to create the subtypes of MBeanFeatureInfo like MBeanAttributeInfo, MBeanOperationInfo etc.
public class ExposedAttribute extends ExposedFeature {
......
public MBeanAttributeInfo createMBeanFeature() {
info = new MBeanAttributeInfo(getName(), getType(), getDescription(),
isReadable(), isWritable(), isIs());
return (MBeanAttributeInfo) info;
}
}
In this manner, we will parse the xml into our Model, ExposedFeature object, and then it will create a corresponding mbean metadata from its createMBeanFeature() method, and make up the MBeanInfo we want. Now that we know how to create the MBean metadata, do we need a container to collect the created mebean metadata? Yes, we do, since I dont want to mix our parsing logic with the mbean metadata collecting logic. Then we need a parser and a MBean metadata holder, called ManagedMBean.
#ManagedMBean
public class ManagedMBean {
private List<ExposedAttribute> attributes = Collections
.synchronizedList(new ArrayList<ExposedAttribute>());
private List<ExposedOperation> operations = Collections
.synchronizedList(new ArrayList<ExposedOperation>());
private List<ExposedConstructor> constructors = Collections
.synchronizedList(new ArrayList<ExposedConstructor>());
private List<ExposedNotification> notifications = Collections
.synchronizedList(new ArrayList<ExposedNotification>());
......
public MBeanInfo getMBeanInfo() {
// For creating MBeanAttributeInfos
MBeanAttributeInfo[] attributeInfos = new MBeanAttributeInfo[attributes
.size()];
int attrIndex = 0;
for (ExposedAttribute attr : attributes) {
attributeInfos[attrIndex++] = attr.createMBeanFeature();
}
// For creating MBeanOperationInfos
MBeanOperationInfo[] operationInfos = new MBeanOperationInfo[operations
.size()];
int opIndex = 0;
for (ExposedOperation op : operations) {
operationInfos[opIndex++] = op.createMBeanFeature();
}
// For creating MBeanConstructorInfos
MBeanConstructorInfo[] constructorInfos = new MBeanConstructorInfo[constructors
.size()];
int conIndex = 0;
for (ExposedConstructor con : constructors) {
constructorInfos[conIndex++] = con.createMBeanFeature();
}
// For creating MBeanNotificationInfos
MBeanNotificationInfo[] notificationInfos = new MBeanNotificationInfo[notifications.size()];
int notiIndex = 0;
for (ExposedNotification noti : notifications) {
notificationInfos[notiIndex++] = noti.createMBeanFeature();
}
return new MBeanInfo(getClassName(), getDescription(), attributeInfos,
constructorInfos, operationInfos, notificationInfos);
}
}
And our parser interface, it defines where to locate the configurable file and the parsing logic.
public interface IMBeanParser {
public List<ManagedMBean> parse();
public void setMBeanFilePath(String path);
}
Since now we have introduce the ManagedMBean object, we need to change our IModelMBean interface too.
public interface IModelMBean extends DynamicMBean {
public void setManagedMBean(ManagedMBean mbean);
public void setManagedResource(Object obj);
}
We don't have to hold the MBeanInfo object directly but its counterpart ManagedMBean object.
After we know how to parse the configurable file into mbean metadata and how to use them to form the MBeanInfo, let's get to implement the IModelMBean interface, as I said before implementing the IModelMBean is actually implementing the dynamic MBean since it is a dynamic MBean, and from the Dynamic MBean entry, you already know how to implement a dynamic MBean by using java reflection, but there are some differences, first of all, the MBeanInfo is created from IMBeanParser, and when you implement the methods of DynamicMBean like setAttribute(Attribute attribute), invoke(String actionName, Object[] params, String[] signature), actually you are invoking the managed resource's methods, which is passing from the IModelMBean#setManagedResource(Object obj).
public class ModelMBeanImpl implements IModelMBean {
private ManagedMBean managedMBean;
private Object managedResource;
public void setManagedMBean(ManagedMBean mbean) {
this.managedMBean = mbean;
}
public void setManagedResource(Object obj) {
this.managedResource = obj;
}
public MBeanInfo getMBeanInfo() {
return managedMBean.getMBeanInfo();
}
......
}
And the left implementations of other methods in DynamicMBean is similar as we showed in DynamicMBean blog entry, you can check them over there.
- 大小: 4.7 KB
- 大小: 3.1 KB
- 大小: 20.1 KB
分享到:
相关推荐
20.5.3 自己编写一个model MBean 164 20.5.4 注册 164 20.5.5 ManagedBean 165 20.5.6 BaseModelMBean 165 20.5.7 使用Modeler API 165 20.6 Catalian中的MBean 165 20.6.1 ClassNameMBean 165 20.6.2 ...
【JMX IN ACTION(七)】章节探讨了JMX中的Model MBean,这是一种特殊类型的MBean,开发者无需编写MBean类。Model MBean是JMX规范定义的一部分,保证在所有符合JMX标准的代理中可用,它是一个通用的MBean,可以被实例...
### Java分布式应用学习笔记09JMX-MBean的介绍 #### MBean概念及作用 MBean,即Managed Bean,是在JMX(Java Management Extensions)框架中用于管理资源的一种特殊Java对象。通过MBean,可以方便地对应用程序进行...
JMX Instrumentor 是一个微型 Java 工具,用于将任意对象检测为 JMX 模型 MBean。 它通过反射检查对象的类并构造相应的 ModelMBeanInfo。 甚至可以通过特殊的 JMX 注释来改进此信息。
2. **MBean Server**:这是JMX架构的核心,它负责注册和管理MBeans,处理MBean之间的交互,并提供给管理工具访问MBeans的接口。 3. **管理工具(Management Tools)**:这些工具可以是图形界面的,也可以是命令行的...
JMX支持四种类型的MBean:Standard MBean、Dynamic MBean、Open MBean和Model MBean。 1. **Standard MBean**:是最简单、最常见的MBean类型,与普通的Java Bean类似,主要用于简单的管理和监控场景。例如,Java的`...
1. **创建MBean**: 首先,你需要创建一个实现了所需管理功能的Java类,然后通过`@ManagedBean`注解声明它为一个MBean。 2. **注册MBean**: 使用`MBeanServer`注册你的MBean。`MBeanServer`是JMX的核心组件,负责...
Model MBean通过`javax.management.modelmbean.ModelMBean`接口和一个描述符文件(如`car-mbean-descriptor.xml`)定义其行为。 描述符文件(如`car-mbean-descriptor.xml`)包含了MBean的元数据,包括属性、操作、...
Quartz jboss Mbean Config
NULL 博文链接:https://jasonhan-sh-hotmail-com.iteye.com/blog/1171904
MBean(Managed Bean)是Java管理扩展(Java Management Extensions,JMX)框架的一部分,它提供了一种标准的方式来管理和监控Java应用程序。MBean是可管理对象的简单表示,它允许外部工具或管理系统通过JMX接口来...
**JMX(一)——MBean Server** Java Management Extensions (JMX) 是Java平台上的一个标准,用于管理和监控应用程序、设备和服务。它提供了一个灵活的框架,使得开发者能够轻松地创建可管理的组件,并通过标准接口...
本课程介绍JMXAPI的基本概念,它被称之为被管理的bean,或者MBean。MBean是一个被管理的Java对象,就像Javabean组件一样,但是它遵从JMX规范的设计模式。MBean可以表示设备、应用或者任何需要被管理的资源。MBeans...
MBean有四种类型:Standard MBean、Dynamic MBean、Open MBean和Model MBean。Standard MBean是最常见且最简单的形式,类似于普通的Java Bean。Dynamic MBean允许动态地获取和设置属性,适合于已存在的、不易转换为...
MBeans分为标准MBean、自定义MBean和动态MBean三种类型,它们分别对应不同的实现方式和灵活性。 2. **在Java Web中的应用** 在Java Web应用中,JMX可以用来监控Servlet容器、数据库连接池、线程池等关键组件的状态...
mbean-annotation-api-4.5.2.jar
mbean-annotation-api-4.5.1.jar
mbean-annotation-api-4.5.0.jar
mbean-annotation-api-4.0.0.jar