Those of you who have done application server monitoring under the Java platform are familiar with JMX, which simply provides a framework for standard management solutions. By management, I mean monitoring platform health, configuring resources at the application level, collecting application statistics, debugging, and monitoring server performance. JMX allows you to package all your resources (hardware and software) into Java objects and expose them to a distributed environment, and JMX provides a mechanism for Existing management protocols, such as SNMP, can be easily mapped to JMX’s own management structure.

This article is not an introduction to JMX, but an analysis of how Tomcat 7 uses JMX to provide administrative functions. If you are not familiar with JMX, you can do a Google search to learn about the technology. There are already some Chinese technical blogs on the web. For example, JMX step by Step on Kawakami IN BlogJava and some translated articles on JMX IN ACTION. Of course, the most authoritative reference is the Oracle documentation, which provides an official link to the JMX 1.4 specification.

Let’s take a look at the JMX management features in Tomcat 7, which can be accessed using JConsole after Tomcat is started:

The JMX standard provides four different types of MBeans:

  • Standard MBeans, which directly implement methods for managing objects, either by implementing a programmer-defined interface whose class name ends with “MBean” or by using a Standard MBean instance that takes a class as a constructor argument, Plus an optional interface class specification. This interface can expose some object methods for administration.

  • Dynamic MBean, which accesses properties dynamically with property accessors and invokes methods with a generalized invoke() method. The available methods are specified in the MBeanInfo interface. This approach is more flexible, but does not have the type safety of Standard MBeans. It greatly reduces coupling, and manageable POJOs (pure old-fashioned Java objects) do not need to implement a specific interface.

  • Model MBeans, which provide an improved abstraction layer and extend the Dynamic MBean Model to further reduce dependency on a given implementation. This can be helpful in situations where you might be using multiple versions of the JVM or where you need to manage third-party classes with loose coupling. The main difference between Dynamic mBeans and Model MBeans is that there is additional metadata in the Model MBeans.

  • Open MBeans, which are restricted Model MBeans that restrict types to a fixed set of types for maximum portability. By limiting data types, more adapters can be used, and technologies like SMTP can be more easily adapted to Java application management. This variant also specifies standard structures such as arrays and tables to improve the management of composite objects.

You can see the use of Standard mBeans and Dynamic MBeans in Tomcat 7, and this article covers both. Let’s start with a simpler standard MBean:

In the Tomcat startup class org. Apache. Catalina. Startup. The final part of Bootstrap createClassLoader method:

ClassLoader classLoader = ClassLoaderFactory.createClassLoader
    (repositories, parent);

// Retrieving MBean server
MBeanServer mBeanServer = null;
if (MBeanServerFactory.findMBeanServer(null).size() > 0) {
    mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
} else {
    mBeanServer = ManagementFactory.getPlatformMBeanServer();
}

// Register the server classloader
ObjectName objectName =
    new ObjectName("Catalina:type=ServerClassLoader,name=" + name);
mBeanServer.registerMBean(classLoader, objectName);
Copy the code

From ClassLoaderFactory. CreateClassLoader method is the last part of the implementation code:

return AccessController.doPrivileged(
        new PrivilegedAction<StandardClassLoader>() {
            @Override
            public StandardClassLoader run() {
                if (parent == null)
                    return new StandardClassLoader(array);
                else
                    returnnew StandardClassLoader(array, parent); }});Copy the code

You can see the above this object is actually a org. Apache. Catalina. Loader. StandardClassLoader instances of the class. Look at the definition of this class:

public class StandardClassLoader
    extends URLClassLoader
    implements StandardClassLoaderMBean 
Copy the code

It implements a StandardClassLoaderMBean interface. From here you can see that most of the code above mBeanServer. RegisterMBean registration is actually a Standard in an. However, the standard MBean is boring. None of the methods are open for management, so jConsole can only see the description of the MBean, and can not see its properties and methods: