1 9 10 package org.apache.commons.pool.impl; 11 12 import java.util.Collections ; 13 import java.util.LinkedList ; 14 import java.util.List ; 15 import java.util.NoSuchElementException ; 16 17 import org.apache.commons.pool.PoolableObjectFactory; 18 import org.apache.commons.pool.impl.GenericKeyedObjectPool.ObjectTimestampPair; 19 20 30 @SuppressWarnings ("unchecked") 31 public class FairGenericObjectPool extends GenericObjectPool { 32 33 37 40 public FairGenericObjectPool() { 41 this(null,DEFAULT_MAX_ACTIVE,DEFAULT_WHEN_EXHAUSTED_ACTION,DEFAULT_MAX_WAIT,DEFAULT_MAX_IDLE,DEFAULT_MIN_IDLE,DEFAULT_TEST_ON_BORROW,DEFAULT_TEST_ON_RETURN,DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,DEFAULT_NUM_TESTS_PER_EVICTION_RUN,DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,DEFAULT_TEST_WHILE_IDLE); 42 } 43 44 48 public FairGenericObjectPool(PoolableObjectFactory factory) { 49 this(factory,DEFAULT_MAX_ACTIVE,DEFAULT_WHEN_EXHAUSTED_ACTION,DEFAULT_MAX_WAIT,DEFAULT_MAX_IDLE,DEFAULT_MIN_IDLE,DEFAULT_TEST_ON_BORROW,DEFAULT_TEST_ON_RETURN,DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,DEFAULT_NUM_TESTS_PER_EVICTION_RUN,DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,DEFAULT_TEST_WHILE_IDLE); 50 } 51 52 57 public FairGenericObjectPool(PoolableObjectFactory factory, GenericObjectPool.Config config) { 58 this(factory,config.maxActive,config.whenExhaustedAction,config.maxWait,config.maxIdle,config.minIdle,config.testOnBorrow,config.testOnReturn,config.timeBetweenEvictionRunsMillis,config.numTestsPerEvictionRun,config.minEvictableIdleTimeMillis,config.testWhileIdle); 59 } 60 61 66 public FairGenericObjectPool(PoolableObjectFactory factory, int maxActive) { 67 this(factory,maxActive,DEFAULT_WHEN_EXHAUSTED_ACTION,DEFAULT_MAX_WAIT,DEFAULT_MAX_IDLE,DEFAULT_MIN_IDLE,DEFAULT_TEST_ON_BORROW,DEFAULT_TEST_ON_RETURN,DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,DEFAULT_NUM_TESTS_PER_EVICTION_RUN,DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,DEFAULT_TEST_WHILE_IDLE); 68 } 69 70 77 public FairGenericObjectPool(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait) { 78 this(factory,maxActive,whenExhaustedAction,maxWait,DEFAULT_MAX_IDLE,DEFAULT_MIN_IDLE,DEFAULT_TEST_ON_BORROW,DEFAULT_TEST_ON_RETURN,DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,DEFAULT_NUM_TESTS_PER_EVICTION_RUN,DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,DEFAULT_TEST_WHILE_IDLE); 79 } 80 81 90 public FairGenericObjectPool(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, boolean testOnBorrow, boolean testOnReturn) { 91 this(factory,maxActive,whenExhaustedAction,maxWait,DEFAULT_MAX_IDLE,DEFAULT_MIN_IDLE,testOnBorrow,testOnReturn,DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,DEFAULT_NUM_TESTS_PER_EVICTION_RUN,DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,DEFAULT_TEST_WHILE_IDLE); 92 } 93 94 102 public FairGenericObjectPool(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle) { 103 this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle,DEFAULT_MIN_IDLE,DEFAULT_TEST_ON_BORROW,DEFAULT_TEST_ON_RETURN,DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,DEFAULT_NUM_TESTS_PER_EVICTION_RUN,DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,DEFAULT_TEST_WHILE_IDLE); 104 } 105 106 116 public FairGenericObjectPool(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn) { 117 this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle,DEFAULT_MIN_IDLE,testOnBorrow,testOnReturn,DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,DEFAULT_NUM_TESTS_PER_EVICTION_RUN,DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,DEFAULT_TEST_WHILE_IDLE); 118 } 119 120 134 public FairGenericObjectPool(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) { 135 this(factory, maxActive, whenExhaustedAction, maxWait, maxIdle, DEFAULT_MIN_IDLE, testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle); 136 } 137 138 153 public FairGenericObjectPool(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) { 154 this(factory, maxActive, whenExhaustedAction, maxWait, maxIdle, minIdle, testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle, DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS); 155 } 156 157 173 public FairGenericObjectPool(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle, long softMinEvictableIdleTimeMillis) { 174 super(factory, maxActive, whenExhaustedAction, maxWait, maxIdle, 175 minIdle, testOnBorrow, testOnReturn, 176 timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, 177 minEvictableIdleTimeMillis, testWhileIdle, 178 softMinEvictableIdleTimeMillis); 179 _borrowerQueue = Collections.synchronizedList(new LinkedList ()); 180 } 181 182 184 188 public Object borrowObject() throws Exception { 189 assertOpen(); 190 long starttime = System.currentTimeMillis(); 191 192 193 194 try { 195 synchronized(this) { 196 _borrowerQueue.add(Thread.currentThread()); 198 199 for(;;) { 200 ObjectTimestampPair pair = null; 201 202 boolean eligible = _borrowerQueue.get(0)==Thread.currentThread(); 205 if(eligible) { 206 try { 208 pair = (ObjectTimestampPair)(_pool.removeFirst()); 209 } catch(NoSuchElementException e) { 210 ; 211 } 212 } 213 214 if(null == pair) { 216 if(eligible && (_maxActive < 0 || _numActive < _maxActive)) { 219 } else { 221 switch(_whenExhaustedAction) { 224 case WHEN_EXHAUSTED_GROW: 225 break; 227 case WHEN_EXHAUSTED_FAIL: 228 throw new NoSuchElementException ("Pool exhausted"); 229 case WHEN_EXHAUSTED_BLOCK: 230 try { 231 if(_maxWait <= 0) { 232 wait(); 233 } else { 234 final long elapsed = (System.currentTimeMillis() - starttime); 237 final long waitTime = _maxWait - elapsed; 238 if (waitTime > 0) 239 { 240 wait(waitTime); 241 } 242 } 243 } catch(InterruptedException e) { 244 } 246 if(_maxWait > 0 && ((System.currentTimeMillis() - starttime) >= _maxWait)) { 247 throw new NoSuchElementException ("Timeout waiting for idle object"); 248 } else { 249 continue; } 251 default: 252 throw new IllegalArgumentException ("WhenExhaustedAction property " + _whenExhaustedAction + " not recognized."); 253 } 254 } 255 } 256 _numActive++; 257 258 boolean newlyCreated = false; 260 if(null == pair) { 261 try { 262 Object obj = _factory.makeObject(); 263 pair = new ObjectTimestampPair(obj); 264 newlyCreated = true; 265 return pair.value; 266 } finally { 267 if (!newlyCreated) { 268 _numActive--; 270 notifyAll(); 271 } 272 } 273 } 274 275 try { 277 _factory.activateObject(pair.value); 278 if(_testOnBorrow && !_factory.validateObject(pair.value)) { 279 throw new Exception ("ValidateObject failed"); 280 } 281 return pair.value; 282 } 283 catch (Throwable e) { 284 _numActive--; 286 notifyAll(); 287 try { 288 _factory.destroyObject(pair.value); 289 } 290 catch (Throwable e2) { 291 } 293 if(newlyCreated) { 294 throw new NoSuchElementException ("Could not create a validated object, cause: " + e.getMessage()); 295 } 296 else { 297 continue; } 299 } 300 } 301 } 302 } finally { 303 _borrowerQueue.remove(Thread.currentThread()); 305 } 306 } 307 308 309 protected List _borrowerQueue; 310 } 311 | Popular Tags |