访问远程GarbageCollectorMXBean

时间:2009-06-09 15:10:24

标签: java jmx mbeans

我可以访问其他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。

1 个答案:

答案 0 :(得分:5)

实际上我需要先查询:

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