garbage collection - Java: GarbageCollectorMXBean getCollectionCount throws java.io.IOException: The client has been closed -


i writing java application using java garbagecollectormxbean apis collection count @ regular intervals (for every 5 seconds). below program have written task.

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

}

the program runs fine, starts giving following ioexception repeatedly in every loop.

exception in thread "main" java.io.ioexception: client has been closed. @ java.util.timerthread.run(timer.java:505) @ com.sun.jmx.remote.internal.clientcommunicatoradmin.restart(clientcommunicatoradmin.java:94) @ com.sun.jmx.remote.internal.clientcommunicatoradmin.gotioexception(clientcommunicatoradmin.java:54) @ javax.management.remote.rmi.rmiconnector$rmiclientcommunicatoradmin.gotioexception(rmiconnector.java:1470) @ javax.management.remote.rmi.rmiconnector$remotembeanserverconnection.getattribute(rmiconnector.java:906) @ com.ibm.lang.management.opentypemappingihandler$6.run(opentypemappingihandler.java:506) @ java.security.accesscontroller.doprivileged(accesscontroller.java:330) @ com.ibm.lang.management.opentypemappingihandler.invokeattributegetter(opentypemappingihandler.java:501) @ com.ibm.lang.management.opentypemappingihandler.invoke(opentypemappingihandler.java:121) @ com.sun.proxy.$proxy112.getcollectioncount(unknown source) @ jmxtest.main(jmxtest.java:48) 

looking @ code, exception caught in catch block fields initialized null , in next loop fields reinitialized. but, looking @ logs, once exception starts coming, above exception @ getcollectioncount() call in every loop. wonder though objects re-initialized, every time same exception.

i looking @ following things above information

  1. in cases, exception java.io.ioexception: client has been closed. in above scenario. know, if call jmxconnector.close() , use created garbagecollectormxbean object collection count, this. code not follow path.
  2. for above issue, jmxserver remote jmx server contributes? tried reproduce stopping/restarting remote jmx server, not it.

judging stack trace last line :

at com.sun.proxy.$proxy112.getcollectioncount(unknown source) @ jmxtest.main(jmxtest.java:48) 

it seems problem line of code

if (jmxconnector != null)             {                 try                 {                     jmxconnector.close();                 } catch (ioexception ioe) {}                 jmxconnector = null;   //...line no.48             }             mbsc = null;    //......this causing issue 

@raghavendra , acquire "mbeanserverconnection" object "jmxconnector" , should close objects / nullify them in order i.e. change code

if (jmxconnector != null)             {                 try                 {                     mbsc = null;    //...object handle assigned null before closing connector                      jmxconnector.close();                 } catch (ioexception ioe) {}                 jmxconnector = null;             } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -