Managing applications with JMX

Lee Chuk-Munn discusses JMX, which is useful in revealing specific data pertaining to your applications.

In our previous article, we used jconsole to look into a running Java application.

The jconsole tool is only available on JavaSE 5 and later releases. Using jconsole, we can find out information like the effective classpath that the current running application is running, the rate of GC (garbage collection), the amount of memory allocated to various objects, and the threads that are active.

However, the information provided is at a system level viz memory, threads, GC, platform, etc. What about specific data pertaining to our application, such as statistics like the number of transactions since this morning and which account has been accessed the most? We can leverage JMX to expose these statistics to jconsole.

An MBean is a Java object that integrates into the JMX management framework. An MBean represents our application in this framework and through it, an application exposes pertinent information. An MBean has:

  1. Properties or attributes
  2. Operations 
  3. Events

In this article, we will look at developing an MBean with properties and operations. In the next article in this series, we will look at event notifications.

In this example we will 'patch' Notepad to provide the following information: (Notepad is a JavaSE demo. You can find the original Notepad under $JAVA_HOME/demo/jfc/Notepad directory.)

  • The number of words and characters in the current document (properties)
  • Count the number of times a certain phrase or word have occurred (properties)
  • Convert the whole document to either upper case or lower case (operation)
  • Clear the entire document (operation)

MBean Interface
To develop an MBean, we first need to develop an interface. Here are a few things you need to keep in mind:

  1. The interface name must end with MBean suffix.
  2. MBean properties follow the JavaBean naming convention viz. properties must have get, and set. For example if you want the MBean to have a read/write property call 'counter' of int type, then you need to have two methods calls: public int getCounter() and public void setCounter(int c). To denote a readonly or writeonly property, only provide the get and set respectively
  3. All other methods in the interface that do not conform to point 2 above will be regarded as an operation.

The following is an example of our Notepad MBean:

public interface NotepadManagementMBean {
    public int getCharacterCount();
    public int getWordCount();
    public int getPhraseCount();
    public void setPhrase(String s);
    public String getPhrase();
    public void clearText();
    public void toUpperCase();
    public void toLowerCase();

MBean Class
Next we implement the NotepadManagementMBean interface. Here is our implementation:

public class NotepadManagement implements NotepadManagementMBean {
    public NotepadManagement(Notepad n) {
        notepad = n;
        editor = n.getEditor();
    public int getCharacterCount() {
        return (editor.getText().length());

The class takes an instance of Notepad in its constructor. The Notepad class gives access to the contents of the document. In the above code snippet, we also see an implementation of getCharacterCount() which returns the number of characters in the document. You can see the entire NotepadManagement source here.

Once we have developed our MBean, we need to register our MBean with the JMX framework. We also need to give a name to our MBean. The name of our NotepadManagement will be 'Notepad:name=notepad'. The name consists of 2 parts; everything before the colon (:) is known as the domain and everything after it is the key. The key is a name value pair. In our case, the name of the key is 'name' and its corresponding property is 'notepad'. You can read more about MBean names here.

Here is the code for registering our MBean. The code is in the main() method Notepad.

//Get the platform MBeansServer
MBeanServer mbServer = ManagementFactory.getPlatformMBeanServer();
//Create an instance of our MBean
NotepadManagementMBean mbean = new NotepadManagement(notepad);
//Create our MBean name
ObjectName mbeanName = new ObjectName( "Notepad:name=notepad");
//Register our MBean with its name
mbServer.registerMBean(mbean, mbeanName);

Now run the Notepad and start jconsole. If you are using JDK 5 start Notepad in the following manner:
    java Notepad
If you are using JDK 6, then start Notepad like so
    java Notepad
You can find out more about starting Java application under JMX and jconsole from my previous article. Here is a screen shot of the jconsole; you can see the properties and operations under the Notepad node.

(Click to enlarge)

You can get the source to this article here. In our next article we will look at how to send notifications.

Lee Chuk-Munn has been programming in the Java language since 1996, when he first joined Sun Microsystems in Hong Kong. He currently works as a senior developer consultant and technology evangelist for Technology Outreach at Sun in Singapore. Chuk's focus is in Java APIs, Java EE, Java SE, and Java ME. Chuk graduated in 1987 from the Royal Melbourne Institute of Technology in Melbourne, Australia, where his favorite subject was compiler theory.