|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
IDE是好。java中的IDE更是百花齐放,你用jbuilder能说jbuilder赶不上vs吗?用eclipse,netbeans也很舒服啊。我就不明白“稍微差一些”那一些是从哪里差来的。standard|word
这个复杂的Mbean只要一个属性和一个办法,StardBeanDemoMain为挪用类,main办法中挪用上面三个办法,代码中都有具体的正文:sbean.doRegistBean();sbean.doManageBean();sbean.regHtmlAdaptor();
运转后,能够看到把持台打印的信息,registerStandMbeansucess..Attribute:state-initvalueAttribute:state2-newstateMyservicestart.....
因为启动了HtmlAdaptor的服务,以是程序一向处于运转形态,(HtmlAdaptor是sunjmx完成自带的一个适配器)测试能够在扫瞄器中会见:http://localhost:8082就能够在扫瞄器中办理mbean了,在ListofMBeanoperations上面按startService按钮,能够看到把持台打出的信息,申明startService办法被挪用了。
/***Mbean接口*StandBeanMBean.java*@authorzmxj*/publicinterfaceStandBeanMBean{publicStringgetState();publicvoidsetState(Strings);publicvoidstartService();}
/***Mbean完成*StandBean.java*@authorzmxj*/publicclassStandBeanimplementsStandBeanMBean{
privateStringstate="initvalue";
publicStringgetState(){returnstate;}
publicvoidsetState(Strings){state=s;}
publicvoidstartService(){System.out.println("Myservicestart.....");}
}
importjavax.management.Attribute;importjavax.management.AttributeNotFoundException;importjavax.management.InstanceNotFoundException;importjavax.management.InvalidAttributeValueException;importjavax.management.MBeanException;importjavax.management.MBeanServer;importjavax.management.MBeanServerFactory;importjavax.management.MalformedObjectNameException;importjavax.management.ObjectName;importjavax.management.ReflectionException;
importcom.sun.jdmk.comm.HtmlAdaptorServer;
/***Agent类*StardBeanDemoMain.java*@authorzmxj*/publicclassStardBeanDemoMain{
privateMBeanServermBserver=null;ObjectNamembeanObjectName=null;Stringdomain=null;StringmbeanName="StandBean";publicstaticvoidmain(String[]args){StardBeanDemoMainsbean=newStardBeanDemoMain();sbean.doRegistBean();sbean.doManageBean();sbean.regHtmlAdaptor();}/***创立MBeanServer并注册一个Mbean*/privatevoiddoRegistBean(){//创立MBeanServermBserver=MBeanServerFactory.createMBeanServer();domain=mBserver.getDefaultDomain();
try{mbeanObjectName=newObjectName(domain+":type="+mbeanName);/*//这里能够间接创立而且同时注册一个mbean到MBeanServer.server.createMBean(mbeanName,mbeanObjectName);*/StandBeanbean=newStandBean();mBserver.registerMBean(bean,mbeanObjectName);System.out.println("registerStandMbeansucess..");}catch(MalformedObjectNameExceptione){e.printStackTrace();System.exit(1);}catch(Exceptione){e.printStackTrace();System.exit(1);}}
/**演示办理mbean,经由过程mbserver修正mbean的属性和实行mbean的办法*/privatevoiddoManageBean(){try{ObjectNamembeanObjectName=newObjectName(domain+":type="+mbeanName);System.out.println("Attribute:state-"+mBserver.getAttribute(mbeanObjectName,"State"));
//修正属性StateAttributestateAttribute=newAttribute("State","newstate");mBserver.setAttribute(mbeanObjectName,stateAttribute);//获得属性值System.out.println("Attribute:state2-"+mBserver.getAttribute(mbeanObjectName,"State"));//挪用办法startServicemBserver.invoke(mbeanObjectName,"startService",null,null);}catch(Exceptione){e.printStackTrace();}}/**注册HtmlAdaptor,注册后能够经由过程http://localhost:8082实行mbean办理操纵*/privatevoidregHtmlAdaptor(){HtmlAdaptorServerhtml=newHtmlAdaptorServer();ObjectNamehtml_name=null;try{html_name=newObjectName("Adaptor:name=html,port=8082");mBserver.registerMBean(html,html_name);}catch(Exceptione){System.out.println(" !!!CouldnotcreatetheHTMLadaptor!!!");e.printStackTrace();return;}html.start();}}
而学习JAVA我觉得最应该避免的就是:只学习,不思考,只记忆,不实践! |
|