Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: GarbageCollectorMXBean getCollectionCount throws java.io.IOException: The client has been closed.

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default GarbageCollectorMXBean getCollectionCount throws java.io.IOException: The client has been closed.

    I am writing a Java application where I am using Java GarbageCollectorMXBean APIs to get the collection count at regular intervals (for every 5 seconds). Below is the program I have written to do the 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=GarbageColle ctor"; 
    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); 
    } 
    }

    he program runs fine but sometimes it starts giving following IOException repeatedly in every loop.

    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.ClientCommunicatorAdmi n.restart(ClientCommunicatorAdmin.java:94)
    at com.sun.jmx.remote.internal.ClientCommunicatorAdmi n.gotIOException(ClientCommunicatorAdmin.java:54)
    at javax.management.remote.rmi.RMIConnector$RMIClient CommunicatorAdmin.gotIOException(RMIConnector.java :1470)
    at javax.management.remote.rmi.RMIConnector$RemoteMBe anServerConnection.getAttribute(RMIConnector.java: 906)
    at com.ibm.lang.management.OpenTypeMappingIHandler$6. run(OpenTypeMappingIHandler.java:506)
    at java.security.AccessController.doPrivileged(Access Controller.java:330)
    at com.ibm.lang.management.OpenTypeMappingIHandler.in vokeAttributeGetter(OpenTypeMappingIHandler.java:5 01)
    at com.ibm.lang.management.OpenTypeMappingIHandler.in voke(OpenTypeMappingIHandler.java:121)
    at com.sun.proxy.$Proxy112.getCollectionCount(Unknown Source)
    at JMXTest.main(JMXTest.java:48)


    But if we look at the code, for any exception it will get caught in catch block where all the fields will be initialized to null and in the next loop all the fields will be reinitialized. But looking at the logs, Once the exception starts coming, I get above exception only at getCollectionCount() call in every loop. I wonder even though the objects are re initialized, every time I get same exception.

    I am looking at following things from the above information

    In what all cases, we get this exception java.io.IOException: The client has been closed. in the above scenario. I know if we call jmxConnector.close() and then use the already created garbageCollectorMXBean object to get the collection count, we get this. But my code does not follow that path.
    For the above issue, does jmxserver remote JMX server contributes ? I tried to reproduce by stopping/restarting the remote JMX server, but could not do it.

    Any help in this regard ?
    Last edited by raagu15; August 19th, 2014 at 12:31 PM. Reason: Proper formatting of Java code


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: GarbageCollectorMXBean getCollectionCount throws java.io.IOException: The client has been closed.

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly per the above link.

Similar Threads

  1. Replies: 22
    Last Post: June 21st, 2013, 02:11 AM
  2. getting closed server socket in client thread
    By shanalikhan in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 13th, 2013, 06:18 PM
  3. java.io.IOException: Premature EOF
    By CrazyVag in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: December 19th, 2012, 08:32 AM
  4. What Wrong With My Code: IOException.
    By Java Programmer in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 8th, 2012, 06:11 PM
  5. Help needed for "java.io.IOException: Not in GZIP format"
    By goldest in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: August 10th, 2010, 03:05 AM

Tags for this Thread