summaryrefslogtreecommitdiff
path: root/java/src/IceInternal/ConnectionMonitor.java
diff options
context:
space:
mode:
authorMarc Laukien <marc@zeroc.com>2003-01-17 19:00:16 +0000
committerMarc Laukien <marc@zeroc.com>2003-01-17 19:00:16 +0000
commit6ef4c296bc6b24017da915154f3937224da2b5af (patch)
tree8ed76ae778a13a682b7f06e21b5c0f9a227c460a /java/src/IceInternal/ConnectionMonitor.java
parentConnectionMonitor (diff)
downloadice-6ef4c296bc6b24017da915154f3937224da2b5af.tar.bz2
ice-6ef4c296bc6b24017da915154f3937224da2b5af.tar.xz
ice-6ef4c296bc6b24017da915154f3937224da2b5af.zip
ConnectionMonitor; fixed batch bug
Diffstat (limited to 'java/src/IceInternal/ConnectionMonitor.java')
-rw-r--r--java/src/IceInternal/ConnectionMonitor.java170
1 files changed, 170 insertions, 0 deletions
diff --git a/java/src/IceInternal/ConnectionMonitor.java b/java/src/IceInternal/ConnectionMonitor.java
new file mode 100644
index 00000000000..153e47f0261
--- /dev/null
+++ b/java/src/IceInternal/ConnectionMonitor.java
@@ -0,0 +1,170 @@
+// **********************************************************************
+//
+// Copyright (c) 2002
+// ZeroC, Inc.
+// Billerica, MA, USA
+//
+// All Rights Reserved.
+//
+// Ice is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License version 2 as published by
+// the Free Software Foundation.
+//
+// **********************************************************************
+
+package IceInternal;
+
+public class ConnectionMonitor extends Thread
+{
+ public void
+ destroy()
+ {
+ synchronized(this)
+ {
+ if(_instance == null)
+ {
+ return;
+ }
+
+ _instance = null;
+ _connections.clear();
+
+ notify();
+ }
+
+ while(true)
+ {
+ try
+ {
+ join();
+ break;
+ }
+ catch(java.lang.InterruptedException ex)
+ {
+ continue;
+ }
+ }
+ }
+
+ public synchronized void
+ add(Connection connection)
+ {
+ if(_instance == null)
+ {
+ throw new Ice.CommunicatorDestroyedException();
+ }
+
+ _connections.add(connection);
+ }
+
+ public synchronized void
+ remove(Connection connection)
+ {
+ if(_instance == null)
+ {
+ throw new Ice.CommunicatorDestroyedException();
+ }
+
+ _connections.remove(connection);
+ }
+
+ //
+ // Only for use by Instance.
+ //
+ ConnectionMonitor(Instance instance, int interval)
+ {
+ _instance = instance;
+ _interval = interval;
+
+ assert(_interval > 0);
+ start();
+ }
+
+ protected void
+ finalize()
+ throws Throwable
+ {
+ assert(_instance == null);
+ assert(_connections.isEmpty());
+
+ super.finalize();
+ }
+
+ public void
+ run()
+ {
+ java.util.HashSet connections = new java.util.HashSet();
+
+ while(true)
+ {
+ synchronized(this)
+ {
+ if(_instance == null)
+ {
+ return;
+ }
+
+ try
+ {
+ wait(_interval * 1000);
+ }
+ catch(InterruptedException ex)
+ {
+ continue;
+ }
+
+ if(_instance == null)
+ {
+ return;
+ }
+
+ connections.clear();
+ connections.addAll(_connections);
+ }
+
+ //
+ // Monitor connections outside the thread synchronization,
+ // so that connections can be added or removed during
+ // monitoring.
+ //
+ java.util.Iterator iter = connections.iterator();
+ while(iter.hasNext())
+ {
+ Connection connection = (Connection)iter.next();
+
+ try
+ {
+ connection.monitor();
+ }
+ catch(Ice.LocalException ex)
+ {
+ synchronized(this)
+ {
+ java.io.StringWriter sw = new java.io.StringWriter();
+ java.io.PrintWriter pw = new java.io.PrintWriter(sw);
+ ex.printStackTrace(pw);
+ pw.flush();
+ String s = "exception in thread pool thread " + getName() + ":\n" + sw.toString();
+ _instance.logger().error(s);
+ }
+ }
+ catch(RuntimeException ex)
+ {
+ synchronized(this)
+ {
+ java.io.StringWriter sw = new java.io.StringWriter();
+ java.io.PrintWriter pw = new java.io.PrintWriter(sw);
+ ex.printStackTrace(pw);
+ pw.flush();
+ String s = "unknown exception in thread pool thread " + getName() + ":\n" + sw.toString();
+ _instance.logger().error(s);
+ }
+ }
+ }
+ }
+ }
+
+ private Instance _instance;
+ private final int _interval;
+ private java.util.HashSet _connections = new java.util.HashSet();
+}