【发布时间】:2010-03-16 06:39:25
【问题描述】:
在学习JMX的过程中,我看到了它的一个重要特性是它可以管理JVM本身,我不明白它在什么意义上可以管理JVM。那么任何人都可以通过一些例子来详细说明这一点。
【问题讨论】:
在学习JMX的过程中,我看到了它的一个重要特性是它可以管理JVM本身,我不明白它在什么意义上可以管理JVM。那么任何人都可以通过一些例子来详细说明这一点。
【问题讨论】:
您自己可以很容易地看到这一点。
特别有趣的是,您可以编写代码来访问正在运行的 Java 程序的 MBean:
有三种不同的方法 访问管理界面。称呼 直接在 MXBean 中的方法 在同一个 Java 虚拟机中。
RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();
// Get the standard attribute "VmVendor" String vendor = mxbean.getVmVendor();
通过一个 MBeanServerConnection 连接到平台 MBeanServer 正在运行的虚拟机。
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
... }
使用 MXBean 代理。
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();
【讨论】: