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
- package com.haitao.jmx;
-
- /**
- * jmx mbean interface
- *
- * @author haitao.tu
-  *   
-  */  
- public interface echombean {
-
- public void print (string YourName);
-
- }
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
- Package com.haitao.jmx;
- /**
- * Implements of JMX Echombean
- *
- * @author Haitao.tu
- *
- */
- Public class Echo implements Echombean {
- @Override
- Public void print (String yourName) {
- System.out.println ("Hi" + yourName + "!");
- }
- }
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
- Package com.haitao.jmx;
- import java.lang.management.ManagementFactory;
- import javax.management.MBeanServer;
- import javax.management.ObjectName;
- /**
- * JMX App Demo
- *
- * @author Haitao.tu
- */
- Public class App {
- Public static void main (string[] args) throws Exception {
- //Create Mbeanserver
- Mbeanserver mbs = Managementfactory.getplatformmbeanserver ();
- //New Mbean ObjectName, identifying registered Mbean in Mbeanserver
- ObjectName name = new ObjectName ("Com.haitao.jmx:type=echo");
- //Create Mbean
- echo Mbean = new Echo ();
- //Register Mbean in Mbeanserver, identified as ObjectName (Com.tenpay.jmx:type=echo)
- Mbs.registermbean (Mbean, name);
- //Call the Print method of the registered Echombean in Mbeanserver
- Mbs.invoke (name, "print", new object[] { "Haitao.tu"}, new string[] {" Java.lang.String "});
- Thread.Sleep (Long.max_value);
- }
- }
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