The XMOJO Project
<< Prev Chapter 2.2.2 Writing Dynamic MBean Next >>

Writing Your Own Dynamic MBean


Let us create a dynamic MBean for managing the information of the same Web server which we managed previously by creating a standard MBean.  The following tables summarize the management information that our dynamic MBean exposes:

Attribute Information
Attribute Name
Attribute Type
Attribute Access
Attribute Description
ServerName java.lang.String
read only
The name of the machine where the server is running.
ServerId java.lang.String
read only
An identifier for the Server.  In a same machine, multiple servers may run at different ports.
ServerStarted boolean
read only
To check whether the server is alive.
ServerPort int
read write
The port at which the server is listening.
StartTime
java.util.Date
read only
To show the latest start time of the server.
ServerUpTime
long
read only
To show the server uptime.
restartCount
int
read only
To show the number of times the server has been restarted.

The attributes StartTime, ServerUpTime and restartCount will be exposed only if the showDetails method is invoked.  The same exposed attributes can also be hidden by invoking tde hideDetails metdod.  The rest of the attributes ServerName, ServerId, ServerStarted and ServerPort will be exposed throughout.

Operation Information
Operation Name
Signature
Parameters
Operation Description
startService void -
To start the server.
stopService void -
To stop the server.
showDetails void -
To show additional details, such as serverStartTime and number of times Server restarted. 
hideDetails void -
To hide the additional details.
restart
void -
To restart the server.

The operation restart will be exposed only if the showDetails method is invoked.  The same exposed operation can also be hidden by invoking the hideDetails metdod.  The rest of the operations, startService, stopService, showDetails, and hideDetails, will be exposed throughout.

Download the DynamicServerInfo.java file which contains the implementation details.

When the showDetails operation is invoked, the boolean variable flag will be set true.  When the flag is true, getMBeanInfo returns an MBeanInfo object which contains seven attributes and five operations.

When the hideDetails operation is invoked, the boolean variable flag will be set false.  When the flag is false, getMBeanInfo returns an MBeanInfo object which contains four attributes and four operations.

Since, the MBean is constructed dynamically at run time using the MBeanInfo returned by getMBeanInfo method, one can control the information available for management.

To see a working example of the above Dynamic MBean, try out this example.

<< Prev Home Next >>
When Dynamic MBean Model MBean