Integrating Java JMX Managed Beans using Spring and Annotations
Introduction to JMX
JMX stands for Java Management Extensions, and was created to implement a standard and uniform way to manage your applications. By using JMX remote can connection to a JVM and monitor running applications on that JVM. In addition to monitoring, remote clients are able to directly run methods and modify runtime parameters of the application that is being managed. For this reason, it is being used in enterprise applications to allow production support staff to better monitor and management deployed applications.
When JMX was introduced to the masses they also introduced a basic console called jConsole. Using this Java Monitoring and Management Console you are able to monitor Heap memory usage, threads, classes loaded and CPU usage.
Connecting to the MBean Server
As defined in the JMX Specification, remote applications obtain MBeans through an MBean server, accessed by means of a JMX connector. We will use RMI (Remote Method Invocation) as a communication protocol between the client and the server. Using jConsole we will access the MBean Server connection and gain access to the bean’s attributes, operations and notifications.
For our example, we will use:
service:jmx:rmi://localhost/jndi/rmi://localhost:1099/issuer

In addition and more importantly for us, using jConsole we are able to monitor and manage our MBeans via the MBeans tab.

In order for us to use JMX we need to create resources called Managed Beans or MBeans.
This article will show you how to create a managed bean (MBean) using Annotations and Spring. We will use the included enhanced Tomcat Server (VMware vFabric tc Server Developer Edition). We can convert any bean to a managed bean by using the @ManagedXXX annotations. For this example, I took my Issuer class which was used as a model/entity in previous examples a converted it to an MBean. You will notice that by using @ManagedResource we will mark the class as an MBean. We will also use the @ManagedAttribute and @ManagedOperation to expose attributes or methods of a class.
Issuer Managed Bean (MBean)
package com.avaldes.model; import org.springframework.jmx.export.annotation.ManagedAttribute; import org.springframework.jmx.export.annotation.ManagedOperation; import org.springframework.jmx.export.annotation.ManagedResource; @ManagedResource public class Issuer { private String ticker; private String issuerName; private String issuerType; private String country; public Issuer() { } public Issuer(String ticker, String issuerName, String issuerType, String country) { setTicker(ticker); setIssuerName(issuerName); setIssuerType(issuerType); setCountry(country); } @ManagedAttribute public String getTicker() { return ticker; } @ManagedAttribute public void setTicker(String ticker) { this.ticker = ticker; } @ManagedAttribute public String getIssuerName() { return issuerName; } @ManagedAttribute public void setIssuerName(String issuerName) { this.issuerName = issuerName; } @ManagedAttribute public String getIssuerType() { return issuerType; } @ManagedAttribute public void setIssuerType(String issuerType) { this.issuerType = issuerType; } @ManagedAttribute public String getCountry() { return country; } @ManagedAttribute public void setCountry(String country) { this.country = country; } @ManagedOperation public void getIssuerValues() { System.out.println("ISSUER CONTENTS: " + toString()); } @ManagedOperation public String toString() { return "[" + getTicker() + ", " + getIssuerName() + ", " + getIssuerType() + ", " + getCountry() + "]"; } }
spring-config.xml
In the spring-config file you will notice I created a bean entry called issuerBean . In this example, I am setting the ticker to a value of “AVAL” using Spring’s IoC (Inversion of Control). The configuration of the bean is done in the config file and the setting of the ticker is done using setter injection.
We need to ensure we have the AnnotationMBeanExported otherwise our bean will not be exported and be available to jConsole. The last thing that we do in the configuration is create the rmiRegistry bean and use reference injection to ensure it is made available to the ConnectorServerFactoryBean.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="issuerBean" class="com.avaldes.model.Issuer"> <property name="ticker" value="AVAL"/> </bean> <bean id="mbeanExporter" class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter"/> <bean id="rmiRegistry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean"/> <bean id="connectorServerBean" class="org.springframework.jmx.support.ConnectorServerFactoryBean" depends-on="rmiRegistry"> <property name="serviceUrl" value="service:jmx:rmi://localhost/jndi/rmi://localhost:1099/issuer"/> </bean> </beans>
SimpleSpringJMXExample
package com.avaldes.tutorial; import java.io.IOException; import java.util.Scanner; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.avaldes.model.Issuer; public class SimpleSpringJMXExample { public static void main(String[] args) throws IOException { Issuer issuer; BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring-config.xml"); issuer = (Issuer) beanFactory.getBean("issuerBean"); System.out.println("TICKER: " + issuer.getTicker()); System.out.println("Open JConsole now and connect to==> service:jmx:rmi://localhost/jndi/rmi://localhost:1099/issuer"); System.out.println("Press ENTER to continue..."); Scanner keyScan = new Scanner(System.in); while(!keyScan.nextLine().equals("")); System.out.println("Issuer Contents ===> " + issuer.toString()); } }
Testing Out JMX using jConsole
Setting and Modifying Attributes using jConsole
Using jConsole we are able to modify any of the attributes that have the @ManagedAttribute annotation. The following screen shot shows how we can manage attributes of an MBean.

Executing Methods using jConsole
We can also use jConsole to execute methods at runtime. In many cases, this allows development and support staff to modify or execute methods in a production system without having to stop the execution of the application.

Console Output
As we execute certain methods you will notice the console output, see below:

Benefits of Using JMX
- By using JMX we enable our Java applications to be managed without heavy investment. We need only make small modifications to the class(es) by adding some annotations and register the MBean in order to benefit
- JMX allows our application to use a standard API for management purposes
- JMX allows for integration with existing management solutions as the JMX APIs are open that outside vendors have used
- JMX leverages existing Java technologies
Download the Code
That’s It!
I hope you enjoyed this tutorial. It was certainly a lot of fun putting it together and testing it out. Please continue to share the love and like us so that we can continue bringing you quality tutorials. Happy Coding!!!
Related Posts
- Introduction to Spring Framework, IoC and Injection
In this tutorial we will cover a basic introduction the Spring framework, Inversion of Control and Dependency Injection. We will discuss the benefits of using it, and why it is so popular in today's environment. - Spring Framework Constructor Injection Example
In this tutorial, we will concentrate on Constructor Injection. As the name suggests, constructor injection is form of dependency injection that is accomplished when we supply the arguments to the constructor so that when the bean is instantiated by the factory it contains all of the necessary information to be property constructed. - Spring Framework Constructor Injection with Collections Example
In this tutorial, we will modify our previous example and add a few collections so we can illustrate how we use constructor injection with differing collection types. Specifically, we will show how to perform constructor injection with List, Map and Set objects. - Spring Framework Setter Injection with Examples In this tutorial we will discuss Spring Framework Setter Injection which is the main method of dependency injection in Spring. The property element is used to define the setter injection in Spring by using the name of the property element and looking for the corresponding setXXXXXX methods in the specified bean.
- Spring Bean Scopes Example
In this tutorial we will begin covering scope of Spring beans. In Spring when you define a bean in the Spring configuration file, you are telling Spring about its scope whether you define it or not. This is a powerful and flexible approach because you can choose the scope of the objects you create via configuration instead of having to hardcode the scope of an object at the Java class level. - Spring Bean Life Cycle Example – @PostConstruct, @PreDestroy, InitializingBean, DisposableBean, init-method and destroy-method
In this tutorial we will cover the Spring Bean Life Cycle and the methods, attributes and annotations used to hook into the management of the bean life cycle in the IoC container. - Integrating Java JMX Managed Beans using Spring and Annotations
This post will show how to create managed beans using the JMX standard using Spring. JMX stands for Java Management Extensions, and was created to implement a standard and uniform way to manage your applications. By using JMX remote can connection to a JVM and monitor running applications on that JVM. In addition to monitoring, remote clients are able to directly run methods and modify runtime parameters of the application that is being managed. - Integrating Spring Message Driven POJO with ActiveMQ
This post will show how to create a message driven POJO that can send and receive JMS message using ActiveMQ as the Message Oriented Middleware (MOM). We will discuss various approaches to creating the MDPs (Message Driven POJOs) in this tutorial and discuss setting up ActiveMQ from Apache.
Please Share Us on Social Media






Leave a Reply