2011-02-02 103 views
3

我正在嘗試遵循Java API文檔中的一個示例(http://download.oracle.com/javase/1.5.0/docs/api/java/lang/management/ MemoryPoolMXBean.html#Notification)與Memory Pool Beans和通知的UsageThreshold屬性相關。每當游泳池超過門檻時,我的意圖就是做一些事情。這是示例代碼:遠程JMX連接和通知問題

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 2011-02-03 11:45:50

回答

0

可能是你可以添加一個變量JMX的環境,這樣的: m.put( 「jmx.remote.x.client.connection.check.period」,0L);

但是,我遇到的是與此有點不同。 你可以看到:http://chainhou.iteye.com/blog/1906688