远程JMX连接和通知的问题

4

我正在尝试跟踪Java API文档(http://download.oracle.com/javase/1.5.0/docs/api/java/lang/management/MemoryPoolMXBean.html#Notification)中与内存池Bean和通知的UsageThreshold属性相关的示例。我的意图是每当池超过阈值时都做一些事情。这是示例代码:

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;

当我执行代码并连接到我的JVM时,我可以看到以下内容:
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

由于某些原因(我还不理解),代码正在尝试重新启动与JVM的连接。有什么想法可以防止这种情况发生或者我做错了什么吗?
谢谢。

2
看起来实际上是保持JMX连接活动的问题。在ClientCommunicatorAdmin中有一个守护线程(Checher),每分钟检查一次连接。 - Gotxi
1个回答

0
也许你可以向jmx环境中添加一个变量,像这样: m.put("jmx.remote.x.client.connection.check.period", 0L);
但是,我遇到的情况与此略有不同。 你可以看看:http://chainhou.iteye.com/blog/1906688

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接