KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > util > pool > thread > ClientPool


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.util.pool.thread;
16
17 import java.util.*;
18 import org.quickserver.util.pool.*;
19 import org.apache.commons.pool.*;
20 import org.apache.commons.pool.impl.*;
21 import org.quickserver.net.server.*;
22 import org.quickserver.util.xmlreader.PoolConfig;
23 import java.util.logging.*;
24
25 /**
26  * This is a class for managing the pool of threads for
27  * handling clients.
28  * @author Akshathkumar Shetty
29  * @since 1.3
30  */

31 public class ClientPool {
32     private static Logger logger = Logger.getLogger(ClientPool.class.getName());
33
34     protected List clients = new ArrayList(3);
35     protected ObjectPool pool;
36     protected PoolConfig poolConfig;
37     private int countNioWriteThreads; //v1.4.6
38
private int maxThreadsForNioWrite = 10;
39             
40     public ClientPool(QSObjectPool objectPool, PoolConfig poolConfig) {
41         this.poolConfig = poolConfig;
42         pool = objectPool;
43     }
44
45     public ObjectPool getObjectPool() {
46         return pool;
47     }
48
49     public void addClient(Runnable JavaDoc r) throws NoSuchElementException {
50         addClient(r, false);
51     }
52
53     public synchronized void addClient(Runnable JavaDoc r, boolean keepObjOnFail)
54             throws NoSuchElementException {
55         //logger.finest("Adding Runnable: "+r);
56
clients.add(r);
57         ClientThread ct = null;
58         try {
59             ct = (ClientThread)pool.borrowObject();
60             
61             if(ct.isReady()==false) {
62                 //ct.start();
63
wait(500); //timeout was just in case :-)
64
//Thread.yield();
65
} else {
66                 synchronized(ct) {
67                     ct.notify();
68                 }
69             }
70         } catch(NoSuchElementException e) {
71             logger.info("No free threads: "+e);
72             if(keepObjOnFail==false)
73                 clients.remove(r);
74             throw e;
75         } catch(Exception JavaDoc e) {
76             logger.warning("Error in addClient: "+e+", Closing client: "+(ClientHandler)r);
77             try {
78                 ((ClientHandler)r).forceClose();
79             } catch(Exception JavaDoc er) {
80                 logger.warning("Error closing client: "+er);
81             }
82             try {
83                 if(ct!=null) pool.returnObject(ct);
84             } catch(Exception JavaDoc er) {
85                 logger.warning("Error in returning thread: "+er);
86             }
87         }
88     }
89
90     public synchronized void returnObject(Object JavaDoc object) {
91         try {
92             pool.returnObject(object);
93         } catch(Exception JavaDoc e) {
94             logger.warning("IGONRED: Error while returning object : "+e);
95             ((Thread JavaDoc)object).interrupt();
96         }
97     }
98
99     public synchronized Runnable JavaDoc getClient() {
100         if(clients.size()==0) {
101             return null;
102         }
103         return (Runnable JavaDoc) clients.remove(0);
104     }
105
106     /**
107      * @since 1.4.5
108      */

109     public boolean isClientAvailable() {
110         if(clients.size()==0) {
111             return false;
112         } else {
113             return true;
114         }
115     }
116
117     protected void finalize() throws Throwable JavaDoc {
118         try {
119             close();
120         } catch(Exception JavaDoc e) {
121             logger.warning("IGONRED:finalize in pool close : "+e);
122         }
123         super.finalize();
124     }
125
126     public void close() throws Exception JavaDoc {
127         pool.close();
128     }
129
130     public void clear() throws Exception JavaDoc {
131         pool.clear();
132     }
133
134     /**
135      * Return the number of instances currently borrowed from my pool.
136      * @since 1.4.1
137      */

138     public int getNumActive() {
139         return pool.getNumActive();
140     }
141
142     /**
143      * Return the number of instances currently idle in my pool.
144      * @since 1.4.1
145      */

146     public int getNumIdle() {
147         return pool.getNumIdle();
148     }
149
150     /**
151      * Returns iterator containing all the active
152      * threads i.e ClientHandler handling connected clients.
153      * @since 1.3.1
154      */

155     public final Iterator getAllClientThread() {
156         return ((QSObjectPool)pool).getAllActiveObjects();
157     }
158
159     public Object JavaDoc getObjectToSynchronize() {
160         return ((QSObjectPool)pool).getObjectToSynchronize();
161     }
162
163     /**
164      * Returns PoolConfig object that configured this pool
165      * @since 1.4.5
166      */

167     public PoolConfig getPoolConfig() {
168         return poolConfig;
169     }
170
171     /**
172      * Sets the maximum threads allowed for nio write. If set to 0 or less no limit is
173      * imposed.
174      * @since 1.4.6
175      */

176     public void setMaxThreadsForNioWrite(int count) {
177         this.maxThreadsForNioWrite = count;
178     }
179
180     /**
181      * Returns the maximum threads allowed for nio write
182      * @since 1.4.6
183      */

184     public int getMaxThreadsForNioWrite() {
185         return maxThreadsForNioWrite;
186     }
187
188     /**
189      * Notifies when NIO write is complete.
190      * @since 1.4.6
191      */

192     protected void nioWriteEnd() {
193         countNioWriteThreads--;
194         if(countNioWriteThreads<0) {
195             logger.warning("countNioWriteThreads should not go less than 0");
196             countNioWriteThreads = 0;
197         }
198     }
199
200     /**
201      * Notifies when NIO write is about to start.
202      * @since 1.4.6
203      */

204     protected void nioWriteStart() {
205         countNioWriteThreads++;
206     }
207
208     /**
209      * Method to suggest if nio write should be sent for processing.
210      * @since 1.4.6
211      */

212     public boolean shouldNioWriteHappen() {
213         if(maxThreadsForNioWrite <= 0 ||
214                 countNioWriteThreads < maxThreadsForNioWrite) {
215             return true;
216         } else {
217             return false;
218         }
219     }
220 }
221
Popular Tags