Module java.management

Class ManagementFactory

java.lang.Object
java.lang.management.ManagementFactory

public class ManagementFactory extends Object
ManagementFactory类是用于获取Java平台管理Bean的工厂类。该类由一组静态方法组成,每个方法返回一个或多个代表Java虚拟机组件管理接口的平台MXBeans

平台MXBeans

平台MXBean是符合JMX仪器规范的管理Bean,仅使用一组基本数据类型。JMX管理应用程序和平台MBeanServer可以在不需要MXBean特定数据类型的类的情况下进行互操作。在JMX连接器服务器和连接器客户端之间传输的数据类型是开放类型,这允许跨版本进行互操作。有关详细信息,请参阅MXBeans规范

每个平台MXBean都是一个PlatformManagedObject,并且具有一个唯一的ObjectName,用于在平台MBeanServer中注册,由getObjectName方法返回。

应用程序可以通过以下方式访问平台MXBean:

1. 直接访问MXBean接口

2. 通过MBeanServer间接访问MXBean接口

  • 通过平台MBeanServer访问本地MXBeans或特定MBeanServerConnection访问远程MXBeans。MXBean的属性和操作仅使用JMX开放类型,包括基本数据类型,CompositeDataTabularDataOpenType中定义。映射在MXBean规范中指定了详细信息。

getPlatformManagementInterfaces方法返回Java虚拟机中支持的所有管理接口,包括下表中列出的标准管理接口以及JDK实现扩展的管理接口。

Java虚拟机具有以下管理接口的单个实例:

管理接口及其单个实例列表
管理接口 ObjectName
ClassLoadingMXBean java.lang:type=ClassLoading
MemoryMXBean java.lang:type=Memory
ThreadMXBean java.lang:type=Threading
RuntimeMXBean java.lang:type=Runtime
OperatingSystemMXBean java.lang:type=OperatingSystem
PlatformLoggingMXBean java.util.logging:type=Logging

Java虚拟机具有以下管理接口的零个或单个实例。

管理接口及其单个实例列表
管理接口 ObjectName
CompilationMXBean java.lang:type=Compilation

Java虚拟机可能具有以下管理接口的一个或多个实例。

