1 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 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; 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 r) throws NoSuchElementException { 50 addClient(r, false); 51 } 52 53 public synchronized void addClient(Runnable r, boolean keepObjOnFail) 54 throws NoSuchElementException { 55 clients.add(r); 57 ClientThread ct = null; 58 try { 59 ct = (ClientThread)pool.borrowObject(); 60 61 if(ct.isReady()==false) { 62 wait(500); } 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 e) { 76 logger.warning("Error in addClient: "+e+", Closing client: "+(ClientHandler)r); 77 try { 78 ((ClientHandler)r).forceClose(); 79 } catch(Exception er) { 80 logger.warning("Error closing client: "+er); 81 } 82 try { 83 if(ct!=null) pool.returnObject(ct); 84 } catch(Exception er) { 85 logger.warning("Error in returning thread: "+er); 86 } 87 } 88 } 89 90 public synchronized void returnObject(Object object) { 91 try { 92 pool.returnObject(object); 93 } catch(Exception e) { 94 logger.warning("IGONRED: Error while returning object : "+e); 95 ((Thread )object).interrupt(); 96 } 97 } 98 99 public synchronized Runnable getClient() { 100 if(clients.size()==0) { 101 return null; 102 } 103 return (Runnable ) clients.remove(0); 104 } 105 106 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 { 118 try { 119 close(); 120 } catch(Exception e) { 121 logger.warning("IGONRED:finalize in pool close : "+e); 122 } 123 super.finalize(); 124 } 125 126 public void close() throws Exception { 127 pool.close(); 128 } 129 130 public void clear() throws Exception { 131 pool.clear(); 132 } 133 134 138 public int getNumActive() { 139 return pool.getNumActive(); 140 } 141 142 146 public int getNumIdle() { 147 return pool.getNumIdle(); 148 } 149 150 155 public final Iterator getAllClientThread() { 156 return ((QSObjectPool)pool).getAllActiveObjects(); 157 } 158 159 public Object getObjectToSynchronize() { 160 return ((QSObjectPool)pool).getObjectToSynchronize(); 161 } 162 163 167 public PoolConfig getPoolConfig() { 168 return poolConfig; 169 } 170 171 176 public void setMaxThreadsForNioWrite(int count) { 177 this.maxThreadsForNioWrite = count; 178 } 179 180 184 public int getMaxThreadsForNioWrite() { 185 return maxThreadsForNioWrite; 186 } 187 188 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 204 protected void nioWriteStart() { 205 countNioWriteThreads++; 206 } 207 208 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 |