JMX Learning Notes (i)-mbean

Source: Internet
Author: User

Source: http://blog.csdn.net/qiao000_000/article/details/6061808

JMX is Java Management Extensions Java Management extension
MBean is the managed beans managed beans

An Mbean is a managed Java object, a bit like JavaBean, a device, an application, or any resource that can be represented as Mbean,mbean exposes an interface that can read or write properties from some object, Usually an mbean needs to define an interface that ends with an mbean, for example: Echombean, in the form of Xxxmbean, which is a specification that must be adhered to. For example:

Java code
    1. package  com.haitao.jmx;   
    2.   
    3. /**  
    4.  * jmx mbean interface  
    5.  *    
    6.  *  @author  haitao.tu  
    7.  *   
    8.  */  
    9. public   interface   echombean {  
    10.   
    11.      public   void  print (string  YourName);   
    12.   
    13. }  

Very simple, in the Echombean interface, defines a print method, with a yourname string type parameter, only the interface seems to be useless, we implement this interface below

Java code
  1. Package com.haitao.jmx;
  2. /**
  3. * Implements of JMX Echombean
  4. *
  5. * @author Haitao.tu
  6. *
  7. */
  8. Public class Echo implements Echombean {
  9. @Override
  10. Public void print (String yourName) {
  11. System.out.println ("Hi" + yourName + "!");
  12. }
  13. }

Echo implements the Echombean interface, very simple we just print the Hi yourname!

According to the definition of JMX, is the managed object, now we just define the object, not managed, then we let the Echo class instance object is managed:

Java code
  1. Package com.haitao.jmx;
  2. import java.lang.management.ManagementFactory;
  3. import javax.management.MBeanServer;
  4. import javax.management.ObjectName;
  5. /**
  6. * JMX App Demo
  7. *
  8. * @author Haitao.tu
  9. */
  10. Public class App {
  11. Public static void main (string[] args) throws Exception {
  12. //Create Mbeanserver
  13. Mbeanserver mbs = Managementfactory.getplatformmbeanserver ();
  14. //New Mbean ObjectName, identifying registered Mbean in Mbeanserver
  15. ObjectName name = new ObjectName ("Com.haitao.jmx:type=echo");
  16. //Create Mbean
  17. echo Mbean = new Echo ();
  18. //Register Mbean in Mbeanserver, identified as ObjectName (Com.tenpay.jmx:type=echo)
  19. Mbs.registermbean (Mbean, name);
  20. //Call the Print method of the registered Echombean in Mbeanserver
  21. Mbs.invoke (name, "print", new object[] { "Haitao.tu"}, new string[] {"   Java.lang.String "});
  22. Thread.Sleep (Long.max_value);
  23. }
  24. }

1. First we applied a Mbeanserver object to Managementfactory in the app class
2. Then we want to make Echo's instance object managed, we need to give this object an identity, which is objectname. Note the ObjectName constructor, which uses the form (Package name: Type= class name).
3. We then registered echo through the Mbs.registermbean method and passed in objectname to identify the Mbean in Mbeanserver.
4. We then invoke the registered Echo's Print method through the Mbs.invoke method, find the Mbean through objectname, and pass the last two parameters, the parameters that are executed by the Print method, and the type of the parameter.
5. Finally we sleep the main thread, waiting for other threads to call.

In this example, we can see that the benefits of Mbean, before Echo's instance object is not managed, can only be called by the Echo object's handle to the public method in Echo, after being managed, We can invoke the Print method of the Echo object through the Mbeanserver handle MBS.

JMX Learning Notes (i)-mbean

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.