JConsole MBean Example

MBeans are regular Java Objects that follow JMX Specification design pattern to become managed Java Object. Using MBeans you can expose a set of operations as well readable/writable attributes.

Operations vs Attributes

An operation can take zero or more parameters to perform a task. For instance, in MBean java.lang.MemoryMXBean the operation gc() will issue a request to JVM to perform Garbage collection.

Attributes are getters and setters that can be used to retrieve and update the values of variables. To declare a getter and setter we just have to add a get and set before the variable name. For instance, in java.lang.ThreadMXBean the attribute ThreadCount returns the count of live threads in the Java Application.

Steps to Register MBeans

There are three steps to register an MBean,

  1. Declare an MBean Interface with the desired operation and attributes.
  2. Implment the Interface.
  3. Register the implementation with the MBean server.

Let’s consider the MBean example hosted on Github and perform some operations on it using JConsole. We will create a simple Application using MBeans that maintains a to-do list.

MBean Interface Example

The MBean interface name should have the postfix “MBean”. The operation exposed by MBean can follow any naming pattern like doX() or performX() etc. However your attributes should start with get or set before the variable name.

Let’s see the MBean Interface example below,

package com.cleantutorials.jconsole.mbeans;
public interface ToDoMBean {
public void storeTask(String taskName);
public String dequeueTask();
public int getTasksLeft();
}
view raw ToDoMBean.java hosted with ❤ by GitHub

The name of the interface is “ToDo” plus the postfix “MBean”. The interface has two operations – storeTask() and dequeueTask() and one attribute – getTasksLeft().

MBean Implementation Example

The MBean implementation class name will be the Interface name minus the postfix “MBean”. Since our interface name is ToDoMBean, our implementation class name will be ToDo. If you don’t follow the convention, you will get javax.management.NotCompliantMBeanException when you register the MBean.

package com.cleantutorials.jconsole.mbeans;
import java.util.LinkedList;
import java.util.Queue;
public class ToDo implements ToDoMBean {
Queue<String> taskQueue = new LinkedList<String>();
@Override
public void storeTask(String taskName) {
taskQueue.add(taskName);
}
@Override
public String dequeueTask() {
return taskQueue.remove();
}
@Override
public int getTasksLeft() {
return taskQueue.size();
}
}
view raw ToDo.java hosted with ❤ by GitHub

The implementation is fairly simple, with just enqueue, dequeue operations on our to-do queue.

Register MBean Server

Finally we will register the MBean with the MBean Server.

package com.cleantutorials.jconsole.mbeans;
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.ObjectName;
public class Main {
public static void main(String[] args) throws Exception {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("com.cleantutorials.jconsole.mbeans:type=ToDo");
ToDo toDo = new ToDo();
server.registerMBean(toDo, name);
Thread.sleep(Long.MAX_VALUE);
}
}

The ObjectName is created with “package:type=Class” where the package is the name of the package where your MBean resides and the Class is the name of the MBean Implementation class.

To finally register the MBean with the server we pass the ObjetName and the MBean object to the registerMBean() function.

JConsole MBeans Operations

We will now connect JConsole to Java Application and perform operations on the MBean example discussed above.

MBean get attribute

Initially, the TaskLeft attribute has a value of 0 because we haven’t added any tasks to our to-do list.

MBean Attribute Chart without performing any operation

By double-clicking on the Value field’s value, it will transform to a chart so that you can monitor the change in attribute value over time similar to the components like Memory Chart.

MBean perform operation store task

On clicking on storeTask operation twice, with the tasks “Finish JConsole Tutorial” and “Finish HTML Tutorial” we get the expected response -“Method successfully invoked”. Now we should have 2 items in our todo queue.

MBean perform operation dequeue task

After performing a dequeueTask operation on the MBean, we get the first item that we inserted in the to-do list in a new dialog box.

MBean Attribute Chart after performing operations

Finally, if we switch back to the TaskLeft attribute chart, we can see how the y-axis which represents the number of tasks changes over time.

MBean as Metric

MBean can also be used to measure metrics in your Java application. Some of the common metrics that you can measure are given below.
  • Average time taken by various operations.
  • Number of elements present in Queues.
  • Number of times a method has been invoked.
  • Number of times expected call returned an error.
  • Value of flags in your application.

Implementing any metric will be quite similar to the MBean Example that we discussed above.

CommentsLoad Comments