生活随笔
收集整理的這篇文章主要介紹了
JMX之模型MBean
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
標(biāo)準(zhǔn)MBean所暴露的屬性,操作,通知都是固定不變的,都固化在ClassNameMBean這個(gè)接口中,靈活度不足。
動(dòng)態(tài)Mbean所暴露的特性是運(yùn)行時(shí)確立,靈活度足夠,但編碼困難,因?yàn)槟阈枰幋a實(shí)現(xiàn)每一個(gè)屬性,操作與通知。
模型Mbean也是一種動(dòng)態(tài)Mbean,能夠使你更快的編寫(xiě)動(dòng)態(tài)MBean.
模型MBean的封裝類(lèi)RequiredModelMBean實(shí)現(xiàn)了ModelMBean接口,而ModelMBean接口繼承自DynamicMBean,所以說(shuō)模型Mbean也是一種動(dòng)態(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~!!!"); ?????} ?} ? 測(cè)試類(lèi):
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還沒(méi)有任何關(guān)系,去掉1,2兩行注釋,你會(huì)發(fā)現(xiàn)ModelMBeanAttributeInfo的描述子用來(lái)存儲(chǔ)了這個(gè)值(代碼中,ModelMBeanAttributeInfo的最后一個(gè)參數(shù),我設(shè)了null,JDK會(huì)為之創(chuàng)建一個(gè)默認(rèn)的描述子)。
?
如何把對(duì)MBean的操作轉(zhuǎn)移動(dòng)對(duì)HelloWordShadow的操作,以達(dá)到我們管是資源的目的。
第一步為ModelMBeanAttributeInfo對(duì)像添加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)并沒(méi)有起作用。剛才說(shuō)到ModelMBeanAttributeInfo用了默認(rèn)的描述子,我們不防給
添加一個(gè)描述子:
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")); ?}? 更糟糕,直接報(bào)錯(cuò):Operation setName not in ModelMBeanInfo。
但這也說(shuō)明是用描述子設(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")); ? 仍然報(bào)錯(cuò): managedResource for invoke setName is null
managedResource是什么?就是我們需要管理的對(duì)像資源,在這里當(dāng)然是HelloWordShadow對(duì)象,終于扯上關(guān)系了。那就構(gòu)造一個(gè)HelloWordShadow對(duì)象:
..... 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,成功。說(shuō)了這么多,那構(gòu)造函數(shù)ModelMBeanAttributeInfo("name",???????? "pepole name",getMethod, setMethod,nameDesc);里的getMethod, setMethod方法有什么用呢,我覺(jué)得好像沒(méi)什么用,只是用來(lái)確認(rèn)屬性類(lèi)型,可讀可寫(xiě)情況。(個(gè)人觀點(diǎn))
最后就是把這個(gè)MBean注冊(cè)到MBeanServer中管理,用Jconsole就可以看到了:
ms.registerMBean(rmm, new ObjectName(
?????? "guojje:type=notification,name=hello"));
(待續(xù))
轉(zhuǎn)載于:https://blog.51cto.com/guojuanjun/599229
總結(jié)
以上是生活随笔為你收集整理的JMX之模型MBean的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。