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.
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.
There are three steps to register an MBean,
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.
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(); | |
} |
The name of the interface is “ToDo” plus the postfix “MBean”. The interface has two operations – storeTask() and dequeueTask() and one attribute – getTasksLeft().
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(); | |
} | |
} |
The implementation is fairly simple, with just enqueue, dequeue operations on our to-do queue.
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.
We will now connect JConsole to Java Application and perform operations on the MBean example discussed above.
Initially, the TaskLeft attribute has a value of 0 because we haven’t added any tasks to our to-do list.
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.
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.
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.
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.
Implementing any metric will be quite similar to the MBean Example that we discussed above.