JMX with Spring Framework

Introduction

This article explores how to use JMX with Spring Framework, and will show how managed beans (MBeans) can be easily resgistered with the JMX Agent using the annotations provided by the Spring Framework.

For an introduction to the JMX architecture and how to register MBeans using the standard JMX API, check our Brief Instroduction to JMX tutorial.

Registering MBeans with Spring Framework

The below Spring Framework annotations are used to define MBeans and register them with the JMX Agent:

  • @ManagedResource – defines an MBean
  • @ManagedAttribute  – defines an attribute of an MBean
  • @ManagedOperation – defines an operation of an MBean
  • @EnableMBeanExport – registers with the JMX Agent all components annotated with the   @ManagedResource

The code snipped below contains an example Java component decorated with the   @ManagedResource  annotation.

Finally, once we declare the  @EnableMBeanExport  annotation in the Spring configuration, it will register all managed resources in the Spring context with the local JMX Agent.

After the Spring context has loaded and the  @EnableMBeanExport  has worked its magic, all beans decorated with the    @ManagedResource annotation will be visible to the JConsole and other JMX clients.

JMX MBeans with Spring Framework
JMX MBeans with Spring Framework

It should be noted the Spring Framework is able to correctly register operation parameter names out of the box.  This is something that the standard JMX API is not able to do easily, and this is why developers sometimes resort to customised annotations.

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

Additional JMX Metadata with Spring Framework

Spring does a good job at extracting metadata from the class, so usually it is sufficient to decorate the Spring beans with the   @ManagedResource ,  @ManagedAttribute and   @ManagedOperation annotations.

Sometimes, however there is a need to further customise this metadata.

Each of the above annotations accept a description attribute.  Additionally, we can decorate operations with the @ManagedOperationParameters and @ManagedOperationPrameter  annotations to describe its parameters.

The code below illustrates an example Spring component with extra metadata in the managed annotations.

Bibliography

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)

Leave a Reply