How JMX works? Monitoring your Java Application remotely.

Java Management Extension (JMX) was introduced in Java Platform 2 Standard Edition (J2SE), Version 5.0. JMX provides an efficient and easy way to monitor and manage remote Java applications. You can monitor a pre-defined set of resources using JMX. For instance, you can monitor the number of threads in your java application or the current state of each thread. It also allows you to perform a set of operations on these managed resources. For instance, you can trigger the Garbage Collection request using a management application. You can also create and register custom resources using MBeans that can be monitored and managed remotely.

JMX Agent, Remote management Application, and MBean.

There are three main components of the JMX architecture. The JMX agent, MBeans, and the Remote management system. The JMX agent runs on the Java Virtual Machine (JVM). It is the Java process that you want to monitor and manage. The remote management application allows you to connect to the JMX Agent from a remote location and monitor the Java Process. The MBeans are the resources that the management application can monitor using the JMX agent.

 

JMX Agent and multiple Remote management applications

Multiple Remote management applications running on different systems can connect to a single JMX Agent. Multiple instances of management application can also run on the same system on which the JMX Agent is running and establish a connection to it but in a different JVM. JConsole is an example of a remote management application. Using JConsole you can monitor the thread, memory, CPU utilization and other important resources.

JMX Architecture

The JMX architecture consists of the following 4 parts.

  • MBeans.
  • MBean Server.
  • Connector and Protocol Adapter.
  • Management and Monitoring Application.

The MBean Server and the Connector/Adapter are the part of the JMX Agent. The management application is a separate entity.

JMX Architecture (MBean, MBean server, Connector , Protocol Adapter, Remote Management Application)

MBeans

MBeans are a set of different resources. In the terms of Java Programming language, they are Java Objects that have to implement an interface that defines the set of operations that can be performed on the particular MBean. The list of allowed operation on an MBean is given below.

  • Read/Write the value of an attribute.
  • Invoke an operation.

By convention, the MBean interface is written with MBean suffixed to the implementation’s name. For instance, if the MBean implementation class name is JMXTutorial, then the interface name should be JMXTutorial + MBean i.e “JMXTutorialMBean“. An example of MBean interface and its implementation is given below.

JMXTutorialMBean.java (The interface)

package com.cleantutorials.jconsole.jmx;
public interface JMXTutorialMBean {
public int getNoOfStudents(); // Reading the value of the attribute (Getter method)
public void setNoOfStudents(int x); // Writing the value of the attribute (Setter Method)
public void incrementNoOfStudents(); // Any operation
}

JMXTutorial.java (The Implementation)

package com.cleantutorials.jconsole.jmx;
public class JMXTutorial implements JMXTutorialMBean {
private int noOfStudents;
public int getNoOfStudents() {
return noOfStudents;
}
public void setNoOfStudents(int noOfStudents) {
this.noOfStudents = noOfStudents;
}
public void incrementNoOfStudents() {
noOfStudents++;
}
}

The above examples show a custom or a user-defined MBean. The MBean class provides the following list of operations on the attribute noOfStudents.

  • A get/read operation on the attribute, noOfStudents.
  • A set/write operation on the attribute, noOfStudents.
  • An increment operation on the attribute, noOfStudents.

The above MBean is an example of a custom object that needs to be managed remotely. But the Java platform provides a set of pre-defined resources (MBeans) that can be used to monitor the JVM. For example, the Java platform provides the implementation of the interface ThreadMBean. The implementation is used to monitor and manage threading in the JVM. The various attributes and operations that can be performed on this MBean are given below.

MBeans

MBean Server

MBean server is the core component of the JMX agent. The server provides services for monitoring and manipulating MBeans. The Remote management application will only be able to monitor and manage resources that are registered with the MBean Server. The following example shows how to register a custom MBean to the MBean server.

package com.cleantutorials.jconsole.jmx;
import java.lang.management.ManagementFactory;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
public class RegisterMyMBean {
public static void main(String[] args) throws MalformedObjectNameException,
InstanceAlreadyExistsException, MBeanRegistrationException,
NotCompliantMBeanException {
MBeanServer server = ManagementFactory.getPlatformMBeanServer(); // Get the MBean Server instance.
ObjectName name = new ObjectName("JMXTutorialMBean:type=JMXTutorial"); // Create a unique Object Name.
JMXTutorial tutorial = new JMXTutorial();
server.registerMBean(tutorial, name); // Register the MBean with the MBean Server.
try {
Thread.sleep(360000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

Registration of MBean with the MBean server requires the following steps.

  • Get the instance of the MBeanServer from the static method “ManagementFactory.getPlatformMBeanServer()”.
  • Create a unique Object Name.
    •  In the Remote management application, the MBean will be visible with the name “JMXTutorial“, which is the type value of the Object name.
  • Register the MBean instance with the MBean Server: “server.registerMBean(tutorial, name);”

After dynamically registering your MBean with the server, it will be visible to the Remote management application.

Connector and Protocol Adapter.

Connector and adapter enable the communication between the MBean Server and the Remote management application. A JMX Agent must have at least one connector to facilitate this intercommunication. The Java platform provides a standard RMI(Remote Method Invocation) connector. JConsole (Management application) uses the RMI connector to communicate with the MBean server. A connector/adapter can allow multiple connections from different remote monitoring applications at a particular JMX port.

Protocol adapter performs the same function as connectors, the only difference between connector and adapter is in the way in which the communication takes place. Protocol adapters are specific to a particular protocol. For instance, if you want to attach a web browser/console with a JMX agent, you will require an adapter that supports HTTP protocol. Whereas, connectors are compliant with the JMX specifications and must at least have the RMI implementation.

Remote Management Application

A remote Management application connects to the JMX Agent, using the connector. Using this application, users can read/write various attributes value and also invoke operations on MBeans. JConsole and VisualVM are two examples of management applications. We can manage the MBean in the above example using JConsole.

Set Operation on MBean Attribute

Using JConsole, we have set the value of the attribute “NoOfStudents” to 54 from the initial value of 0. This is possible because we defined a setter method for setting the value of this attribute. If we had not written the implementation of the setter method, we would not have been allowed to set the value of this variable directly.

Invoke operation on MBean using remote management application.

On Clicking the incrementNoOfStudent button, JConsole will send the trigger to the JMX Agent to invoke this function to increase the number of the student by 1, that we defined in the class “JMXTutorial.java”. As seen from the figure, JConsole receives the response from the JMX agent that the operation is successfully performed.

Get Operation on MBean.

After performing the increment operation, the value of the attribute “NoOfStudents” increased from 54 to 55. Indicating that the operation was invoked successfully using JConsole.

CommentsLoad Comments