Configure MBeans in Spring boot using Java config and Jolokia

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). Before we start, I highly recommend that you also check our Jolokia and Spring boot tutorial before you proceed as it will make understanding this post much easier.

What is an MBean

Managed beans (MBeans) are Java beans that allow the management of running applications, change of parameters or triggering of certain procedures, without needing to restart or redeploy the application.

This makes MBeans very useful to configure and make changes to production environments on the fly, without the costly inconvenience of having downtime.

Implementing an 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.

package com.nullbeans.java.mathematics;

public interface BasicMathsMbean {

    Float add(Float a, Float b);

    Float subtract(Float a, Float b);

}

And below is the implementation class:

package com.nullbeans.java.mathematics;

import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;

@ManagedResource
public class BasicMaths implements BasicMathsMbean {

    @ManagedOperation
    @Override
    public Float add(Float a, Float b) {
        return a+b;
    }

    @ManagedOperation
    @Override
    public Float subtract(Float a, Float b) {
        return a-b;
    }

}

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.

@Bean
public BasicMathsMbean basicMathsMbean(){
    return new BasicMaths();
}


Testing the bean using Jolokia

There are multiple ways to test 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 which contains your newly created MBean, then it means that the bean is configured correctly 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.

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.

......},"com.nullbeans.java.mathematics":{"name=basicMathsMbean,type=BasicMaths":
{"op":{"add":{"args":[{"name":"a","type":"java.lang.Float","desc":"a"},
{"name":"b","type":"java.lang.Float","desc":"b"}],
"ret":"java.lang.Float","desc":"add"},"subtract":{"args":[{"name":"a","type":
"java.lang.Float","desc":"a"},{"name":"b","type":"java.lang.Float","desc":"b"}],
"ret":"java.lang.Float","desc":"subtract"}},"class":
"com.nullbeans.java.mathematics.BasicMaths","desc":""}},"java.nio":...........

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:

<servername>:<port>/<baseURL>/,<jolokia>/exec/<mbeanPackageName>:name=<beanId>,type=<beanImplementation>/<operationName>/<firstParam>/<secondParam>/<thirdParam.....

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.

{"request":{"mbean":"com.nullbeans.java.mathematics:name=basicMathsMbean,
type=BasicMaths","arguments":["3","5"],"type":"exec","operation":"add"},
"value":8.0,"timestamp":1545929380,"status":200}

Summary

Jolokia is a very powerful framework which allows you to perform managed operations quite easily with little configuration, specially on Spring Boot. Having Jolokia configured allows you to extend already existing management functionalities by creating your own MBeans and exposing them through Jolokia’s REST interface.

This makes Jolokia a worthy library for learning and implementing in your project.

Configure MBeans in Spring boot using Java config and Jolokia
Scroll to top