February 22, 2019
Hot Topics:

Standard MBeans

  • May 23, 2003
  • By O'Reilly & Associates
  • Send Email »
  • More Articles »

Registering the MBean with the MBean server

Without an ObjectName instance, an MBean cannot be registered with the MBean server. In fact, the ObjectName is critical to doing anything meaningful with the MBean server. In the previous section, we saw how to create an ObjectName instance using one of the three constructors provided by ObjectName. In this section, we will see how to use that ObjectName to register an MBean.

The first step in using the MBean server is to obtain a reference to it. Every compliant JMX implementation must provide an MBeanServerFactory class that contains several methods that allow you to gain access to the MBean server (these will be discussed in more detail in Chapter 6). The easiest method to use is createMBeanServer(), which takes no arguments and returns a reference to a newly created MBean server:

//. . .
MBeanServer server = MBeanServerFactory.createMBeanServer();
//now do something with the MBean server
//. . .

Now that we have a reference to the MBean server, we can register our MBean. The following example shows how to create an ObjectName, obtain a reference to the MBean server, and register the Controller MBean:

//. . .
try {
  MBeanServer server = MBeanServerFactory.createMBeanServer();
  ObjectName objName = new
  Controller controller = new Controller();
  server.registerMBean(controller, objName);
}catch (MalformedObjectNameException e) {
//. . .
//. . .

There are several ways to register an MBean. In the previous example, the MBean object was created explicitly using the new keyword, and then a reference to that object was passed to the registerMBean() method of the MBean server. However, those two steps could have been combined into one, allowing the MBean server to create the MBean object:

//. . .
try {
  MBeanServer server = MBeanServerFactory.createMBeanServer();
  ObjectName objName = new
  server.createMBean("sample.standard.Controller", objName);
}catch (MalformedObjectNameException e) {
//. . .
//. . .

The MBean server also provides an overloaded version of createMBean() that allows you to specify constructor parameters for your MBean. The various ways to create and register MBeans will be covered in more detail in Chapter 6.

Once the MBean is registered with the MBean server, it is available for management. The mechanisms used by a management application to plug into and manage an MBean server are part of the distributed services level of the JMX architecture and are not fully specified in the JMX 1.0 RI. However, provided with the RI is a class called HTMLAdaptorServer, which is used throughout this book and is described in the next section.

The HTMLAdaptorServer Class

The HTMLAdaptorServer class is located in the com.sun.jdmk.comm package, which is distributed as part of the RI in jmxtools.jar. This handy class allows us to manage an MBean server through a web browser. HTMLAdaptorServer ("Adaptor" for short) is itself an MBean, and as such it must have an ObjectName and be registered with the MBean server. This class is essentially an HTTP server that listens on a specified port and generates HTML forms that are sent to the web browser. It is through these forms that you can manage and monitor your MBeans.

To use the HTMLAdaptorServer class, you must create an ObjectName for it and register it with the MBean server,as you would any other MBean:

//. . .
  MBeanServer server = MBeanServerFactory.createMBeanServer();
  int portNumber = 8090;
  HtmlAdaptorServer html = new HtmlAdaptorServer(portNumber);
  ObjectName html_name = null;
  try {
    html_name = new ObjectName("Adaptor:name=html, port=" +
    server.registerMBean(html, html_name);
  } catch (Exception e) {
    System.out.println("Error creating the HTML adaptor...");
//. . .

In this example, the Adaptor will be listening for HTTP requests on port 8090 of the machine on which it is running. A new instance of the HTMLAdaptorServer class is created, passing the specified port number to its constructor. Then an ObjectName is created for the Adaptor, and it is registered with the MBean server. Finally, the Adaptor is started. HTMLAdaptorServer implements the Runnable interface (actually, its parent class, CommunitorServer, does), so it runs on its own thread. Once the thread is started (by calling the start() method), the Adaptor is running and awaiting HTTP requests.

Now that the Adaptor is running, all you need to do is point your browser to the machine that contains the JVM in which the MBean server is running. Assuming that the browser and the MBean server are running on the same machine, simply point your browser to http://localhost:8090. Figure 2-8 shows a screen shot of the form that will be displayed.

Figure 2-8. The Adaptor in action.

The important elements are:

"Filter by object name:"
This text box contains a pattern for which MBeans to show under "List of registered MBeans by domain:". The pattern starts with the domain name, followed by a colon, followed by the key property list of the MBeans to show. By default, this is "*:*", which means to show all domains and all MBeans.
"List of registered MBeans by domain:"
This is a bulleted list of domains that match the filter (see above) and the MBeans within that domain that also match the pattern.

Notice that there are two domains, Adaptor and JMIplementation. In Figure 2-8, we see that when the ObjectName was created for the Adaptor MBean, "Adaptor" was provided as the domain name. The key property list consists of "name=html,port=8090".

If you click on this MBean (the key property list contains a link), you can view the attributes and operations exposed on the Adaptor MBean. The attributes are shown in Figure 2-9, and the operations are shown in Figure 2-10.

Figure 2-9. Attributes exposed for management on the HTMLAdaptorServer;scroll down to view the operations

Figure 2-10. Operations exposed for management on the HTMLAdaptorServer

We will use the HTMLAdaptorServer class throughout this book for managing all of the MBeans in the application.

Downloading and Installing the JMX Reference Implementation

Before you can build and run the application, you must first obtain the JMX RI. The easiest way to do this is to download it from Sun Microsystems at http://java.sun.com/products/JavaManagement/. Select either the source code or binary RI under "JMX Deliverables" and follow the instructions.

Once you've downloaded the RI, you should unzip the downloaded file directly into your c:\ drive for Windows or your home directory for Unix.

About the Author

J. Steven Perry

Steve Perry has been a software developer for over 10 years. During that time, he's been a maintenance programmer, a system analyst, and is now an architect. This has given him the opportunity to see first hand how critical the need is for application management, and the dire consequences that can result when it's absent. He currently works in the Emerging Systems Architecture group at ALLTEL Information Services, in Little Rock, Arkansas.

Source of this material

This is Chapter 2: Standard MBeans from the book Java Management Extensions (ISBN: 0-596-00245-9) written by J. Steven Perry, published by O'Reilly & Associates.

To access the full Table of Contents for the book

Page 7 of 7

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date