亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

How to write your own Model MBean

系統(tǒng) 1605 0

???? 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.


How to write your own Model MBean
?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:


How to write your own Model MBean
?? 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.

??
How to write your own Model MBean
?? 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.

?

How to write your own Model MBean


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 777色狠狠一区二区三区 | 欧美日韩国产亚洲一区二区 | 亚洲精品99久久久久中文字幕 | 国产欧美日韩综合精品一区二区 | 久久精品在线观看 | 奇米吧| 在线观看中文字幕一区 | 四虎永久在线精品视频播放 | 日本一区二区三区高清福利视频 | 五月天在线免费视频 | 欧美日韩高清在线观看一区二区 | 国产成人教育视频在线观看 | 日韩成人小视频 | 阳光灿烂的日子在线观看 | 91亚洲区国产区精品区 | 天天操妹子 | 精品日韩一区二区三区 | 国产中文字幕视频 | 夜夜涩| 国产一区二区三区久久精品 | 亚洲九九九 | 欧美成人a级在线视频 | 日日撸| 97xxxx| 精品一区二区三区免费毛片爱 | 欧美一级视频在线 | 久久久久久久久综合影视网 | 视频一区二区在线 | 久久www免费人成精品香蕉 | 久久久久久久一精品 | 香蕉网久久 | 久草视频在线免费播放 | 97桃色| 久久com| 视频一区二区国产 | 久久亚洲精品tv | 在线日本韩国 | 99在线观看国产 | 在线观看国产欧美 | 久久免费播放 | 情趣色视频网站 |