Summary:

In this article, we will create a new custom MBean. We will show how to configure this bean and then we will test it using Jolokia (JMX over HTTP).

 

Prerequisites:

If you have not yet configured Jolokia, then we suggest that you go over this article https://nullbeans.com/how-to-configure-jolokia-on-a-spring-boot-server/ before you proceed.

 

Introduction:

Managed beans (MBeans) allow the management of applications running applications, change of parameters or triggering of certain procedures, without needing to restart or redeploy the application. This makes MBeans very useful for production environments, where downtime can be either inconvenient or very costly.

 

Implementing the MBean:

 

In this example, we will write a very simple bean for learning purposes. The bean can perform two operations. Addition and subtraction. Like regular Spring beans, the bean implementation implements an interface. By default, this will be the bean’s name.

 

And below is the implementation class:

 

 

To mark a bean as an MBean in Spring boot, all that is needed is to annotate the class with the @ManagedResource annotation. This will indicate to the JmxAutoConfiguration class that this is a managed bean. In order to expose specific methods to JMX interfaces, the methods should be annotated with the @ManagedOperation annotation.

 

Exposing the bean to JMX interfaces:

 

By default, Spring boot will create an MBeanServer and an MBeanExporter automatically. If you have one application context per running Java application, then this should be sufficient. (If you are running multiple contexts, then additional configuration might be needed. We will explore this issue in a later post.)

 

In order to expose the Mbean, all that you need to do is to configure it as a normal bean in your Java config and you are done.

 

 

Testing the bean using Jolokia:

There are multiple ways of testing an MBean configuration. If you are using Jolokia, then you are in luck. You can simply call the list function of Jolokia. If you receive a reply with containing your new MBean, then the bean is configured and ready for use. Of course, we would need to make sure that the bean is accessible via the correct JMX domain and the bean and its operations have the correct names (in case we override the default settings. More on that in a later post). Let us start up our Spring boot server, and try out the list functionality by going to the following URL in the browser: http://localhost:8080/actuator/jolokia/list (or your corresponding URL if not using the default settings). This will provide the list of available MBeans and their corresponding operations and parameters.

 

 

If you find the MBean in the response list, then the MBean has been configured properly. We can also directly execute one of the MBean functions via Jolokia. To execute an MBean function via Jolokia, we need to use the exec function. The URL has the following format:

 

For example, we can excute the “add” functionality using the following URL:

http://localhost:8080/actuator/jolokia/exec/com.nullbeans.java.mathematics:name=basicMathsMbean,type=BasicMaths/add/3/5

Executing the “add” operation will yield the following response from Jolokia, with the “value” parameter containing the result of the operation.