GarbageCollectorMXBean

GarbageCollectorMXBean

我正在编写一个Java应用程序,其中使用Java GarbageCollectorMXBean API定期获取收集计数(每5秒一次)。下面是我编写的用于完成任务的程序。

import java.io.IOException;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Map;

import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class JMXTest {
    public static final String GC_NAME = "java.lang:name=MarkSweepCompact,type=GarbageCollector";
    private static GarbageCollectorMXBean garbageCollectorMXBean;
    private static JMXConnector jmxConnector;
    private static MBeanServerConnection mbsc;

    public static void main(String[] args) throws Exception {
        String rmiHostname = "jmxserver";
        String defaultUrl = "service:jmx:rmi:///jndi/rmi://" + rmiHostname + ":1999/jmxrmi";
        JMXServiceURL jmxServiceURL = new JMXServiceURL(defaultUrl);

        Map<String,Object> jmxCredentials = new HashMap<String,Object>();
        String[] credentials = new String[]{"jmxusername", "jmxpassword"};
        jmxCredentials.put("jmx.remote.credentials", credentials);

       boolean run = true;
       while(run){
         try {
            if(garbageCollectorMXBean == null){
                if (mbsc == null){
                    jmxConnector = JMXConnectorFactory.connect(jmxServiceURL, jmxCredentials);
                    mbsc = jmxConnector.getMBeanServerConnection();
                }
                garbageCollectorMXBean = ManagementFactory.newPlatformMXBeanProxy(mbsc, GC_NAME,GarbageCollectorMXBean.class);
            }
            long count = garbageCollectorMXBean.getCollectionCount();
            System.out.println("Garbage Collector count = " + count);
        } catch (Exception e) {
            e.printStackTrace();
            garbageCollectorMXBean = null;
            if (jmxConnector != null)
            {
                try
                {
                    jmxConnector.close();
                } catch (IOException ioe) {}
                jmxConnector = null;
            }
            mbsc = null;
        }
        Thread.currentThread().sleep(5000);
    }
}

}

该程序运行良好,但有时它会在每个循环中重复开始以下IOException。
Exception in thread "main" java.io.IOException: The client has been closed.
at java.util.TimerThread.run(Timer.java:505)
at com.sun.jmx.remote.internal.ClientCommunicatorAdmin.restart(ClientCommunicatorAdmin.java:94)
at com.sun.jmx.remote.internal.ClientCommunicatorAdmin.gotIOException(ClientCommunicatorAdmin.java:54)
at javax.management.remote.rmi.RMIConnector$RMIClientCommunicatorAdmin.gotIOException(RMIConnector.java:1470)
at javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection.getAttribute(RMIConnector.java:906)
at com.ibm.lang.management.OpenTypeMappingIHandler$6.run(OpenTypeMappingIHandler.java:506)
at java.security.AccessController.doPrivileged(AccessController.java:330)
at com.ibm.lang.management.OpenTypeMappingIHandler.invokeAttributeGetter(OpenTypeMappingIHandler.java:501)
at com.ibm.lang.management.OpenTypeMappingIHandler.invoke(OpenTypeMappingIHandler.java:121)
at com.sun.proxy.$Proxy112.getCollectionCount(Unknown Source)
at JMXTest.main(JMXTest.java:48)

查看代码,任何异常都将被捕获在catch块中,在该块中,所有字段都将初始化为null,并且在下一个循环中,所有字段将被重新初始化。但是,查看日志,一旦异常开始到来,我只会在每个循环的getCollectionCount()调用中获得上述异常。我想知道,即使对象被重新初始化,每次遇到相同的异常也是如此。

我正在根据以上信息查看以下内容
  • 在所有情况下,在上述情况下,我们都会获得此异常java.io.IOException: The client has been closed.。我知道,如果我们调用jmxConnector.close(),然后使用已经创建的garbageCollectorMXBean对象来获取集合计数,那么我们将得到这个。但是我的代码没有遵循这条路径。
  • 对于上述问题,jmxserver远程JMX服务器是否起作用?我试图通过停止/重新启动远程JMX服务器来进行重现,但无法做到。
  • 最佳答案

    从堆栈跟踪的最后一行来看:

    at com.sun.proxy.$Proxy112.getCollectionCount(Unknown Source)
    at JMXTest.main(JMXTest.java:48)
    

    似乎问题在于代码行
    if (jmxConnector != null)
                {
                    try
                    {
                        jmxConnector.close();
                    } catch (IOException ioe) {}
                    jmxConnector = null;   //...line no.48
                }
                mbsc = null;    //......this is probably causing the issue
    

    @raghavendra,当您从“JMXConnector”获取“MBeanServerConnection”对象时,应按顺序关闭对象或将其无效,即,将代码更改为
    if (jmxConnector != null)
                {
                    try
                    {
                        mbsc = null;    //...object handle assigned null before closing the connector
                        jmxConnector.close();
                    } catch (IOException ioe) {}
                    jmxConnector = null;
                }
    

    07-26 05:12