gpt4 book ai didi

java - 管理 Java 虚拟机的 JMX 功能 Out-Of-Box

转载 作者:行者123 更新时间:2023-12-02 00:52:26 29 4
gpt4 key购买 nike

在研究JMX时,我看到它的一个重要特性是它可以管理JVM本身,但我不明白它在什么意义上可以管理JVM。那么任何人都可以用一些例子来详细说明这一点吗?

最佳答案

您可以很容易地亲眼看到这一点。

  • 第 1 步:下载 JConsole
  • 第 2 步:启动 Java 进程(Java 5 或更高版本)
  • 第 3 步:使用 JConsole 连接到您的 Java 进程
  • 第 4 步:查看用于触发堆转储事件、垃圾收集请求、线程信息、加载的类等的 MBean

特别有趣的是,您可以编写代码来访问正在运行的 Java 程序的 MBean:

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();

另请参阅The Java Language Management API

关于java - 管理 Java 虚拟机的 JMX 功能 Out-Of-Box,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2452526/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com