1. Establishment of an MbeanPackageCOM.LHT.JMX; /* Hellombean.java-mbean interface describing the management operations and attributes for the "Hello World MBean." In the case there are two operations, "SayHello" and "add", and two attributes, "Name" and "CacheSize". */ PublicInterfaceHellombean {//operations PublicvoidSayHello (); PublicintAddintXinty); Attributes//A read-only attribute called Name of type String PublicString GetName (); A read-write attribute called CacheSize of type int PublicintGetcachesize (); PublicvoidSetCacheSize (intsize); }
2. Create a class that implements an MbeanPackageCOM.LHT.JMX; PublicclassHelloImplementsHellombean { PublicvoidSayHello () {System.out.println ("Hello, World"); } PublicintAddintXintY) { return x + y;  } /* getter for the name attribute. the pattern shown here is frequent: the getter returns a private field Representing the attribute value. In Our case, the attribute value never Changes, but for other attributes it might change as the application runs. consider an attribute representing statistics such as uptime or memory usage, for example. being     read-only just means that it can ' t be changed through the management interface. */ PublicString GetName () { return This. Name; }/* Getter for the CacheSize attribute. The pattern shown this is Frequent:the getter returns a private field representing the attribute value, And the setter changes that field. */ PublicintGetcachesize () { return This. cacheSize; }/* Setter for the CacheSize attribute. To avoid problems and stale values in multithreaded situations, it is a good idea for setters to be SYN Chronized. */ PublicsynchronizedvoidSetCacheSize (intSize) { This. cacheSize = size; /* In a real application, changing the attribute would typically have effects beyond just modifying the CacheSize Field. For example, resizing the cache might mean discarding entries or allocating new ones. The logic for these effects would are here. * * SYSTEM.OUT.PRINTLN ("Cache size Now" + This. cacheSize); }PrivateFinalString name = "Reginald";PrivateintCacheSize = default_cache_size;PrivateStaticFinalintDefault_cache_size = 200; }
3. Client code
PackageCOM.LHT.JMX;Importjava.lang.management.*;Importjavax.management.*;ImportCom.sun.jdmk.comm.HtmlAdaptorServer; PublicclassMain { PublicStaticvoidMain (string[] args)throwsException {Mbeanserver mbs = Managementfactory.getplatformmbeanserver ();FinalHtmladaptorserver Htmladaptor =NewHtmladaptorserver ();FinalObjectName Htmladaptoron =NewObjectName ("Com.example.mbeans:name=htmladaptor"); Mbs.registermbean (Htmladaptor, Htmladaptoron); Htmladaptor.setport (9999); System.out.print ("Starting the htmladaptor ..."); Htmladaptor.start (); } }
4. Run the Java code above
Console information: Starting the htmladaptor ....
5. Enter in the browser
http://localhost:9999/
At this point you can see an open page, and now you can use this page to manage the Mbean!
More details: http://www.itisedu.com/phrase/200604261751455.html
Http://dev2dev.bea.com.cn/techdoc/20005040805.html