Sunday, July 15, 2012

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 asConfigurationChangedNotification 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.
  • 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.
  • 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 new 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 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
Part of VM Managed
Object Name
Instances per VM
Class loading system
java.lang:type= ClassLoading
Compilation system
java.lang:type= Compilation
Zero or one
Garbage collector
java.lang:type= GarbageCollector, name=collectorName
One or more
Logging system
java.util.logging:type =Logging
MemoryManagerMXBean (sub-interface of GarbageCollectorMXBean)
Memory pool
java.lang: typeMemoryManager, name=managerName
One or more
java.lang: type= MemoryPool, name=poolName
One or more
Memory system
java.lang:type= Memory
Underlying operating system
java.lang:type= OperatingSystem
Runtime system
java.lang:type= Runtime
Thread system
java.lang:type= Threading
See the package description in the API reference for the 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 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.