访问远程 GarbageCollectorMXBean

发布于 2024-07-22 22:26:43 字数 3206 浏览 7 评论 0原文

我可以访问其他 MXBean 类型,例如 RuntimeMXBean,但不能访问 GarabageCollectorMXBean:

JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL("service:jmx:rmi:///jndi/rmi://coretest:1542/jmxrmi"));
MBeanServerConnection mbs = connector.getMBeanServerConnection();
GarbageCollectorMXBean gc = ManagementFactory.newPlatformMXBeanProxy(mbs, ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE, GarbageCollectorMXBean.class);

结果是

Exception in thread "main" java.lang.IllegalArgumentException: java.lang:type=GarbageCollector not found in the connection.
at java.lang.management.ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.java:629)
at jmx.JMXConnectorSample.main(JMXConnectorSample.java:32)
Caused by: javax.management.InstanceNotFoundException: java.lang:type=GarbageCollector
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1094)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.isInstanceOf(DefaultMBeanServerInterceptor.java:1398)
at com.sun.jmx.mbeanserver.JmxMBeanServer.isInstanceOf(JmxMBeanServer.java:1051)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1432)
at javax.management.remote.rmi.RMIConnectionImpl.access$200(RMIConnectionImpl.java:72)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1264)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1359)
at javax.management.remote.rmi.RMIConnectionImpl.isInstanceOf(RMIConnectionImpl.java:898)
at sun.reflect.GeneratedMethodAccessor91.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)
at com.sun.jmx.remote.internal.PRef.invoke(Unknown Source)
at javax.management.remote.rmi.RMIConnectionImpl_Stub.isInstanceOf(Unknown Source)
at javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection.isInstanceOf(RMIConnector.java:1073)
at java.lang.management.ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.java:616)
... 1 more

这可能是因为有多个可用的 GarbageCollectorMXBean,但我在 ManagementFactory 上找不到其他相关方法用于访问多个远程 bean。

I can access other MXBean types, such as the RuntimeMXBean, but not the GarabageCollectorMXBean:

JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL("service:jmx:rmi:///jndi/rmi://coretest:1542/jmxrmi"));
MBeanServerConnection mbs = connector.getMBeanServerConnection();
GarbageCollectorMXBean gc = ManagementFactory.newPlatformMXBeanProxy(mbs, ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE, GarbageCollectorMXBean.class);

and the result is

Exception in thread "main" java.lang.IllegalArgumentException: java.lang:type=GarbageCollector not found in the connection.
at java.lang.management.ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.java:629)
at jmx.JMXConnectorSample.main(JMXConnectorSample.java:32)
Caused by: javax.management.InstanceNotFoundException: java.lang:type=GarbageCollector
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1094)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.isInstanceOf(DefaultMBeanServerInterceptor.java:1398)
at com.sun.jmx.mbeanserver.JmxMBeanServer.isInstanceOf(JmxMBeanServer.java:1051)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1432)
at javax.management.remote.rmi.RMIConnectionImpl.access$200(RMIConnectionImpl.java:72)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1264)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1359)
at javax.management.remote.rmi.RMIConnectionImpl.isInstanceOf(RMIConnectionImpl.java:898)
at sun.reflect.GeneratedMethodAccessor91.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)
at com.sun.jmx.remote.internal.PRef.invoke(Unknown Source)
at javax.management.remote.rmi.RMIConnectionImpl_Stub.isInstanceOf(Unknown Source)
at javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection.isInstanceOf(RMIConnector.java:1073)
at java.lang.management.ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.java:616)
... 1 more

This might be because there are multiple GarbageCollectorMXBeans available, but I can find no other relevant methods on the ManagementFactory for accessing multiple remote beans.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骄傲 2024-07-29 22:26:43

实际上我需要先查询:

ObjectName gcName = new ObjectName(ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",*");

for (ObjectName name : mbs.queryNames(gcName, null)) {
    GarbageCollectorMXBean gc = ManagementFactory.newPlatformMXBeanProxy(mbs, name.getCanonicalName(), GarbageCollectorMXBean.class);            
    System.out.println(gc.getName());            
}

Actually I needed to query first:

ObjectName gcName = new ObjectName(ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",*");

for (ObjectName name : mbs.queryNames(gcName, null)) {
    GarbageCollectorMXBean gc = ManagementFactory.newPlatformMXBeanProxy(mbs, name.getCanonicalName(), GarbageCollectorMXBean.class);            
    System.out.println(gc.getName());            
}
挽清梦 2024-07-29 22:26:43

您也可以使用(可能晚了 13 年...):

List<GarbageCollectorMXBean> gclist = ManagementFactory.getPlatformMXBeans(mbs,GarbageCollectorMXBean.class);

gclist 现在包含 MBean 服务器返回的所有收集器的代理。

现在您可以循环浏览列表以使用各个代理,如下所示:

for(GarbageCollectorMXBean gc : gclist){
                    System.out.println(gc.getName());
                }

您可以阅读上述函数 此处

You could also have used ( Might be 13 years too late... ):

List<GarbageCollectorMXBean> gclist = ManagementFactory.getPlatformMXBeans(mbs,GarbageCollectorMXBean.class);

gclist now contains proxies for all the collector's returned by the MBean Server.

Now you can loop through the list to use the individual proxies, like this:

for(GarbageCollectorMXBean gc : gclist){
                    System.out.println(gc.getName());
                }

You can read about the above function here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文