|
DEVELOPER: Trends
Monitor, Control, and Extend with JMX
By Cameron O'Rourke
Improve your applications with Java Management Extensions.
One of the most interesting and yet overlooked features recently added to the Java platform is Java Management Extensions (JMX). The purpose of JMX is to provide a standard interface for monitoring and controlling Java applications. But the JMX architecture is so flexible that it
is capable of much more than that. JMX has already started appearing in Oracle products (see Next Steps) and many other third-party products. In this column, I'll explain the basics
of JMX and how it can
help you build better
Java applications.
The Basics
The JMX framework can improve the manageability of your applications by letting you see and adjust what is going on inside:
- You can monitor your application for critical events, performance problems, error conditions, and statistics. For example, you could be notified of a large increase in traffic or a sudden drop in performance.
- You can make your application more controllable and adjustable at runtime by directly exposing application APIs and parameters. For example, you could switch the database connection to an alternative server or change the number of rows shown for each request.
- You can change the level of debugging and logging as the application runs and invoke debug functions within the application without having to shut it down.
- You can interface JMX with your hardware, database, and application servers to monitor the health and performance of your infrastructure.
You gain insight into your applications and infrastructure through modular plug-ins called Managed Beans (MBeans). MBeans integrate with your applications, components (such as Enterprise Java-Beans), or other resources to expose
properties (values) and operations. A management application (sometimes referred to as a client or a console) is able
to see and change these properties and invoke operations.
The MBeans themselves are managed inside an MBean Server. The MBean Server is responsible for keeping track of the running MBeans and for providing a uniform way to access them. MBeans are never invoked directly; all requests must go through the MBean Server.
A JMX Agent is a Java process that contains the MBean Server and provides other JMX services such as timers, class loaders, and protocol adapters. A JMX Agent is something that you can run standalone, or it could be built into something else, such as an application server.
A Simple Example
Let's look at a very simple example that still does something useful. We would like to monitor a Web site to be sure that it is up and operating by periodically sending a request for the home page. We need to be able to configure the Web site address and the request interval and get back a site condition (OK, WARNING, ERROR, or UNKNOWN) with the HTTP status code.
We will create an MBean called SiteMonitor to perform these functions. The rules for creating this type of MBean are quite simple: The MBean must implement a corresponding MBean interface, must have a public constructor, and must be a public, concrete class.
The first step in developing our SiteMonitor MBean is to write its Java interface, called SiteMonitorMBean. The interface describes the methods that our MBean makes available through the MBean Server. It declares four properties and two operations, as shown in Listing 1. The "MBean" suffix is significant and tells the JMX Agent that this interface describes an MBean.
Next, we write a Java class that implements the SiteMonitorMBean interface. The SiteMonitor class allows the Interval and SiteAddress properties to be modified by a management console and allows a monitor thread to be started and stopped. As the monitor thread is running, it simply makes an HTTP HEAD request to the Web server, reads the HTTP response code, and
then sleeps for the specified interval. The SiteMonitor class is shown in Listing 2.
The JMX reference implementation includes an HTML adapter, so we can connect to the JMX Agent from any Web browser. Using the HTML interface, we can monitor and control both the JMX Agent and the registered MBeans.
As the monitor is running, we can change the SiteAddress, and the MBean will then monitor the new address. What is noteworthy is that this is done without our having to stop and restart our program.
So What Have We Accomplished?
We have built a simple management component that can be monitored and controlled from any other program, including any JMX management console. The JMX MBean Server can instantiate any number of MBeans, so we can have several SiteMonitors running simultaneously.
The fact that our component is exposed to the outside world and that it can be configured at runtime greatly increases its value. If an MBean is coded correctly, it can run continuously without ever having to be restarted.
The JMX Agent also provides other services, such as notification, a timer,
a relationship service, and dynamic MBean loading from XML files. MBeans can use these services to cooperate and combine into more-complex management scenarios.
This ability to dynamically load and loosely couple components provides a lot of flexibility. Next, we will look at different ways that JMX can be used.
Architecting with JMX
There are several ways that MBeans can interact with your application and with each other. Obviously, if you are writing a new application, you will have more flexibility than when you are integrating MBeans with an existing application. Figure 1 shows four different ways to architect an application with JMX.
Figure 1: Different ways to architect with JMX
- You are trying to expose an existing application. Your MBeans will have to communicate by using whatever interfaces are available. This might mean being able to use existing APIs, reaching into the underlying database tables, or doing nothing more than accessing the screens and simulating a user. The benefit is that you'll be able to manage your application in a uniform and flexible way.
- If you have access to the source code, then you have more options. Your MBeans can implement a finer degree of monitoring and control over the application. In the second case, you start the application from within a JMX Agent and the MBeans interact with the various system components, using either existing APIs or new APIs that you create.
- If you are building a new system, you can create it as a set of MBeans and let the MBean Server manage your system's runtime lifecycle. I think that this is the most exciting aspect of JMX. In this case, the MBean Server acts as a framework for your application, giving it a very flexible and controllable component architecture. You can add, change, or replace components as necessary and couple them together at runtime. Several open source projects have taken this approach.
- If you have a distributed system, you might need to deploy MBeans on several different hosts. Some JMX implementations, such as MX4J, provide a proxy MBean that exposes a remote MBean
for local management.
Oracle Support for JMX
Oracle is involved in JSR-77, which defines the way a Java 2 Platform, Enterprise Edition (J2EE) application server is managed via JMX. Oracle Application Server will expose a
JMX Agent, and Oracle Enterprise Manager will provide a generic MBean browser that will allow customers to manage custom MBeans.
The OC4J 10.0.3 Developer Previewavailable on Oracle Technology Network (OTN)provides an early look at this support. If you install OC4J on your local machine, you can access its JMX console by entering http://localhost:8888/
adminoc4j in your browser. You can also deploy your own MBeans to OC4J; to deploy the SiteMonitor MBean to OC4J 10.0.3, follow these steps:
- Package the MBean class and interface into a JAR file and copy it to <OC4J_ home>/j2ee/home/applib.
- Tell OC4J about the MBean, by adding the following to the j2ee/home/
config/application.xml file:
<jmx-mbean
objectname= ":name=SiteMonitor" class="devtrends.jmxping.SiteMonitor">
<description>Pings Web Sites
</description>
</jmx-mbean>
- Restart OC4J.
The JMX implications for Oracle developers are: (1) developers can create applications that monitor and control OC4J; (2) developers can create plug-ins to manage their own applications; and (3) developers can augment OC4J with new, integrated services.
The sample code for this article
is on the DevTrends Web site (see Next Steps). To learn more about
JMX, download the Oracle JMX FAQ demonstration from OTN.
Cameron O'Rourke (cameron.orourke@oracle.com) has been an Oracle technologist since 1992.
|