KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > smartlib > pool > core > ConnectionLeakPollThread


1 /*
2  * @(#) ConnectionLeakPollThread.java 1.0 02/08/01
3  */

4
5 package org.smartlib.pool.core;
6
7
8 import java.util.*;
9
10 /**
11  * The responsibility of this class is to poll the pools, detect leaks
12  * and release excessive free connections if required.
13  *
14  * Note: I know this class can be optimised, will do it later.
15  *
16  * @author Sachin Shekar Shetty
17  * @version 1.0, 02/08/01
18  */

19
20
21 class ConnectionLeakPollThread implements Runnable JavaDoc {
22
23     private Vector connectionsInUse ;
24     private Vector connectionListenerList;
25     private String JavaDoc poolName;
26     private long sleepTime;
27     private boolean keepGoing ;
28     private long leakTimeOut ;
29     private static Debugger debug;
30     private Pool pool;
31
32     /**
33      * @param connectionsInUse Vector containing the connections that
34      * are in use.
35      * @param connectionListenerList Vector of registered listenetrs.
36      * @param poolName Name of the pool.
37      * @param sleepTime Sleep time for the poll thread.
38      * @param leakTimeOut time interval after which a leak is said to
39      * have occurred.
40      * @param pool A reference to Pool implementation.
41      *
42      */

43     ConnectionLeakPollThread(Vector connectionsInUse
44                 , Vector connectionListenerList , String JavaDoc poolName
45                 , long sleepTime , long leakTimeOut , Pool pool) {
46
47         this.connectionsInUse = connectionsInUse;
48         this.connectionListenerList = connectionListenerList;
49         this.poolName = poolName;
50         this.sleepTime = sleepTime;
51         this.leakTimeOut = leakTimeOut;
52         keepGoing = true;
53         this.pool = pool;
54         debug = new Debugger("ConnectionLeakPollThread-" + poolName ,
55                              true );
56
57     }
58
59     /**
60      * This methods notifies the registered listeners.
61      * The logic seem to contain redundent iterations, will optimize
62      * it later.
63      * @param sConn Connection for which to notify the listener.
64      */

65
66     private void notifyAll(SmartConnection sConn) {
67
68         for (int i=0 ; i<connectionListenerList.size() ; i++ ) {
69             //debug.print("Found Leak notifying");
70
ConnectionLeakEvent cle = new ConnectionLeakEventImpl (
71                     sConn , sConn.getOwner() , sConn.getLastAccessedTime() ,
72                     sConn.getConnectionObtainedTime() , poolName);
73             //debug.print("Found Leak notifying" + cle);
74
ConnectionLeakListener c =
75                 (ConnectionLeakListener)connectionListenerList.get(i);
76             c.connectionTimeOut(cle);
77         }
78
79     }
80
81     // invokes the pool to release excessive connections
82
private void checkAndRelease() {
83
84         pool.releaseConnections();
85
86     }
87
88     // checks and notifies the listeners
89
private void checkAndNotify() {
90
91         for (int i=0 ; i<connectionsInUse.size() ; i++ ) {
92             SmartConnection sConn = (SmartConnection)connectionsInUse.get(i);
93             // notify all the listeners
94
if (System.currentTimeMillis() - sConn.getConnectionObtainedTime()
95                          >= leakTimeOut)
96                     notifyAll(sConn);
97             // checks and accordingly returns connections back to the
98
// pool, one which have been idle for more than specified
99
// time
100
if (System.currentTimeMillis() - sConn.getLastAccessedTime() > ((PoolMonitor)pool).getConfigMonitor().getMaxConnectionIdleTime()) {
101                 debug.print("Found a Idle Connection ...");
102                 sConn.forcedClose();
103             }
104         }
105
106     }
107
108     // Poor method, once starts running never stops untill killed,
109
// will implement stoping of polling thread at runtime in later versions.
110
public void run() {
111         debug.print("Starting Thread for detecting connection leaks");
112         while (keepGoing) {
113             debug.print("Polling .............");
114             checkAndNotify();
115             checkAndRelease();
116             try {
117                 Thread.sleep(sleepTime);
118             }
119             catch (Exception JavaDoc e) {
120             }
121         }
122
123     }
124
125     // To stop the thread, FORWARD COMPATIBILITY YOU SEE.
126
private void stop() {
127
128         keepGoing = false;
129
130     }
131
132 }
133
Popular Tags