The Standardmbean of Jmx Getting Started Helloword

Source: Internet
Author: User
Tags exit interface modify return domain
Standard|word


This simple Mbean has only one attribute and one method, Stardbeandemomain for the calling class, and the following three methods are called in the Main method, with detailed comments in the code: Sbean.doregistbean ();        Sbean.domanagebean (); Sbean.reghtmladaptor ();

After running, you can see the information that the console prints, register Standmbean sucess. Attribute:state-init valueattribute:state 2-new statemy service start ....

Because the Htmladaptor service is started, the program is always running, and (Htmladaptor is one of the adapters that Sun JMX implements) the test can be accessed in the browser: http://localhost:8082 You can manage the MBean in the browser, and by pressing the StartService button under the list of MBean operations, you can see the information that the console is playing, indicating that the StartService method was invoked.



/** * Mbean Interface * Standbeanmbean.java * @author ZMXJ */public interface Standbeanmbean {public String getState ();        public void SetState (String s); public void StartService ();}



/** * Mbean Implementation * Standbean.java * @author ZMXJ */public class Standbean implements Standbeanmbean {

Private String state = "init value";

Public String GetState () {return state; }

public void SetState (String s) {state = S; }

public void StartService () {System.out.println ("My service start ..."); }

}

Import Javax.management.attribute;import Javax.management.attributenotfoundexception;import Javax.management.instancenotfoundexception;import Javax.management.invalidattributevalueexception;import Javax.management.mbeanexception;import Javax.management.mbeanserver;import javax.management.MBeanServerFactory; Import Javax.management.malformedobjectnameexception;import Javax.management.objectname;import Javax.management.ReflectionException;

Import Com.sun.jdmk.comm.HtmlAdaptorServer;

/** * Agent class * Stardbeandemomain.java * @author ZMXJ */public class Stardbeandemomain {

    private Mbeanserver mbserver = null;        objectname Mbeanobjectname = null; string Domain = null; string mbeanname = "Standbean";     public static void Main (string[] args) {                 Stardbeandemomain Sbean = new Stardbeandemomain ();                 Sbean.doregistbean ();        Sbean.domanagebean ();        sbean.reghtmladaptor ();   }        /**     * Create mbeanserver and register a mbean      */    private void Doregistbean ()     {        //create mbeanserver        Mbserver = Mbeanserverfactory.creatembeanserver () ;         domain = Mbserver.getdefaultdomain ();

     try {         mbeanobjectname = new objectname (domain + ": Type= "+ mbeanname);                 /*         //Here you can create and register an Mbean to mbeanserver.          Server.creatembean (mbeanname,mbeanobjectname);         */                   standbean bean = new Standbean ();          Mbserver.registermbean (Bean, mbeanobjectname);                   System.out.println ("Register Standmbean sucess ...");     &nbsp} catch (Malformedobjectnameexception e) {         E.printstacktrace ();         system.exit (1);     &NBSP} catch (Exception e) {         e.printstacktrace ();         system.exit (1);     }        }



* * Demo admin Mbean, through Mbserver modify Mbean properties and execute Mbean method * * private void Domanagebean () {try {Obje                                   Ctname mbeanobjectname = new objectname (domain + ": type=" + mbeanname); System.out.println ("Attribute:state-" +mbserver.getattribute (Mbeanobjectname, "State"));

           //Modify Properties state             attribute Stateattribute = new Attribute ("state", "new state");             Mbserver.setattribute (Mbeanobjectname, Stateattribute);                         //Get property value             System.out.println ("Attribute:state 2-" +mbserver.getattribute (Mbeanobjectname, "State"));                         //Call method startservice            MBserver.invoke ( Mbeanobjectname, "StartService", Null,null);                   } catch (Exception e) {            E.printstacktrace ();       }   }        /*     * Registration Htmladaptor, after registration can be performed via http://localhost:8082 Mbean management operations       */    private void Reghtmladaptor ()     {      Htmladaptorserver html = new Htmladaptorserver ();     objectname html_name = null;      try {         html_name = new ObjectName ("adaptor:name=html,port=8082 ");         Mbserver.registermbean (HTML, html_name);     } catch (Exception e) {         System.out.println ("\ t!!! Could not create the HTML adaptor!!! ");          e.printstacktrace ();      &NBsp;  return;     }     html.start ();    }}


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.