Problem with remote JMX connection and Notifications

4.5k Views Asked by At

I'm trying to follow one example from the Java API documentation (http://download.oracle.com/javase/1.5.0/docs/api/java/lang/management/MemoryPoolMXBean.html#Notification) related to the UsageThreshold property of the Memory Pool Beans and notifications. My intention is to do something every time the pool overcomes the threshold. This is the sample code:

MemoryPoolMXBean remoteOldGenMemoryPool =
    ManagementFactory.newPlatformMXBeanProxy(
        jmxServer,
        "java.lang:type=MemoryPool,name=PS Old Gen",
        MemoryPoolMXBean.class);


class MyListener implements javax.management.NotificationListener {
    public void handleNotification(Notification notification, Object handback)  {
      String notifType = notification.getType();
      if (notifType.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
        // Do Something
        println "Threshold passed";
      }
    }
  }

// Register MyListener with MemoryMXBean
MemoryMXBean remoteMemory =
      ManagementFactory.newPlatformMXBeanProxy(
          jmxServer,
          ManagementFactory.MEMORY_MXBEAN_NAME,
          MemoryMXBean.class);

NotificationEmitter emitter = remoteMemory as NotificationEmitter;
MyListener listener = new MyListener();
emitter.addNotificationListener(listener, null, null);

remoteOldGenMemoryPool.setUsageThreshold 500000000;

When I execute the code and connect to my JVM I can see the following:

Threshold passed
02-Feb-2011 16:30:00 ClientCommunicatorAdmin restart
WARNING: Failed to restart: java.io.IOException: Failed to get a RMI stub: javax.naming.CommunicationException [Root exception is java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is: 
    java.net.SocketException: Connection reset]
02-Feb-2011 16:30:03 RMIConnector RMIClientCommunicatorAdmin-doStop
WARNING: Failed to call the method close():java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is: 
    java.net.SocketException: Connection reset
02-Feb-2011 16:30:03 ClientCommunicatorAdmin Checker-run
WARNING: Failed to check connection: java.net.SocketException: Connection reset
02-Feb-2011 16:30:03 ClientCommunicatorAdmin Checker-run
WARNING: stopping

For some reason (that I don't understand yet) the code is trying to restart the connection to JVM. Any ideas why this can be happening or how to prevent it? Am I doing somehing wrong?

Thanks

1

There are 1 best solutions below

0
On

May be you can add a variable to the jmx enviroment, like this: m.put("jmx.remote.x.client.connection.check.period", 0L);

But, what i have encountered is little different from this. you can see : http://chainhou.iteye.com/blog/1906688.