Provides the management interface for monitoring and management of the
Java virtual machine as well as the operating system on which the
Java virtual machine is running. It allows both local and remote
monitoring and management of the running Java virtual machine.
Platform MXBeans
This package defines the management interface of the following
components:
A platform MXBean is a managed bean that defines the management
interface for one component for the platform and is specified in the
ManagementFactory class.
An application can monitor the instrumentation of the
Java virtual machine and manage certain characteristics in
the following ways:
- Direct access to an MXBean interface
- Get the MXBean instance through the static factory method
and access the MXBean interface locally of the running
virtual machine.
- Construct an MXBean proxy instance that
forwards the method calls to a given
MBeanServer
by calling
ManagementFactory.newPlatformMXBeanProxy
.
A proxy is typically constructed to remotely access
an MXBean of another running virtual machine.
- Indirect access via
MBeanServer
interface
- Go through the
platform MBeanServer
to access MXBeans locally or
a specific MBeanServerConnection to access
MXBeans remotely.
The attributes and operations of an MXBean use only
JMX open types which include basic data types,
CompositeData
,
and TabularData
defined in OpenType
.
Below shows a few
examples of different
ways to access MXBeans.
ManagementFactory
The
ManagementFactory
class is the management
factory class for the Java platform. This class provides a set of
static factory methods to obtain the MXBeans for the Java platform
to allow an application to access the MXBeans directly.
A platform MBeanServer can be accessed with the
getPlatformMBeanServer
method. On the first call to this method,
it creates the platform MBeanServer and registers all platform MXBeans
including platform MXBeans defined in other packages such as
LoggingMXBean
.
Each platform MXBean is registered with a unique name defined in the
ManagementFactory
class
for constructing ObjectName
.
This is a single MBeanServer that can be shared by different managed
components running within the same Java virtual machine.
Interoperability
A management application and a platform MBeanServer of a running
virtual machine can interoperate
without requiring classes used by the platform MXBean interfaces.
The data types being transmitted between the JMX connector
server and the connector client are JMX
open types
and
this allows interoperation across versions.
A data type used by the MXBean interfaces are mapped to
an open type when being accessed via MBeanServer interface.
The data type mapping is specified in the
ManagementFactory
class.
There are three different ways to access the management interfaces.
- Call the methods in the MXBean directly within the same
Java virtual machine.
RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();
// Get the standard attribute "VmVendor"
String vendor = mxbean.getVmVendor();
- Go through a MBeanServerConnection connecting
to the platform MBeanServer of a running virtual machine.
MBeanServerConnection mbs;
// Connect to a running JVM (or itself) and get MBeanServerConnection
// that has the JVM MXBeans registered in it
...
try {
// Assuming the RuntimeMXBean has been registered in mbs
ObjectName oname = new ObjectName(ManagementFactory.RUNTIME_MXBEAN_NAME);
// Get standard attribute "VmVendor"
String vendor = (String) mbs.getAttribute(oname, "VmVendor");
} catch (....) {
// Catch the exceptions thrown by ObjectName constructor
// and MBeanServer.getAttribute method
...
}
- Use MXBean proxy.
MBeanServerConnection mbs;
// Connect to a running JVM (or itself) and get MBeanServerConnection
// that has the JVM MBeans registered in it
...
// Get a MBean proxy for RuntimeMXBean interface
RuntimeMXBean proxy =
ManagementFactory.newPlatformMXBeanProxy(mbs,
ManagementFactory.RUNTIME_MXBEAN_NAME,
RuntimeMXBean.class);
// Get standard attribute "VmVendor"
String vendor = proxy.getVmVendor();
A Java virtual machine implementation may add its platform extension to
the management interface by defining platform-dependent
interfaces that extend the standard management interfaces to include
platform-specific metrics and management operations.
The static factory methods in the
ManagementFactory class will
return the MBeans with the platform extension.
It is recommended to name the platform-specific attributes with
a vendor-specific prefix such as the vendor's name to
avoid collisions of the attribute name between the future extension
to the standard management interface and the platform extension.
If the future extension to the standard management interface defines
a new attribute for a management interface and the attribute name
is happened to be same as some vendor-specific attribute's name,
the applications accessing that vendor-specific attribute would have
to be modified to cope with versioning and compatibility issues.
Below is an example showing how to access a platform-specific
attribute from Sun's implementation of the RuntimeMXBean.
1) Direct access to the Sun-specific MXBean interface
com.sun.management.RuntimeMXBean mxbean =
(com.sun.management.RuntimeMXBean) ManagementFactory.getRuntimeMXBean();
// Get the standard attribute "VmVendor"
String vendor = mxbean.getVmVendor();
// Get the platform-specific attribute "Bar"
BarType bar = mxbean.getBar();
2) Access the Sun-specific MXBean interface via MBeanServer
MBeanServerConnection mbs;
// Connect to a running JVM (or itself) and get MBeanServerConnection
// that has the JVM MXBeans registered in it
...
try {
// Assuming the RuntimeMXBean has been registered in mbs
ObjectName oname = new ObjectName(ManagementFactory.RUNTIME_MXBEAN_NAME);
// Get standard attribute "VmVendor"
String vendor = (String) mbs.getAttribute(oname, "VmVendor");
// Check if this MXBean contains Sun's extension
if (mbs.isInstanceOf(oname, "com.sun.management.RuntimeMXBean")) {
// Get platform-specific attribute "Bar"
BarType bar = (String) mbs.getAttribute(oname, "Bar");
}
} catch (....) {
// Catch the exceptions thrown by ObjectName constructor
// and MBeanServer methods
...
}
Unless otherwise noted, passing a null argument to a constructor
or method in any class or interface in this package will cause a NullPointerException
to be thrown.
The java.lang.management API is thread-safe.