JMX Simple Example

Source: Internet
Author: User

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;       &nbsp}           /* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.