Starting the RMI Registry
Earlier, I alluded to a RMI registry and said that a JNDI lookup
is done when accessing the service. However, right now we
don't have a RMI registry running, so a JNDI lookup will
fail. A RMI registry started may be started manually or
programmatically.
Using the Command Line
On your Windows or Linux command line, type the following to
start a RMI registry:
rmiregistry &
This will start the RMI registry at your default host and port,
localhost and 1109, respectively. However, for our web application
we cannot rely on an RMI registry being available on application
start and would rather take care of this in our code.
Programmatically Starting the RMI Registry
To programmatically start the RMI Registry, you can use the
LocateRegistry.createRegistry(int port) method. The
method returns an object of the type Registry. We store
this reference, as we would like to stop the registry on application
end. Right before we start our JMXConnectorServer in
JMXAgent.start(), we first start an RMI registry using
the following line of code:
registry = LocateRegistry.createRegistry(
Constants.RMI_REGISTRY_PORT);
On application end, after stopping the
JMXConnectorServer in JMXAgent.stop(),
the following method is called to stop the registry:
UnicastRemoteObject.unexportObject(registry, true);
Note that the StartupListener class triggers
application start and end tasks.
Accessing our JMX Service
There are a number of ways we can access JSR 160 services. We
may do so programmatically or by using a GUI.
Connecting using MC4J
Deploy the application by copying jmxapp.war to Tomcat's
webapps directory. Download and install MC4J. Once
installed, create a new Server Connection of the type JSR 160 and
specify the Server URL that was printed in the application server
logs on application startup. In my case, it was:
service:jmx:rmi://zarar:9589/jndi/rmi://localhost:1100/jmxapp
Supply the username and password, which MC4J refers to as
"Principle" and "Credentials," respectively. Clicking Next takes
you to a screen where you can customize your classpath. Default
settings should work fine, and you can click on Finish to connect
to the JMX service. Once connected, browse the MC4J tree structure
as shown in Figure 1 until you reach the Properties option of the
LoginStats MBean implementation.

Figure 1. MC4J view
Clicking on the Properties option displays the statistics, as
shown in Figure 2:

Figure 2. Properties window
Connecting to a "Cluster" using jManage
Deploy the application by copying jmxapp.war to Tomcat's
webapps directory. Note the URL that is printed on
application startup. Next, deploy another instance of this
application by changing the RMI_REGISTRY_PORT and
MBEAN_SERVER_PORT variables in the
Constants class so that the second instance of the
application will not try to use ports that are already in use.
Change the app.name property in the build.xml
file so that the new instance will be deployed in a different
context (e.g., jmxapp2). Do an ant clean war, which will
create jmxapp2.war in the base directory. Copy
jmxapp2.war to Tomcat's webapps directory. The
application will deploy and now you have two instances of the same
application running. Again, note the URL that is printed on
startup.
Download and install jManage. Once installed, use
jManage's web interface to create a JSR 160 application by
following the Add New Application link found on the home page.
The Add Application page is shown in Figure 3:

Figure 3. Add Application page
Once again, use the appropriate username, password, and URL.
Repeat the steps for the second application that is deployed. Once
you have created the two applications, you must create a cluster by
following the Add New Application Cluster link found on the home
page. Simply add the two applications you have already created to
your cluster, as shown in Figure 4:

Figure 4. Add Application Cluster page
That's it, we are done! From the home page, click on one of the
applications in the cluster and then click on the Find More
Objects button. You will see the name=LoginStats MBean; click on
it, and you will see the FailedLogins and
SuccessLogins attributes that we have exposed.
Clicking on the Cluster View link on the same page will display a
page similar to Figure 5, where a running count of statistics from
both applications can be seen:

Figure 5. Cluster view for jmxapp and jmxapp2
Try a few logins on both applications
(http://localhost:8080/jmxapp and
http://localhost:8080/jmxapp2) and see how the numbers
change.
Conclusion
You now know how to "JMX enable" your new and existing web
applications and securely manage them using MC4J and jManage.
Although J2SE 5.0 provides a powerful implementation of the JMX
specification, other open source projects such as XMOJO and
MX4J provide additional
features, such as connecting via web interfaces and more. Interested
readers who want to learn more about JMX will find Java
Management Extensions by J. Steven
Perry a very useful book. For those interested in remote
application management, Connecting JMX Clients and Servers
by Jeff Hanson is a valuable resource that provides real-world
examples.
Resources