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

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條評論
主站蜘蛛池模板: 亚洲一级毛片视频 | 欧美在线视频免费看 | 日韩欧美视频一区二区在线观看 | 2021天天干| 精品一区二区视频 | 欧美片欧美日韩国产综合片 | 日本人xx视频免费视频 | 免费的成人a视频在线观看 免费的黄色网 | aⅴ免费视频| 久久九九青青国产精品 | 不卡伦理 | 亚洲综合成人网 | 国产伦理久久精品久久久久 | 久久久精品久久视频只有精品 | 青草视频在线观看视频 | 超激情碰碰碰啪在线视频 | 国产成+人+综合+亚洲不卡 | 久久精品国产亚洲网站 | 四虎在线观看视频 | 久久久久久久免费视频 | 色综合欧美亚洲另类久久 | 亚洲欧美日韩中文v在线 | 俺去也理论网站 | 亚洲成年| 第一福利视频网 | 久久99久久 | 久久久久久久久久久96av | 久久国产精品99精品国产 | 99在线国产 | 九色福利 | 久久机热一这里只精品 | 91亚洲国产在人线播放午夜 | 久久不射影院 | 精品久久亚洲一级α | 男人的天堂视频在线 | 国产综合色香蕉精品五月婷 | 国产一级黄色网 | 性做久久| 大学生一级毛片高清版 | 免费观看黄色毛片 | 搜毛片 |