Overview of Java SE Monitoring and Management
This chapter introduces the features and utilities that provide monitoring and management services to the Java Platform, Standard Edition (Java SE platform). The features introduced here will be expanded upon in the following chapters of this guide.
Key Monitoring and Management Features
The Java SE platform includes significant monitoring and management features. These features fall into four broad categories.
-
Instrumentation for the Java Virtual Machine (Java VM).
-
Monitoring and Management application programming interfaces (API).
-
Monitoring and Management tools.
-
The Java Management Extensions (JMX) technology.
These categories of monitoring and management features are introduced further in the next sections.
Java VM Instrumentation
The Java VM is instrumented for monitoring and management, enabling built-in (or out-of-the-box) management capabilities that can be accessed both remotely and locally. For more information, see Chapter 2, Monitoring and Management Using JMX Technology and Chapter 5, SNMP Monitoring and Management.
The Java VM includes a platform MBean server and platform MBeans for use by management applications that conform to the JMX specification. These are implementations of the monitoring and management API described in the next section. Platform MXBeans and MBean servers are introduced in Platform MXBeans and Platform MBean Server.
Example code is provided in the JDK_HOME/demo/management directory, where JDK_HOME is the directory in which the Java Development Kit (JDK) is installed.
Monitoring and Management API
The java.lang.management package provides the interface for monitoring and managing the Java VM. This API provides access to the following types of information.
-
Number of classes loaded and threads running.
-
Java VM uptime, system properties, and VM input arguments.
-
Thread state, thread contention statistics, and stack trace of live threads.
-
Memory consumption.
-
Garbage collection statistics.
-
Low memory detection.
-
On-demand deadlock detection.
-
Operating system information.
In addition to the java.lang.management API, the java.util.logging.LoggingMXBean API provides allows you to perform monitoring and management of logging.
Monitoring and Management Tools
The Java SE platform provides a graphical monitoring tool called JConsole. JConsole implements the JMX API and enables you to monitor the performance of a Java VM and any instrumented applications, by providing information to help you optimize performance. Introduced in the Java SE platform 5.0, JConsole became an officially supported feature of the platform in the Java SE 6.
Some of the enhancements that have been made to JConsole between these two releases of the Java SE platform are as follows.
-
JConsole Plug-in support, that allows you to build your own plug-ins to run with JConsole, for example, to add a custom tab for accessing your applications' MBeans.
-
Dynamic attach capability, allowing you to connect JConsole to any application that supports the Attach API, that was added to the Java SE platform, version 6.
-
Enhanced user interface, that makes data more easily accessible.
-
New Overview and VM Summary tabs, for a better presentation of general information about your Java VM.
-
The HotSpot Diagnostic MBean, which provides an API to request heap dump at runtime and also change the setting of certain VM options.
-
Improved presentation of MBeans, to make it easier to access your MBeans' operations and attributes.
JConsole is presented in full in Chapter 3, Using JConsole.
Other command-line tools are also supplied with the Java SE platform. See the Monitoring Tools section of the JDK Development Tools document for more information.
Java Management Extensions (JMX) Technology
The Java SE platform, version 8 includes the JMX specification, version 1.4. The JMX API allows you to instrument applications for monitoring and management. An RMI connector allows this instrumentation to be remotely accessible, for example by JConsole.
For more information, see the JMX technology documentation for the Java SE platform. A very brief introduction to the main components of the JMX API is included in the next sections.
What are MBeans?
JMX technology MBeans are managed beans, namely Java objects that represent resources to be managed. An MBean has a management interface consisting of the following.
-
Named and typed attributes that can be read and written.
-
Named and typed operations that can be invoked.
-
Typed notifications that can be emitted by the MBean.
For example, an MBean representing an application's configuration could have attributes representing different configuration parameters, such as a cache size. Reading the CacheSize attribute would return the current size of the cache. Writing CacheSize would update the size of the cache, potentially changing the behavior of the running application. An operation such as save could store the current configuration persistently. The MBean could send a notification such as ConfigurationChangedNotification when the configuration changes.
MBeans can be standard or dynamic. Standard MBeans are Java objects that conform to design patterns derived from the JavaBeans component model. Dynamic MBeans define their management interface at runtime. More recently, an additional type of MBean called an MXBean has also been added to the Java platform.
-
A standard MBean exposes the resource to be managed directly through its attributes and operations. Attributes are exposed through "getter" and "setter" methods. Operations are the other methods of the class that are available to managers. All these methods are defined statically in the MBean interface and are visible to a JMX agent through introspection. This is the most straightforward way of making a new resource manageable.
-
A dynamic MBean is an MBean that defines its management interface at runtime. For example, a configuration MBean could determine the names and types of the attributes it exposes by parsing an XML file.
-
An MXBean is a type of MBean that provides a simple way to code an MBean that only references a predefined set of types. In this way, you can be sure that your MBean will be usable by any client, including remote clients, without any requirement that the client have access to model-specific classes representing the types of your MBeans. The platform MBeans introduced below are all MXBeans.
MBean Server
To be useful, an MBean must be registered in an MBean server. An MBean Server is a repository of MBeans. Each MBean is registered with a unique name within the MBean server. Usually the only access to the MBeans is through the MBean server. In other words, code does not access an MBean directly, but rather accesses the MBean by name through the MBean server.
The Java SE platform includes a built-in platform MBean server. For more information, see Chapter 4, Using the Platform MBean Server and Platform MXBeans.
Creating and Registering MBeans
There are two ways to create an MBean. One is to construct a Java object that will be the MBean, then use the registerMBean method to register it in the MBean Server. The other is to create and register the MBean in a single operation using one of the createMBean methods.
The registerMBean method is simpler for local use, but cannot be used remotely. The createMBean method can be used remotely, but sometimes requires attention to class loading issues. An MBean can perform actions when it is registered in or unregistered from an MBean Server if it implements the MBeanRegistration interface.
Instrumenting Applications
General instructions on how to instrument your applications for management by the JMX API is beyond the scope of this document. See the documentation for the Java Management Extensions (JMX) Technology for information.
Platform MXBeans
A platform MXBean is an MBean for monitoring and managing the Java VM and other components of the Java Runtime Environment (JRE). Each MXBean encapsulates a part of VM functionality such as the class loading system, just-in-time (JIT) compilation system, garbage collector, and so on.
Table 1-1 lists all the platform MXBeans and the aspect of the VM that they manage. Each platform MXBean has a unique javax.management.ObjectName for registration in the platform MBean server. A Java VM may have zero, one, or more than one instance of each MXBean, depending on its function, as shown in the table.
Table 1-1 Platform MXBeans
Interface | Part of VM Managed | Object Name | Instances per VM |
---|---|---|---|
ClassLoadingMXBean |
Class loading system |
java.lang:type= ClassLoading |
One |
CompilationMXBean |
Compilation system |
java.lang:type= Compilation |
Zero or one |
GarbageCollectorMXBean |
Garbage collector |
java.lang:type= GarbageCollector, name=collectorName |
One or more |
LoggingMXBean |
Logging system |
java.util.logging:type =Logging |
One |
MemoryManagerMXBean (sub-interface of GarbageCollectorMXBean) |
Memory pool |
java.lang: typeMemoryManager, name=managerName |
One or more |
MemoryPoolMXBean |
Memory |
java.lang: type= MemoryPool, name=poolName |
One or more |
MemoryMXBean |
Memory system |
java.lang:type= Memory |
One |
OperatingSystemMXBean |
Underlying operating system |
java.lang:type= OperatingSystem |
One |
RuntimeMXBean |
Runtime system |
java.lang:type= Runtime |
One |
ThreadMXBean |
Thread system |
java.lang:type= Threading |
One |
See the package description in the API reference for the java.lang.management package for details of the platform MXBeans (apart from LoggingMXBean). See the API reference for java.util.logging for details of the LoggingMXBean.
Platform MBean Server
The platform MBean Server can be shared by different managed components running within the same Java VM. You can access the platform MBean Server with the method ManagementFactory.getPlatformMBeanServer(). The first call to this method, creates the platform MBean server and registers the platform MXBeans using their unique object names. Subsequently, it returns the initially created platform MBeanServer instance.
MXBeans that are created and destroyed dynamically (for example, memory pools and managers) will automatically be registered and unregistered in the platform MBean server. If the system property javax.management.builder.initial is set, the platform MBean server will be created by the specified MBeanServerBuilder.
You can use the platform MBean server to register other MBeans besides the platform MXBeans. This enables all MBeans to be published through the same MBean server and makes network publishing and discovery easier.