
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: May 11, 2024
The Java Management Extensions (JMX) framework was introduced in Java 1.5 and has found widespread acceptance in the Java developers community since its inception.
It provides an easily configurable, scalable, reliable and more or less friendly infrastructure for managing Java application either locally or remotely. The framework introduces the concept of MBeans for real-time management of applications.
This article is a beginner’s step by step guide to create and setup a basic MBean and manage it through JConsole.
JMX architecture follows a three-layered approach:
While creating MBeans, there is a particular design pattern which we must conform to. The model MBean class MUST implement an interface with the following name: “model class name” plus MBean.
So let’s define our MBean interface and the class implementing it:
public interface GameMBean {
public void playFootball(String clubName);
public String getPlayerName();
public void setPlayerName(String playerName);
}
public class Game implements GameMBean {
private String playerName;
@Override
public void playFootball(String clubName) {
System.out.println(
this.playerName + " playing football for " + clubName);
}
@Override
public String getPlayerName() {
System.out.println("Return playerName " + this.playerName);
return playerName;
}
@Override
public void setPlayerName(String playerName) {
System.out.println("Set playerName to value " + playerName);
this.playerName = playerName;
}
}
The Game class overrides a method playFootball() of the parent interface. Apart from this, the class has a member variable playerName and getter/setter for it.
Note that getter/setter are also declared in the parent interface.
JMX agents are the entities running either locally or remotely which provide the management access to the MBeans registered with them.
Let’s use PlatformMbeanServer – the core component of JMX agent and register the Game MBean with it.
We’ll use another entity – ObjectName – to register the Game class instance with the PlatformMbeanServer; this is a String consisting of two parts:
In this example, we’ll use: “com.baledung.tutorial:type=basic,name=game”.
We’ll get the MBeanServer from the factory class java.lang.management.ManagementFactory.
Then we’ll register the model MBean using the created ObjectName:
try {
ObjectName objectName = new ObjectName("com.baeldung.tutorial:type=basic,name=game");
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
server.registerMBean(new Game(), objectName);
} catch (MalformedObjectNameException | InstanceAlreadyExistsException |
MBeanRegistrationException | NotCompliantMBeanException e) {
// handle exceptions
}
Finally, just to be able to test it – we’ll add a while loop to prevent the application from terminating before we can access the MBean through JConsole:
while (true) {
}
Here’s a quick look at the JConsole part of the process:
The basics of MBean management are simple:
Let’s see what that means for the Game MBean in practice:
The Following log will appear in the Eclipse console:
Set playerName to value Messi
The following log will appear in the eclipse console:
Messi playing football for Barcelona
This tutorial touched upon the basics of setting up a JMX-enabled application by use of MBeans. Also, it discussed about using a typical client-side tool like JConsole to manage the instrumented MBean.
The domain of JMX technology is very wide in scope and reach. This tutorial can be considered a beginner’s step towards that.
Follow the Java Category