# Overview of Monitoring and Management

- Key Features
- What are MBeans?
- Platform MBeans
- Platform MBean Server
- Logging Management

## Key Features

Starting with J2SE 5.0, the Java platform includes significant monitoring and management features:

- JVM Instrumentation
- Monitoring and Management APIs
- Tools
- Integrated JMX

### JVM Instrumentation

The Java virtual machine (JVM) is instrumented for monitoring and management, providing built-in ("out-of-the-box") management capabilities for for both remote and local access. For more information, see JMX Monitoring and Management and SNMP Monitoring and Management.

The JVM include a platform MBean server (management agent) and platform MBeans that JMX management applications can use. These are implementations of the monitoring and management API described below.

Example code is provided in the `JDK_HOME/demo/management` directory.

### Monitoring and Management APIs

The java.lang.management package provides the interface for monitoring and managing the JVM.

The API provides access to information such as:

- Number of classes loaded and threads running
- Virtual machine uptime, system properties, and JVM 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

The API includes logging monitoring and management capabilities. The java.util.logging.LoggingMXBean interface provides for management of the logging facility.

The com.sun.management package contains Sun Microsystems' platform extension to the management API.

### Tools

A graphical JMX monitoring tool, `jconsole`, enables you to monitor the performance of a JVM and instrumented applications, providing information to help you optimize performance. For more information, see Using jconsole. Other command-line tools are also available: see J2SE Monitoring and Management Tools for more information.

### Integrated JMX

Java Management Extensions (JMX) API version 1.2. The JMX API allows you to instrument applications for monitoring and management. The RMI connector allows this instrumentation to be remotely accessible. This API, formerly an optional extension, is now included in J2SE.

## What are MBeans?

MBeans are managed beans, Java objects that represent resources to be managed. An MBean has a management interface consisting of:

- 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 the 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.

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.

### MBean Server

To be useful, an MBean must be registered in an MBean Server, also called an MBean agent. 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.

Starting with release 5.0, J2SE includes a built-in platform MBean server. For more information, see Using the Platform MBean Server.

### 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 instruction on instrumenting your applications for JMX management is beyond the scope of this document. J2SE includes extensive JMX documentation, including:

- JMX Technology Overview
- JMX API Specification
- JMX Tutorial
- JMX Examples

## Platform MBeans

A platform MBean (also called an MXBean) is an MBean for monitoring and managing the Java Virtual Machine (JVM). Each MXBean encapsulates a part of JVM functionality such as the JVM's class loading system, JIT compilation system, garbage collector, and so on. The `java.lang.management` package defines the platform MXBeans.

Table 1 lists all the platform MBeans and the aspect of the VM that they manage. Each platform MXBean has a unique `javax.management.ObjectName` for registration in the platform MBeanServer. A JVM may have zero, one, or more than one instance of each MXBean, depending on its function, as shown in the table.

Platform MBeans
Interface | Manages | 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
`MemoryManagerMXBean` (sub-interface of GarbageCollectorMXBean) | Memory pool | `java.lang:type=MemoryManager, 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

## Platform MBean Server

The Platform MBean Server can be shared by different managed components running within the same Java Virtual Machine. You can access the Platform MBean Server with the method `ManagementFactory.getPlatformMBeanServer()`. The first call to this method, creates the platform `MBeanServer` and registers the platform MXBeans using their unique ObjectNames. Subsequently, it returns the initially created platform `MBeanServer`.

MXBeans that get created and destroyed dynamically, for example, memory pools and managers, will automatically be registered and deregistered into the platform `MBeanServer`. If the system property `javax.management.builder.initial` is set, the platform `MBeanServer` creation will be done by the specified `MBeanServerBuilder`.

Use the platform MBeanServer to register other MBeans besides the platform MXBeans. This enables all MBeans to be published through the same `MBeanServer` and makes network publishing and discovery easier.

## Logging Management

The LoggingMXBean enables you to:

- Get the name of the log level associated with the specified logger.
- Get the list of currently registered loggers.
- Get the name of the parent for the specified logger.
- Sets the specified logger to the specified new level.

The unique ObjectName of the LoggingMXBean is: `java.util.logging:type=Logging`, kept in `LogManager.LOGGING_MXBEAN_NAME.`

There is a single global instance of the `LoggingMXBean`, which you can get by calling `LogManager.getLoggingMXBean()`.