管理接口及其单个实例列表
管理接口 ObjectName
GarbageCollectorMXBean java.lang:type=GarbageCollector,name=收集器名称
MemoryManagerMXBean java.lang:type=MemoryManager,name=管理器名称
MemoryPoolMXBean java.lang:type=MemoryPool,name=池名称
BufferPoolMXBean java.nio:type=BufferPool,name=池名称
自:
1.5
参见:
  • Field Details

    • CLASS_LOADING_MXBEAN_NAME

      public static final String CLASS_LOADING_MXBEAN_NAME
      ClassLoadingMXBeanObjectName的字符串表示。
      参见:
    • COMPILATION_MXBEAN_NAME

      public static final String COMPILATION_MXBEAN_NAME
      CompilationMXBeanObjectName的字符串表示。
      参见:
    • MEMORY_MXBEAN_NAME

      public static final String MEMORY_MXBEAN_NAME
      MemoryMXBeanObjectName的字符串表示。
      参见:
    • OPERATING_SYSTEM_MXBEAN_NAME

      public static final String OPERATING_SYSTEM_MXBEAN_NAME
      OperatingSystemMXBeanObjectName的字符串表示。
      参见:
    • RUNTIME_MXBEAN_NAME

      public static final String RUNTIME_MXBEAN_NAME
      RuntimeMXBeanObjectName的字符串表示。
      参见:
    • THREAD_MXBEAN_NAME

      public static final String THREAD_MXBEAN_NAME
      ThreadMXBeanObjectName的字符串表示。
      参见:
    • GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE

      public static final String GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
      GarbageCollectorMXBeanObjectName的域名和类型键属性。一个GarbageCollectorMXBean的唯一ObjectName可以通过将此字符串与",name=收集器名称"连接而形成。
      参见:
    • MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE

      public static final String MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE
      MemoryManagerMXBeanObjectName的域名和类型键属性。一个MemoryManagerMXBean的唯一ObjectName可以通过将此字符串与",name=管理器名称"连接而形成。
      参见:
    • MEMORY_POOL_MXBEAN_DOMAIN_TYPE

      public static final String MEMORY_POOL_MXBEAN_DOMAIN_TYPE
      MemoryPoolMXBeanObjectName的域名和类型键属性。一个MemoryPoolMXBean的唯一ObjectName可以通过将此字符串与,name=池名称连接而形成。
      参见:
  • Method Details

    • getClassLoadingMXBean

      public static ClassLoadingMXBean getClassLoadingMXBean()
      返回Java虚拟机的类加载系统的托管bean。
      返回:
      用于Java虚拟机的ClassLoadingMXBean对象。
    • getMemoryMXBean

      public static MemoryMXBean getMemoryMXBean()
      返回Java虚拟机的内存系统的托管bean。
      返回:
      用于Java虚拟机的MemoryMXBean对象。
    • getThreadMXBean

      public static ThreadMXBean getThreadMXBean()
      返回Java虚拟机的线程系统的托管bean。
      返回:
      用于Java虚拟机的ThreadMXBean对象。
    • getRuntimeMXBean

      public static RuntimeMXBean getRuntimeMXBean()
      返回Java虚拟机的运行时系统的托管bean。
      返回:
      用于Java虚拟机的RuntimeMXBean对象。
    • getCompilationMXBean

      public static CompilationMXBean getCompilationMXBean()
      返回Java虚拟机的编译系统的托管bean。如果Java虚拟机没有编译系统,则此方法返回null
      返回:
      用于Java虚拟机的CompilationMXBean对象,如果Java虚拟机没有编译系统,则返回null
    • getOperatingSystemMXBean

      public static OperatingSystemMXBean getOperatingSystemMXBean()
      返回Java虚拟机正在运行的操作系统的托管bean。
      返回:
      用于Java虚拟机的OperatingSystemMXBean对象。
    • getMemoryPoolMXBeans

      public static List<MemoryPoolMXBean> getMemoryPoolMXBeans()
      返回Java虚拟机中的MemoryPoolMXBean对象列表。Java虚拟机可以拥有一个或多个内存池,在执行过程中可能会添加或删除内存池。
      返回:
      一个MemoryPoolMXBean对象列表。
    • getMemoryManagerMXBeans

      public static List<MemoryManagerMXBean> getMemoryManagerMXBeans()
      返回Java虚拟机中的MemoryManagerMXBean对象列表。Java虚拟机可以拥有一个或多个内存管理器,在执行过程中可能会添加或删除内存管理器。
      返回:
      一个MemoryManagerMXBean对象列表。
    • getGarbageCollectorMXBeans

      public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans()
      返回Java虚拟机中的GarbageCollectorMXBean对象列表。Java虚拟机可能拥有一个或多个GarbageCollectorMXBean对象,在执行过程中可能会添加或删除GarbageCollectorMXBean
      返回:
      一个GarbageCollectorMXBean对象列表。
    • getPlatformMBeanServer

      public static MBeanServer getPlatformMBeanServer()
      返回平台MBeanServer。在第一次调用此方法时,它首先通过调用MBeanServerFactory.createMBeanServer方法创建平台MBeanServer,并将每个平台MXBean注册到此平台MBeanServer中,带有其ObjectName。在随后的调用中,此方法将简单地返回最初创建的平台MBeanServer

      动态创建和销毁的MXBeans,例如内存管理器,将自动注册和注销到平台MBeanServer中。

      如果设置了系统属性javax.management.builder.initial,平台MBeanServer的创建将由指定的MBeanServerBuilder完成。

      建议除了平台MXBeans之外,还应使用此平台MBeanServer来注册其他应用程序托管的bean。这将允许通过相同的MBeanServer发布和发现所有MBeans,从而更容易进行网络发布和发现。应避免与平台MXBeans的名称冲突。

      Returns:
      the platform MBeanServer; the platform MXBeans are registered into the platform MBeanServer at the first time this method is called.
      Throws:
      SecurityException - if there is a security manager and the caller does not have the permission required by MBeanServerFactory.createMBeanServer().
      See Also:
    • newPlatformMXBeanProxy

      public static <T> T newPlatformMXBeanProxy(MBeanServerConnection connection, String mxbeanName, Class<T> mxbeanInterface) throws IOException
      Returns a proxy for a platform MXBean interface of a given MXBean name that forwards its method calls through the given MBeanServerConnection.

      This method is equivalent to:

      Proxy.newProxyInstance(mxbeanInterface.getClassLoader(), new Class[] { mxbeanInterface }, handler)
      where handler is an InvocationHandler to which method invocations to the MXBean interface are dispatched. This handler converts an input parameter from an MXBean data type to its mapped open type before forwarding to the MBeanServer and converts a return value from an MXBean method call through the MBeanServer from an open type to the corresponding return type declared in the MXBean interface.

      If the MXBean is a notification emitter (i.e., it implements NotificationEmitter), both the mxbeanInterface and NotificationEmitter will be implemented by this proxy.

      Notes:

      1. Using an MXBean proxy is a convenience remote access to a platform MXBean of a running virtual machine. All method calls to the MXBean proxy are forwarded to an MBeanServerConnection where IOException may be thrown when the communication problem occurs with the connector server. If thrown, IOException will be wrappped in UndeclaredThrowableException. An application remotely accessing the platform MXBeans using proxy should prepare to catch UndeclaredThrowableException and handle its cause as if that cause had been thrown by the MBeanServerConnection interface.
      2. When a client application is designed to remotely access MXBeans for a running virtual machine whose version is different than the version on which the application is running, it should prepare to catch InvalidObjectException which is thrown when an MXBean proxy receives a name of an enum constant which is missing in the enum class loaded in the client application. If thrown, InvalidObjectException will be wrappped in UndeclaredThrowableException.
      3. MBeanServerInvocationHandler or its newProxyInstance method cannot be used to create a proxy for a platform MXBean. The proxy object created by MBeanServerInvocationHandler does not handle the properties of the platform MXBeans described in the class specification.
      Type Parameters:
      T - an mxbeanInterface type parameter
      Parameters:
      connection - the MBeanServerConnection to forward to.
      mxbeanName - the name of a platform MXBean within connection to forward to. mxbeanName must be in the format of ObjectName.
      mxbeanInterface - the MXBean interface to be implemented by the proxy.
      Returns:
      a proxy for a platform MXBean interface of a given MXBean name that forwards its method calls through the given MBeanServerConnection, or null if not exist.
      Throws:
      IllegalArgumentException - if
      • mxbeanName is not with a valid ObjectName format, or
      • the named MXBean in the connection is not a MXBean provided by the platform, or
      • the named MXBean is not registered in the MBeanServerConnection, or
      • the named MXBean is not an instance of the given mxbeanInterface
      IOException - if a communication problem occurred when accessing the MBeanServerConnection.
    • getPlatformMXBean

      public static <T extends PlatformManagedObject> T getPlatformMXBean(Class<T> mxbeanInterface)
      Returns the platform MXBean implementing the given mxbeanInterface which is specified to have one single instance in the Java virtual machine. This method may return null if the management interface is not implemented in the Java virtual machine (for example, a Java virtual machine with no compilation system does not implement CompilationMXBean); otherwise, this method is equivalent to calling:
          getPlatformMXBeans(mxbeanInterface).get(0);
       
      Type Parameters:
      T - an mxbeanInterface type parameter
      Parameters:
      mxbeanInterface - a management interface for a platform MXBean with one single instance in the Java virtual machine if implemented.
      Returns:
      the platform MXBean that implements mxbeanInterface, or null if not exist.
      Throws:
      IllegalArgumentException - if mxbeanInterface is not a platform management interface or not a singleton platform MXBean.
      Since:
      1.7
    • getPlatformMXBeans

      public static <T extends PlatformManagedObject> List<T> getPlatformMXBeans(Class<T> mxbeanInterface)
      Returns the list of platform MXBeans implementing the given mxbeanInterface in the Java virtual machine. The returned list may contain zero, one, or more instances. The number of instances in the returned list is defined in the specification of the given management interface. The order is undefined and there is no guarantee that the list returned is in the same order as previous invocations.
      Type Parameters:
      T - an mxbeanInterface type parameter
      Parameters:
      mxbeanInterface - a management interface for a platform MXBean
      Returns:
      the list of platform MXBeans that implement mxbeanInterface.
      Throws:
      IllegalArgumentException - if mxbeanInterface is not a platform management interface.
      Since:
      1.7
    • getPlatformMXBean

      public static <T extends PlatformManagedObject> T getPlatformMXBean(MBeanServerConnection connection, Class<T> mxbeanInterface) throws IOException
      Returns the platform MXBean proxy for mxbeanInterface which is specified to have one single instance in a Java virtual machine and the proxy will forward the method calls through the given MBeanServerConnection. This method may return null if the management interface is not implemented in the Java virtual machine being monitored (for example, a Java virtual machine with no compilation system does not implement CompilationMXBean); otherwise, this method is equivalent to calling:
           getPlatformMXBeans(connection, mxbeanInterface).get(0);
       
      Type Parameters:
      T - an mxbeanInterface type parameter
      Parameters:
      connection - the MBeanServerConnection to forward to.
      mxbeanInterface - a management interface for a platform MXBean with one single instance in the Java virtual machine being monitored, if implemented.
      Returns:
      the platform MXBean proxy for forwarding the method calls of the mxbeanInterface through the given MBeanServerConnection, or null if not exist.
      Throws:
      IllegalArgumentException - if mxbeanInterface is not a platform management interface or not a singleton platform MXBean.
      IOException - if a communication problem occurred when accessing the MBeanServerConnection.
      Since:
      1.7
      See Also:
    • getPlatformMXBeans

      public static <T extends PlatformManagedObject> List<T> getPlatformMXBeans(MBeanServerConnection connection, Class<T> mxbeanInterface) throws IOException
      返回通过给定的MBeanServerConnection转发mxbeanInterface方法调用的平台MXBean代理列表。返回的列表可能包含零个、一个或多个实例。返回列表中的实例数量在给定管理接口的规范中定义。顺序是未定义的,并且不能保证返回的列表与先前调用的顺序相同。
      类型参数:
      T - 一个mxbeanInterface类型参数
      参数:
      connection - 要转发到的MBeanServerConnection
      mxbeanInterface - 用于平台MXBean的管理接口
      返回:
      通过给定的MBeanServerConnection转发mxbeanInterface方法调用的平台MXBean代理列表。
      抛出:
      IllegalArgumentException - 如果mxbeanInterface不是平台管理接口。
      IOException - 如果访问MBeanServerConnection时发生通信问题。
      自版本:
      1.7
      参见:
    • getPlatformManagementInterfaces

      public static Set<Class<? extends PlatformManagedObject>> getPlatformManagementInterfaces()
      返回Class对象的集合,这些对象是PlatformManagedObject的子接口,表示用于监视和管理Java平台的所有管理接口。
      返回:
      表示用于监视和管理Java平台的所有管理接口的Class对象的集合,这些对象是PlatformManagedObject的子接口。
      自版本:
      1.7