The XMOJO Project
<< Prev Chapter 5.2 Dynamic MBean Example Next >>

Dynamic MBean: A working Example


To know about Dynamic MBean, please refer the DynamicMBean section.

 About the Example 

In this example, we will see how to dynamically expose the management information when some condition is satisfied.  Here, we will be using the following resources:
  1. A dynamic MBean for managing a Web server information -- DynamicServerInfo.java and the actual class that contains the management information -- ServerInfo.java
  2. A dynamic MBean for configuring a logger -- DynamicLogConfig.java
  3. A simple JMX agent for registering the above-mentioned dynamic MBeans and adaptors -- RunDynamicAgent.java
  4. A class that acts as a NotificationListener -- ServerStateChangeListener.java

 DynamicServerInfo 

This class implements the DynamicMBean interface.  The MBean information is obtained from the getMBeanInfo method.  This MBeanInfo is dynamic.  Initially, this MBean exposes four attributes and four operations.  The attributes are ServerName, ServerId, ServerStarted, and ServerPort.  The operations are startService, stopService, showDetails, and hideDetails.  When the operation showDetails is invoked, the MBeanInfo changes.  Now, seven attributes and five operations will be exposed.  The additional three attributes are StartTime, ServerUpTime, and RestartCount.  The additional operation is restart.  The original management information that is exposed initially can be obtained by invoking the hideDetails operation.

The DynamicServerInfo class does not provides any implementation for obtaining the management information.  The implementation details of Web server management is available in the class ServerInfo.  DynamicServerInfo makes use of the ServerInfo class (package: examples.applications.server) and thus, provides a DynamicMBean as a wrapper for the ServerInfo.

Whenever the startService or the stopService operation is performed, a notification of type server.started or server.stopped will be sent respectively.  This notification is handled in the handleNotification method defined in the ServerStateChangeListener class.

 DynamicLogConfig 

This class is also a dynamic MBean since it implements the DynamicMBean interface.  Initially, the MBeanInfo returned by the getMBeanInfo method contains five attributes and one operation.  All the five attributes will be readOnly and their names are LogLevel, LogFileName, LogDirectory, MaxLines, and RotationLimit.  The operation name is stopLogging.  

When the operation stopLogging is invoked, the existing stopLogging operation will be removed, a new operation startLogging will be added, and all the five attributes will become readWrite, so that they can be modified.  For example, LogLevel can be modified from INFO to DEBUG.  For the changes to take effect, startLogging has to be performed.  On invoking startLogging, all the attributes become readOnly and the operation stopLogging replaces the startLogging operation.

 Running the Example 

 For Windows OS 

  1. Change your working directory to %XMOJO_HOME%/examples/mbeans/dynamic directory.
  2. Execute the build.bat (This batch file compiles the Java files under the src directory and the output class files will be stored in the classes directory).
  3. Execute the run.bat  (This batch file executes the RunDynamicAgent class).
The JMX Agent will be started.  RMI Server will be started at port 1099.  HTML Server will be started at port 8030.  For testing the example, refer the section Testing the Agent given below.

 For Unix OS 

  1. Change your working directory to $XMOJO_HOME/examples/mbeans/dynamic directory.
  2. Execute the build.sh (This script file compiles the Java files under the src directory and the output class files will be stored in the classes directory).
  3. Execute the run.sh  (This script file executes the RunDynamicAgent class).
The JMX Agent will be started.  RMI Server will be started at port 1099.  HTML Server will be started at port 8030.  For testing the example, refer to the section, Testing the Agent given below:

 Testing the Agent 

 Using the RMI Client 

Start the MBeanBrowser tool by executing the mbeanbrowser.bat/mbeanbrowser.sh file under XMOJO_HOME/bin directory.
Connect to the RMI Server.  (In the menu bar, choose Settings >> Client Settings.  Configure the HostName and PortNumber; here, HostName is the machine name where the RMI Server is running.  After configuring the client settings, choose Operations >> Connect).  This will establish a connection with the RMI Server.

The registered MBeans will be listed in the left frame.

Testing the DynamicServerMBean
Under the domain MyDynMBean, an MBean with the name name=localhost,type=server is present.  Under this MBean node, click Attributes. It lists three read only attributes, one read write attribute.  Click Operations.  It lists four operations.  Click the operation showDetails.  Now, once again click Attributes.  Three more attributes are added.  Also, in the operations, one more operation restartServer will be added.  In the Menu bar, choose view >> Notifications.  This opens the notification dialog.  Click Operations.  Invoke the operation stopService.  Now, a notification of type server.stopped with the messageServer stopped will be sent by the DynamicMBean.  This notification will be broadcasted to all the listeners.  You can find the print statements in the agent side, and the notification will be shown in the notification dialog.

Testing the DynamicLoggerMBean
Under the domain MyDynMBean, an MBean with the name id=1,type=logger is present.  Under this MBean node, click Attributes.  Five read only attributes are listed.  Click Operations.  Only one operation with the name stopLogging is listed.  Invoke this operation.  Now, once again click Attributes.  All the read only attributes will have write access now.  Also, the operation stopLogging would be replaced with startLogging.  Modify some of the attributes.  Click Operations.  Invoke the operation startLogging.  Now, the attributes would have changed to read only.

 Using the Web Browser 

Open any Web browser and type the URL http://localhost:8030.  If you are testing from a remote machine, then enter the URL http://hostName:8030.  Here hostName is the machine name where the JMX agent is running.  If the HTML Adaptor is listening at some other port, then replace the 8030 with the appropriate port number in the above URL.

The index page lists the various domains and some useful links for viewing notifications, searching the MBeans, customizing MBean views, etc..  Click the MyDynMBean link under Domains.  Two MBeans will be listed.  
  1. Click the link MyDynMBean:name=localhost,type=server.  It lists one read write attribute and three read only attributes.  The read write attribute is Port with the value 8072.  The read only attributes are ServerName, ServerId, and ServerStarted with the values test-server, test-server_1, and true respectively.   Click on the image with the name Operations.  Four operations, startService, stopService, showDetails, and hideDetails, are listed.  Invoke the showDetails method.  Click the Continue button.  An additional operation restart is exposed.  Click the link MyDynMBean:name=localhost,type=server.  Additional three read only attributes, StartTime, ServerUpTime, and RestartCount, are exposed for management.  Click on the image with the name Operations.  Invoke the operation stopService.  Click the link Home.  Click the link View Notifications.  A notification of type server.stopped with the message Server stopped is displayed.
  2. Click the link MyDynMBean:id=1,type=logger.  It lists five read only attributes.  The read only attributes are LogLevel, LogFileName, LogDirectory, MaxLines, and RotationLimit and their values are INFO, test.log, C:\mylogs, 1000, and 10 respectively.  Click on the image with the name Operations.  Only one operation stopLogging is listed.  Invoke the stopLogging operation.  Click the Continue button.  The operation stopLogging will not be exposed, instead, a new operation startLogging will be exposed.  Click the link MyDynMBean:id=1,type=logger.  You will find all the read only attributes now have write permissions also.  Modify some of the attributes.  Click the Save Values button. Click on the image with the name Operations.  Invoke the operation startLogging.  Click the Continue button.  Click the link MyDynMBean:id=1,type=logger.  The five attributes now become read only attributes.

<< Prev Home Next >>
Standard MBean Example
Model MBean Example