To know about Model MBean, please refer to the ModelMBean
section.
About the Example
This example shows how to expose the management information of a Web server
using model MBeans. It also shows how an existing class, which does not know
anything about JMX, can be wrapped as an MBean that serves for management.
The following resources are used:
An XML file for exposing the model MBean attributes and operations --
ServerInfo.xml
A class which contains the implementation details of the managed resource
(Web server) -- ServerInfo.java
A class which contains the implementation details for constructing
a model MBean object using the API -- ModelServerInfo.java
This XML file contains the management information. The Model MBean attributes,
operations, notifications, and constructor descriptors are defined in this
XML file.
ServerInfo
This class contains the implementation details of a web server. Though this
class contains the implementation details, it is not a model MBean.
ModelServerInfo
This is the model MBean class. This class contains sample code for creating
a ModelMBean using API. (This class defines a method called getMBeanInfo,
which returns an object of type ModelMBeanInfo.)
RunModelAgent
This class is a simple JMX Agent which registers the required adaptors and
the model MBean. This class obtains the model MBean object in two ways:
If the argument passed while invoking the main method is "xml", the
ServerInfo.xml file will be converted into a ModelMBeanInfo object. A RequiredModelMBean
object will be created out of this ModelMBeanInfo object. The managed resource
for this RequiredModelMBean will be set to the ServerInfo class and registered
with the MBeanServer.
If no argument is passed or any argument other than the string "xml"
is passed, the ModelMBeanInfo object is created using the available API methods.
The getMBeanInfo method of ModelServerInfo class is invoked, which returns
a ModelMBeanInfo object. A RequiredModelMBean object will be created out
of this ModelMBeanInfo object. The managed resource for this RequiredModelMBean
will be set to the ServerInfo class and registered with the MBeanServer.
Running the Example
For Windows OS
Change your working directory to examples/mbeans/model directory.
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.)
Execute the run.bat (This batch file executes the RunModelAgent class.)
Note: If the model MBean has to be created using the XML file,
then invoke the run.bat by passing the string argument "xml". In the command
prompt, typerun.bat xml
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 Linux OS
Change your working directory to examples/mbeans/model directory.
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.)
Execute the run.sh (This script file executes the RunModelAgent class.)
Note: If the model MBean has to be created using the XML file,
then invoke the run.sh by passing the string argument "xml". In the console,
type sh run.sh xml
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.
Under the domain ModelDomain, an MBean with the name name=ServerInfo will
be listed. It will expose seven attributes, of which six are read only and
three operations. Thus, the management information of a Web server is exposed
using model MBeans.
Using the Web Browser
Open any Web browser and type the URL http://localhost:8030. If you are testing
from a remote machine, 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, 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
ModelDomain link under Domains. Two MBeans will be listed. Click the link
ModelDomain:name=serverInfo. It should list one read write attribute and
six read only attributes. The read write attribute will be Port and with
the value 8072. The read only attributes will be RestartCount, ServerId,
ServerName, ServerUpTime, StartTime, and ServerStarted. Thus, the management
information of a Web server is exposed using model MBeans.