Register mbean favorites
Register mbean
One mbeanserver maintains the mbean registry in a JMX proxy. mbean can be registered in either of the following two methods:
1) Create an mbean instance and register it with mbeanserver:
Public objectinstance registermbean (Object object, objectname name) Here, object is the created mbean instance, and name is a unique identifier of mbean.
2) use one of the creatembean methods:
Public objectinstance creatembean (string classname, objectname name) Public objectinstance creatembean (string classname, objectname, objectname loader) Public objectinstance creatembean (string classname, objectname, object [] Params, string [] types) Public objectinstance creatembean (string classname, objectname, objectname loader, object [] Params, string [] types) The creatembean method uses Java introspection to create an mbean instance. for all creatembean methods, two variables are common.
String classname -- Name of the class of the mbean instance to be created.
Objectname name -- Name of the object to be registered in mbean.
If two variables are used as the creatembean constructor, the default class loader is used to load the mbean class, And mbean is initialized using the default constructor. if mbean has been initialized, this instance will be registered by mbeanserver with the Object Name of the second variable. if you want to use the specified class loader, you can use the constructors of three variables. this class loader will be registered as an mbean by mbeanserver. the object name of this class loader will be used as the third variable.
When instantiating a class, if any parameter must be passed in, you can use the creatembean method of the four parameters. the last two parameters of the creatembean method include the object array (required for class initialization) and their signatures. if the mbean class must be loaded with another specified class loader, use the constructor with five parameters.
Register multiple instances of one mbean
You may need to monitor more than 10 web servers. if the management information of all Web servers is the same, you do not have to write ten mbeans. an mbean is enough. we can create multiple instances and register each instance with a unique object name.
Now we can see the importance of using object names to avoid confusion.
Obviously, the port number of the server and the name of the server are unique to any server. That's because, on one machine, you cannot run two servers on one port.
Therefore, you can create an object name, as shown in figure
Servers: Hostname = localhost, portnumber = xxxx
Assume that more than one server is started on the same machine, the object name should be
Servers: Hostname = localhost, portnumber = yyyy
Because the object name is the unique identifier of mbean registration, multiple mbean instances are registered as different object names.
Object names are usually distinguished by key value pairs they contain, such as the host name and port name Keywords of a server mbean and their respective values. similarly, for mbean groups with similar unique identifiers, object names are assembled with corresponding keywords and values. for example, type = servermbean indicates that this mbean is a server mbean type. therefore, the object name value server: TYPE = servermbean, hostname = localhost, portnumber = 8050 provides a simple mbean group, which is generated as the implementation of the server mbean. in addition, a method is provided to uniquely identify the mbean instance of the server running on localhost: 8050.
A simple example
Let mbeans that provide management information (common to all web servers) Be dynamic mbeans and name them serverinfo.
Let the two web servers run on ports 8080 and 80 of machine A (Machinea. let a web server run on port 8080 of machine B (machineb), and another Web Server Run on port 80 of machine C (machinec. in short, we need to manage these four web servers. we can use the serverinfo class and create four instances to manage each web server. register four mbeansCodeThe snippet is as follows:
Serverinfo srvr1 = new serverinfo (); serverinfo srvr2 = new serverinfo (); serverinfo srvr3 = new serverinfo (); serverinfo srvr4 = new serverinfo ();
Mbeanserver MBS = NULL;
Try {MBS = mbeanserverfactory. creatembeanserver (); MBS. registermbean (srvr1, new objectname ("server: Hostname = Machinea, portnumber = 8080"); MBS. registermbean (srvr2, new objectname ("server: Hostname = Machinea, portnumber = 80"); MBS. registermbean (srvr3, new objectname ("server: Hostname = machineb, portnumber = 8080"); MBS. registermbean (srvr4, new objectname ("server: Hostname = machinec, portnumber = 80");} catch (exception e) {e. printstacktrace ();}
Cancel mbean
Mbean can be deregistered using the following method of mbeanserver:
Public void unregistermbean (objectname name) Here name is the name of the object registered by this mbean instance.
After mbean is deregistered, mbeanserver no longer saves any association of this mbean instance.
Control mbeanregistration
JMX defines an interface called mbeanregistration. The interface aims to allow mbean developers to control registration and cancellation on mbeanserver. This is achieved through the javax. Management. mbeanregistration interface of mbean.
The mbeanregistration interface defines the behavior of the registration control mechanism. It defines the following four methods:
Public objectname preregister (mbeanserver MBS, objectname name) Public void postregister () Public void prederegister () Public void postderegister ()
All of the above methods are callback methods,
Mbeanserver will call these methods at the right time.
If an mbean implements mbeanregistration and the mbean is registered, mbeanserver calls the preregister method before registration. the object name returned by this method will be used during mbean registration. after mbean registration is completed, mbeanserver calls the postregister method.
If the above mbean is canceled, mbeanserver calls the prederegister method before the cancellation. If the cancellation is successful, mbeanserver calls the postderegister method.
Notes
For a single mbean class, multiple instances can be created and registered as the object name provided by mbean at registration.
Whenever an mbean is registered, mbeanserver creates a message of Type JMX. mbean. Created. mbeanserverdelegate mbean to broadcast the message to all registered listeners.
The mbeanregistration interface provides hooks for monitoring the registration and cancellation processes.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zhangliulin/archive/2008/02/17/2100194.aspx