A Brief Introduction to JMX

A Brief Introduction to JMX

The Java Management eXtension (JMX) is a technology to manage and monitor Java applications remotely.

In a nutshell, these are the main participants in a JMX architecture:

  • Managed Beans (MBeans). Can be managed and monitored remotely via JMX.
  • JMX Agent. Manages MBeans and provides an interface so that they can be accessed remotely
  • Remote Management Application. Monitors and interacts with the MBeans via the JMX Agent

An MBean needs a management interface to define the attributes and operations that can be managed and monitored.  The management interface contains the following:

  • Attributes as getter/setter
  • Operations
  • Notifications
JMX Overview
JMX Overview

The JMX specification defines five types of MBeans:

  • Standard MBeans. These are the simplest. Their management interface is defined by their method names.
  • Dynamic MBeans. The MBean management interface is defined at runtime.
  • Open MBeans. They are dynamic MBeans that rely on basic data types for universal manageability. This ensure remote clients do not need to have any application specific compiled classes to communicate with the MBean.
  • Model MBeans. They are dynamic MBeans configurable and self-described at runtime. They provide an MBean class with default behaviour for dynamic instrumentation of resources.
  • MXBean. An MXBean is similar to a Standard MBean but makes use of universal data types similar to Open MBeans.

In this guide we will only look into Standard Beans and MXBeans. We will also look how we can leverage the SpringFramework to easily register MBeans.

Standard MBeans

Standard MBeans are the simplest type of MBean. A Standard MBean is defined by a Java interface called <Resource>MBean  and a Java class called <Resource>  that implements that interface.

Every method in the interface defines either an attribute or an operation on the MBean.  Attributes follow the standard JavaBean getter/setter convention. Everything else is considered an operation.

The example below is the management interface of a Standard MBean containing two attributes and three operations:

The class below is the MBean implementation:

And finally, the MBean needs to be registered with a JMX Agent so that it can be managed remotely. In our example, we register it with the platform MBean server, which will make it available in the JConsole.

The ManagementFactory.getPlatformMBeanServer()  retrieves the existing MBean server, or if none exist, one is automatically created.

The ObjectName uniquely identifies the MBean in this JMX agent.  Object names are defined using the pattern below:

The last step is just to start the JConsole and point at our small Java program.  You will see our newly created MBean in the MBeans tab as per below:

A Standard Bean in the JConsole
A Standard Bean in the JConsole

The source code from this section can be accessed here.

MXBeans

An MXBean is another simple type of simple MBean that references only a predefined set of data types.

The benefit of MXBeans over Standard MBeans is that they can be used by any client.  The issue with Standard MBeans is that all classes defined in the interface must be Serializable and available to the client JVM.  That is, if your MBean interface returns a class named Dog, that class must be available to the remote client JVM (needless to say, the JConsole does not know about Dogs).

MXBeans, however, transparently map all classes defined in the interface to a predefined set of types defined in the javax.management.openbean.  These are called Open Types, and the mapping rules highlighted below:

  • Primitive classes and wrappers are mapped to an equivalent SimpleType
  • Arrays and Lists are mapped to an ArrayType
  • Maps are mapped to a TabularType
  • Any other objects are mapped to a CompositeType

As the example below illustrates, MXBeans are defined very similarly to MBeans.  The only difference is that we postfix the interface with MXBean instead of MBean.

The below is the MXBean implementation:

Finally, this is the ResourceItem class defined in the ResourceMXBean interface:

The JMX Agent will transparently convert ResourceItem into a CompositeType, so that contents of this class will be readily available to the remote client.

The full source code from this section can be accessed here.

MBeans with Spring Framework

Spring Framework provides a handful of annotations that simplify registering MBeans with the local JMX Agent.

If you want to turn a component into a managed resource, you just need to decorate it with the @ManagedResource  annotation.  Similarly, you can decorate with the  @ManagedAttribute  and @ManagedOperation  any attributes and operations that you want to publish via JMX.

Finally, to ensure MBeans are exported to the JMX Agent, you need to declare the @EnableMBeanExport  annotation.

For a more detailed tutorial how to register your Spring beans with JMX, check out the JMX with Spring Framework tutorial.

The full source code from this section can be accessed here.

Additional Reading on JMX

Show MBean Parameter Names in JConsole

You might have noticed the JConsole does a reasonable job when showing MBeans.  The problem is that it does not show meaningful parameter names when using the standard JMX API.  For example, notice that the addItem parameter is p0 instead of the more meaningful item name used in the code.

Missing Param Names in JConsole
Missing Param Names in JConsole

It is worth saying that this is not an issue when using JMX with Spring Framework, as the Spring annotations automatically take care of parameter names.

Having said that, if you cannot use Spring with your project, check out this tutorial to find out how to create custom annotations to simplify providing JMX metadata.

JMX Notifications

JMX Notifications allow MBeans to fire notifications to change of state.  Any component with access to the JMX Agent, locally or remotely, can then hook up and listen to those notifications.

They are an advanced concept, and we cover them in the JMX Notifications tutorial.

Bibliography

Source Code

The following two tabs change content below.

Eduard Manas

Eduard is a senior IT consultant with over 15 years in the financial sector. He is an experienced developer in Java, C#, Python, Wordpress, Tibco EMS/RV, Oracle, Sybase and MySQL.Outside of work, he likes spending time with family, friends, and watching football.

Latest posts by Eduard Manas (see all)

One thought on “A Brief Introduction to JMX”

Leave a Reply