生活随笔
收集整理的這篇文章主要介紹了
JMX之模型MBean
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
標(biāo)準(zhǔn)MBean所暴露的屬性,操作,通知都是固定不變的,都固化在ClassNameMBean這個接口中,靈活度不足。
動態(tài)Mbean所暴露的特性是運(yùn)行時確立,靈活度足夠,但編碼困難,因為你需要編碼實現(xiàn)每一個屬性,操作與通知。
模型Mbean也是一種動態(tài)Mbean,能夠使你更快的編寫動態(tài)MBean.
模型MBean的封裝類RequiredModelMBean實現(xiàn)了ModelMBean接口,而ModelMBean接口繼承自DynamicMBean,所以說模型Mbean也是一種動態(tài)Mbean。
首先定義需要管理的資源:
package?guojje.jmx; ??import?javax.management.NotificationBroadcasterSupport; ??public?class?HelloWordShadow?extends?NotificationBroadcasterSupport{ ????? ?????private?String?name?=?"anranran"; ?????public?HelloWordShadow(String?name){ ?????????this.name?=?name; ?????} ????? ?????public?String?getName(){ ?????????System.out.println("invoke?getName?method!!"); ?????????return?name; ?????} ????? ?????public?void?setName(String?name){ ?????????System.out.println("invoke?setName?method!!"); ?????????this.name?=?name; ?????} ????? ?????public?void?Say(){ ?????????System.out.println("hi~!!!"); ?????} ?} ? 測試類:
package?guojje.jmx; ?public?class?Main?{ ?????public?static?void?main(String?args[])?throws?Exception?{ ?????????JMXServiceURL?jUrl?=?new?JMXServiceURL("iiop",?"192.168.1.61",?9998, ?????????????????"/jndi/rmi://localhost:9999/guojje"); ?????????MBeanServer?ms?=?MBeanServerFactory.createMBeanServer(); ?????????JMXConnectorServer?cs?=?JMXConnectorServerFactory ?????????????????.newJMXConnectorServer(jUrl,?null,?ms); ?????????cs.start(); ?????????System.out.println("jmx?address:"?+?cs.getAddress()); ??????????exeHelloWordShadow(ms); ?????} ?private?static?void?exeHelloWordShadow(MBeanServer?ms)?throws?Exception?{ ?????????RequiredModelMBean?rmm?=?new?RequiredModelMBean(); ??????????????????ModelMBeanAttributeInfo?nameAttr?=?new?ModelMBeanAttributeInfo("name","java.lang.String", ?????????????????"pepole?name",true,?true,?false,null); ????????? ?????????ModelMBeanInfo?mmInfo?=?new?ModelMBeanInfoSupport(RequiredModelMBean.class.toString(),?"Jmx?demo",?new?ModelMBeanAttributeInfo[]{nameAttr},?null,?null,?null); ?????????rmm.setModelMBeanInfo(mmInfo); ??????????????rmm.setAttribute(new?Attribute("name",?"guojianjun")); ?????????System.out.println(rmm.getAttribute("name")); ??????} ? 目前name與HelloWorkShadow還沒有任何關(guān)系,去掉1,2兩行注釋,你會發(fā)現(xiàn)ModelMBeanAttributeInfo的描述子用來存儲了這個值(代碼中,ModelMBeanAttributeInfo的最后一個參數(shù),我設(shè)了null,JDK會為之創(chuàng)建一個默認(rèn)的描述子)。
?
如何把對MBean的操作轉(zhuǎn)移動對HelloWordShadow的操作,以達(dá)到我們管是資源的目的。
第一步為ModelMBeanAttributeInfo對像添加get,set方法.
private?static?void?exeHelloWordShadow(MBeanServer?ms)?throws?Exception?{ ? RequiredModelMBean?rmm?=?new?RequiredModelMBean(); ?????????Method?getMethod?=?HelloWordShadow.class.getMethod("getName", ?????????????????new?Class[]?{}); ?????????Method?setMethod?=?HelloWordShadow.class.getMethod("setName", ?????????????????new?Class[]?{String.class}); ?????????//add?a?property?called?'name' ?????????ModelMBeanAttributeInfo?nameAttr?=?new?ModelMBeanAttributeInfo("name", ?????????????????"pepole?name",getMethod,?setMethod,null); ????????? ????????? ????????? ?????????ModelMBeanInfo?mmInfo?=?new?ModelMBeanInfoSupport(RequiredModelMBean.class.toString(),?"Jmx?demo",?new?ModelMBeanAttributeInfo[]{nameAttr},?null,?null,?null); ?????????rmm.setModelMBeanInfo(mmInfo); ????????? ?????????rmm.setAttribute(new?Attribute("name",?"guojianjun")); ?????????System.out.println(rmm.getAttribute("name"));? ?}? 運(yùn)行結(jié)果:
jmx address:service:jmx:iiop://192.168.1.61:9998/jndi/rmi://localhost:9999/guojje
guojianjun
發(fā)現(xiàn)并沒有起作用。剛才說到ModelMBeanAttributeInfo用了默認(rèn)的描述子,我們不防給
添加一個描述子:
private?static?void?exeHelloWordShadow(MBeanServer?ms)?throws?Exception?{ ???? ..... Descriptor?nameDesc?=?new?DescriptorSupport(); ?????????nameDesc.setField("name",?"Name");?????????nameDesc.setField("descriptorType",?"attribute");?????????nameDesc.setField("displayName",?"Name"); ?????????nameDesc.setField("getMethod",?"getName"); ?????????nameDesc.setField("setMethod",?"setName"); ??????????????????ModelMBeanAttributeInfo?nameAttr?=?new?ModelMBeanAttributeInfo("name", ?????????????????"pepole?name",getMethod,?setMethod,nameDesc); ????????? ?????????ModelMBeanInfo?mmInfo?=?new?ModelMBeanInfoSupport(null,?"Jmx?demo",?new?ModelMBeanAttributeInfo[]{nameAttr},?null,?null,?null); ?????????rmm.setModelMBeanInfo(mmInfo); ????????? ?????????rmm.setAttribute(new?Attribute("name",?"guojianjun")); ?????????System.out.println(rmm.getAttribute("name")); ?}? 更糟糕,直接報錯:Operation setName not in ModelMBeanInfo。
但這也說明是用描述子設(shè)定set方法是正確的。這樣我們需要在ModelMBeanInfo中聲明setName操作,getName一樣:?
.....?????????ModelMBeanAttributeInfo?nameAttr?=?new?ModelMBeanAttributeInfo("name", ?????????????????"pepole?name",getMethod,?setMethod,nameDesc); ????????? ????????????????????ModelMBeanOperationInfo?getName?=?new?ModelMBeanOperationInfo(?????????????????????"getName",??????????????????????"get?name?attribute",??????????????????????null,??????????????????????"java.lang.String",??????????????????????MBeanOperationInfo.ACTION,??????????????????????null???????????????????);? ?????????? ??????????MBeanParameterInfo?mParam?=?new?MBeanParameterInfo("name",?"java.lang.String",?"set?name?methord?param"); ?????????? ??????????ModelMBeanOperationInfo?setName?=?new?ModelMBeanOperationInfo(?????????????????????"setName",??????????????????????"set?name?attribute",??????????????????????new?MBeanParameterInfo[]{mParam},??????????????????????null,??????????????????????MBeanOperationInfo.ACTION,??????????????????????null???????????);? ????????? ?????????ModelMBeanInfo?mmInfo?=?new?ModelMBeanInfoSupport(null,?"Jmx?demo",?new?ModelMBeanAttributeInfo[]{nameAttr},? ?????????????????null?,?new?ModelMBeanOperationInfo[]{getName,setName},?null); ?????????rmm.setModelMBeanInfo(mmInfo); ????????? ?????????rmm.setAttribute(new?Attribute("name",?"guojianjun")); ?????????System.out.println(rmm.getAttribute("name")); ? 仍然報錯: managedResource for invoke setName is null
managedResource是什么?就是我們需要管理的對像資源,在這里當(dāng)然是HelloWordShadow對象,終于扯上關(guān)系了。那就構(gòu)造一個HelloWordShadow對象:
..... ModelMBeanInfo?mmInfo?=?new?ModelMBeanInfoSupport(RequiredModelMBean.class.toString(),?"Jmx?demo",?new?ModelMBeanAttributeInfo[]{nameAttr},? ?????????????????null?,?new?ModelMBeanOperationInfo[]{getName,setName},?null); ?????????rmm.setModelMBeanInfo(mmInfo); ????????? ?????????HelloWordShadow?hw?=?new?HelloWordShadow("miniName"); ?????????rmm.setManagedResource(hw,?"ObjectReference"); ????????? ?????????rmm.setAttribute(new?Attribute("name",?"guojianjun")); ?????????System.out.println(rmm.getAttribute("name")); ? 運(yùn)行輸出:
jmx address:service:jmx:iiop://192.168.1.61:9998/jndi/rmi://localhost:9999/guojje
invoke getName method!!
invoke setName method!!
invoke getName method!!
guojianjun
OK,成功。說了這么多,那構(gòu)造函數(shù)ModelMBeanAttributeInfo("name",???????? "pepole name",getMethod, setMethod,nameDesc);里的getMethod, setMethod方法有什么用呢,我覺得好像沒什么用,只是用來確認(rèn)屬性類型,可讀可寫情況。(個人觀點)
最后就是把這個MBean注冊到MBeanServer中管理,用Jconsole就可以看到了:
ms.registerMBean(rmm, new ObjectName(
?????? "guojje:type=notification,name=hello"));
(待續(xù))
轉(zhuǎn)載于:https://blog.51cto.com/guojuanjun/599229
總結(jié)
以上是生活随笔為你收集整理的JMX之模型MBean的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。