The XMOJO Project
<< Prev Chapter 5.1 Standard MBean Example Next >>

Standard MBean:  A Working Example


To know about Standard MBean, please refer to the StandardMBean section.

 About this example 

In this example, we will see how the MBean attributes and operations are exposed using introspection. Here, we will be using the following resources:
  1. A standard MBean for managing a Web server information -- ServerInfoMBean.java and the implementation details ServerInfo.java
  2. A standard MBean with which we will explain how the introspection is done -- OverloadedMBean.java and the implementation details Overloaded.java
  3. A simple JMX agent for registering the above-mentioned standard MBeans and adaptors -- RunStandardAgent.java

 ServerInfo 

The ServerInfo class implements the ServerInfoMBean interface and is a simple example for a standard MBean.  This standard MBean exposes four attributes ( ServerName, ServerId,  ServerStarted, and Port)  and two operations (startService and stopService).  An additional method, restart, was defined in the ServerInfo.java, but it will not be exposed for management, since it is not defined in the management interface, that is, ServerMBeanInfo.java.

 Overloaded 

This class is also a standard MBean.  It implements the OverloadedMBean interface.  This standard MBean is created just to explain the introspection mechanism, i.e, how the MBean attributes and operations are introspected and exposed for management.

The Overloaded class implements the following four methods of the OverloadedMBean interface:

public int getState();

public int getState(int value);

public void setPassword(String name);

public void setPassword(String name, String newname);


As discussed in the StandardMBean section, the semantics for a read-write attribute is

public
AttributeType get<AttributeName>( );
public void set<AttributeName>(AttributeType obj);


The OverloadedMBean interface defines a method public int getState( )and does not define a method, such as public void setState(int param0)
So, the attribute State becomes a read-only attribute.  

Also, the getState method is overloaded.  There is a getState method which takes an int as an argument.  This method cannot be a getter method, since the getter methods will not take any arguments, and this method is exposed as one of the operations.

Similarly, the Overloaded class defines a method public void setPassword(String name) which follows the attribute semantics.  There is no method defined like public String getPassword( ). So, the attribute Password becomes a write-only attribute.

Also, the setPassword method is overloaded.  It defines one more setPassword method which takes two arguments.  This method cannot be a setter method, since the setter methods will take only one argument.  Hence, this method is exposed as one of the operations.

Thus, the management interface OverloadedMBean defines the following management information:

Attributes
  1. State -- read-only attribute of type int.
  2. Password -- write-only attribute of type String.
Operations
  1. getState(int i) -- returns int
  2. setPassword(String s1, String s2) -- returns void

 Running the Example 

 For Windows OS 

  1. Change your working directory to %XMOJO_HOME%/examples/mbeans/standard directory.
  2. Execute the build.bat file (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 file  (This batch file executes the RunStandardAgent 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.

 For Unix OS 

  1. Change your working directory to $XMOJO_HOME/examples/mbeans/standard directory.
  2. Execute the build.sh file (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 file (This script file executes the RunStandardAgent 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 establishes a connection with the RMI Server.

The registered MBeans is listed in the left frame.  Under the domain MyStdMBean, two MBeans with the names id=1,type=server and type=overloaded are listed.  Click the Attributes of the ServerMBean ( id=1,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 the Operations of the ServerMBean.  Two operations, startService and stopService, are listed.  On invoking the operations or modifying the read write attribute, appropriate methods are invoked.

Similarly, the other MBean (overloaded MBean) contains two attributes, State and Password.  State is a read only attribute and Password is of type write only (only write access is allowed, and no read access).   The MBean also contain two operations, getState and setPassword.

 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 MyStdMBean link under Domains.  Two MBeans are listed.  
  1. Click the link MyStdMBean:id=1,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.  Two operations, startService and stopService, are listed.  On invoking the operations or modifying the read write attribute, appropriate methods are invoked.
  2. Click the link MyStdMBean:type=overloaded.  It lists one read only attribute and one write only attribute.  The read only attribute is State with the value 1.  The name of the write only attribute is Password.  Click on the image with the name Operations.  Two operations, getState and setPassword, are listed.  Invoke the getState operation passing the value 32.  Click the Continue button.  Click the link MyStdMBean:type=overloaded.  You will find the read only attribute State is set with the value 32.

<< Prev Home Next >>
Examples
Dynamic MBean Example