1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.pool.impl; 19 20 import org.apache.commons.pool.KeyedObjectPool; 21 import org.apache.commons.pool.KeyedObjectPoolFactory; 22 import org.apache.commons.pool.KeyedPoolableObjectFactory; 23 24 /** 25 * A factory for creating {@link GenericKeyedObjectPool} instances. 26 * 27 * @see GenericKeyedObjectPool 28 * @see KeyedObjectPoolFactory 29 * 30 * @author Rodney Waldhoff 31 * @author Dirk Verbeeck 32 * @version $Revision: 777748 $ $Date: 2009-05-22 20:00:44 -0400 (Fri, 22 May 2009) $ 33 * @since Pool 1.0 34 */ 35 public class GenericKeyedObjectPoolFactory implements KeyedObjectPoolFactory { 36 /** 37 * Create a new GenericKeyedObjectPoolFactory. 38 * 39 * @param factory the KeyedPoolableObjectFactory to used by created pools. 40 * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory) 41 */ 42 public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory) { 43 this(factory,GenericKeyedObjectPool.DEFAULT_MAX_ACTIVE,GenericKeyedObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,GenericKeyedObjectPool.DEFAULT_MAX_WAIT,GenericKeyedObjectPool.DEFAULT_MAX_IDLE,GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW,GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN,GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE); 44 } 45 46 /** 47 * Create a new GenericKeyedObjectPoolFactory. 48 * 49 * @param factory the KeyedPoolableObjectFactory to used by created pools. 50 * @param config a non-null GenericKeyedObjectPool.Config describing the configuration. 51 * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, GenericKeyedObjectPool.Config) 52 * @throws NullPointerException when config is <code>null</code>. 53 */ 54 public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, GenericKeyedObjectPool.Config config) throws NullPointerException { 55 this(factory,config.maxActive,config.whenExhaustedAction,config.maxWait,config.maxIdle,config.maxTotal,config.minIdle,config.testOnBorrow,config.testOnReturn,config.timeBetweenEvictionRunsMillis,config.numTestsPerEvictionRun,config.minEvictableIdleTimeMillis,config.testWhileIdle,config.lifo); 56 } 57 58 /** 59 * Create a new GenericKeyedObjectPoolFactory. 60 * 61 * @param factory the KeyedPoolableObjectFactory to used by created pools. 62 * @param maxActive the maximum number of objects that can be borrowed from pools at one time. 63 * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, int) 64 */ 65 public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive) { 66 this(factory,maxActive,GenericKeyedObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,GenericKeyedObjectPool.DEFAULT_MAX_WAIT,GenericKeyedObjectPool.DEFAULT_MAX_IDLE, GenericKeyedObjectPool.DEFAULT_MAX_TOTAL,GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW,GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN,GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE); 67 } 68 69 /** 70 * Create a new GenericKeyedObjectPoolFactory. 71 * 72 * @param factory the KeyedPoolableObjectFactory to used by created pools. 73 * @param maxActive the maximum number of objects that can be borrowed from pools at one time. 74 * @param whenExhaustedAction the action to take when the pool is exhausted. 75 * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted. 76 * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, int, byte, long) 77 */ 78 public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait) { 79 this(factory,maxActive,whenExhaustedAction,maxWait,GenericKeyedObjectPool.DEFAULT_MAX_IDLE, GenericKeyedObjectPool.DEFAULT_MAX_TOTAL,GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW,GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN,GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE); 80 } 81 82 /** 83 * Create a new GenericKeyedObjectPoolFactory. 84 * 85 * @param factory the KeyedPoolableObjectFactory to used by created pools. 86 * @param maxActive the maximum number of objects that can be borrowed from pools at one time. 87 * @param whenExhaustedAction the action to take when the pool is exhausted. 88 * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted. 89 * @param testOnBorrow whether to validate objects before they are returned by borrowObject. 90 * @param testOnReturn whether to validate objects after they are returned to returnObject. 91 * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, int, byte, long, boolean, boolean) 92 */ 93 public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, boolean testOnBorrow, boolean testOnReturn) { 94 this(factory,maxActive,whenExhaustedAction,maxWait,GenericKeyedObjectPool.DEFAULT_MAX_IDLE, GenericKeyedObjectPool.DEFAULT_MAX_TOTAL,testOnBorrow,testOnReturn,GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE); 95 } 96 97 /** 98 * Create a new GenericKeyedObjectPoolFactory. 99 * 100 * @param factory the KeyedPoolableObjectFactory to used by created pools. 101 * @param maxActive the maximum number of objects that can be borrowed from pools at one time. 102 * @param whenExhaustedAction the action to take when the pool is exhausted. 103 * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted. 104 * @param maxIdle the maximum number of idle objects in the pools. 105 * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, int, byte, long, int) 106 */ 107 public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle) { 108 this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle, GenericKeyedObjectPool.DEFAULT_MAX_TOTAL,GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW,GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN,GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE); 109 } 110 111 /** 112 * Create a new GenericKeyedObjectPoolFactory. 113 * 114 * @param factory the KeyedPoolableObjectFactory to used by created pools. 115 * @param maxActive the maximum number of objects that can be borrowed from pools at one time. 116 * @param whenExhaustedAction the action to take when the pool is exhausted. 117 * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted. 118 * @param maxIdle the maximum number of idle objects in the pools. 119 * @param maxTotal the maximum number of objects that can exists at one time. 120 */ 121 public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int maxTotal) { 122 this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle, maxTotal, GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW,GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN,GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE); 123 } 124 125 /** 126 * Create a new GenericKeyedObjectPoolFactory. 127 * 128 * @param factory the KeyedPoolableObjectFactory to used by created pools. 129 * @param maxActive the maximum number of objects that can be borrowed from pools at one time. 130 * @param whenExhaustedAction the action to take when the pool is exhausted. 131 * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted. 132 * @param maxIdle the maximum number of idle objects in the pools. 133 * @param testOnBorrow whether to validate objects before they are returned by borrowObject. 134 * @param testOnReturn whether to validate objects after they are returned to returnObject. 135 * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, int, byte, long, int, boolean, boolean) 136 */ 137 public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn) { 138 this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle, GenericKeyedObjectPool.DEFAULT_MAX_TOTAL,testOnBorrow,testOnReturn,GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE); 139 } 140 141 /** 142 * Create a new GenericKeyedObjectPoolFactory. 143 * 144 * @param factory the KeyedPoolableObjectFactory to used by created pools. 145 * @param maxActive the maximum number of objects that can be borrowed from pools at one time. 146 * @param whenExhaustedAction the action to take when the pool is exhausted. 147 * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted. 148 * @param maxIdle the maximum number of idle objects in the pools. 149 * @param testOnBorrow whether to validate objects before they are returned by borrowObject. 150 * @param testOnReturn whether to validate objects after they are returned to returnObject. 151 * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction. 152 * @param numTestsPerEvictionRun the number of idle objects to examine per run of the evictor. 153 * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction. 154 * @param testWhileIdle whether to validate objects in the idle object eviction thread. 155 * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, int, byte, long, int, boolean, boolean, long, int, long, boolean) 156 */ 157 public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) { 158 this(factory, maxActive, whenExhaustedAction, maxWait, maxIdle, GenericKeyedObjectPool.DEFAULT_MAX_TOTAL, testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle); 159 } 160 161 /** 162 * Create a new GenericKeyedObjectPoolFactory. 163 * 164 * @param factory the KeyedPoolableObjectFactory to used by created pools. 165 * @param maxActive the maximum number of objects that can be borrowed from pools at one time. 166 * @param whenExhaustedAction the action to take when the pool is exhausted. 167 * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted. 168 * @param maxIdle the maximum number of idle objects in the pools. 169 * @param maxTotal the maximum number of objects that can exists at one time. 170 * @param testOnBorrow whether to validate objects before they are returned by borrowObject. 171 * @param testOnReturn whether to validate objects after they are returned to returnObject. 172 * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction. 173 * @param numTestsPerEvictionRun the number of idle objects to examine per run of the evictor. 174 * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction. 175 * @param testWhileIdle whether to validate objects in the idle object eviction thread. 176 * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, int, byte, long, int, int, boolean, boolean, long, int, long, boolean) 177 */ 178 public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int maxTotal, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) { 179 this(factory, maxActive, whenExhaustedAction, maxWait, maxIdle, maxTotal, GenericKeyedObjectPool.DEFAULT_MIN_IDLE , testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle); 180 } 181 182 /** 183 * Create a new GenericKeyedObjectPoolFactory. 184 * 185 * @param factory the KeyedPoolableObjectFactory to used by created pools. 186 * @param maxActive the maximum number of objects that can be borrowed from pools at one time. 187 * @param whenExhaustedAction the action to take when the pool is exhausted. 188 * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted. 189 * @param maxIdle the maximum number of idle objects in the pools. 190 * @param maxTotal the maximum number of objects that can exists at one time. 191 * @param minIdle the minimum number of idle objects to have in the pool at any one time. 192 * @param testOnBorrow whether to validate objects before they are returned by borrowObject. 193 * @param testOnReturn whether to validate objects after they are returned to returnObject. 194 * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction. 195 * @param numTestsPerEvictionRun the number of idle objects to examine per run of the evictor. 196 * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction. 197 * @param testWhileIdle whether to validate objects in the idle object eviction thread. 198 * @since Pool 1.3 199 * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, int, byte, long, int, int, int, boolean, boolean, long, int, long, boolean) 200 */ 201 public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int maxTotal, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) { 202 this(factory, maxActive, whenExhaustedAction, maxWait, maxIdle, maxTotal, minIdle, testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle, GenericKeyedObjectPool.DEFAULT_LIFO); 203 } 204 205 /** 206 * Create a new GenericKeyedObjectPoolFactory. 207 * 208 * @param factory the KeyedPoolableObjectFactory to used by created pools. 209 * @param maxActive the maximum number of objects that can be borrowed from pools at one time. 210 * @param whenExhaustedAction the action to take when the pool is exhausted. 211 * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted. 212 * @param maxIdle the maximum number of idle objects in the pools. 213 * @param maxTotal the maximum number of objects that can exists at one time. 214 * @param minIdle the minimum number of idle objects to have in the pool at any one time. 215 * @param testOnBorrow whether to validate objects before they are returned by borrowObject. 216 * @param testOnReturn whether to validate objects after they are returned to returnObject. 217 * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction. 218 * @param numTestsPerEvictionRun the number of idle objects to examine per run of the evictor. 219 * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction. 220 * @param testWhileIdle whether to validate objects in the idle object eviction thread. 221 * @param lifo whether or not objects are returned in last-in-first-out order from the idle object pool. 222 * @since Pool 1.4 223 * @see GenericKeyedObjectPool#GenericKeyedObjectPool(KeyedPoolableObjectFactory, int, byte, long, int, int, int, boolean, boolean, long, int, long, boolean, boolean) 224 */ 225 public GenericKeyedObjectPoolFactory(KeyedPoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int maxTotal, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle, boolean lifo) { 226 _maxIdle = maxIdle; 227 _maxActive = maxActive; 228 _maxTotal = maxTotal; 229 _minIdle = minIdle; 230 _maxWait = maxWait; 231 _whenExhaustedAction = whenExhaustedAction; 232 _testOnBorrow = testOnBorrow; 233 _testOnReturn = testOnReturn; 234 _testWhileIdle = testWhileIdle; 235 _timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; 236 _numTestsPerEvictionRun = numTestsPerEvictionRun; 237 _minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; 238 _factory = factory; 239 _lifo = lifo; 240 } 241 242 public KeyedObjectPool createPool() { 243 return new GenericKeyedObjectPool(_factory,_maxActive,_whenExhaustedAction,_maxWait,_maxIdle,_maxTotal,_minIdle,_testOnBorrow,_testOnReturn,_timeBetweenEvictionRunsMillis,_numTestsPerEvictionRun,_minEvictableIdleTimeMillis,_testWhileIdle,_lifo); 244 } 245 246 //--- protected attributes --------------------------------------- 247 248 protected int _maxIdle = GenericKeyedObjectPool.DEFAULT_MAX_IDLE; 249 protected int _maxActive = GenericKeyedObjectPool.DEFAULT_MAX_ACTIVE; 250 protected int _maxTotal = GenericKeyedObjectPool.DEFAULT_MAX_TOTAL; 251 protected int _minIdle = GenericKeyedObjectPool.DEFAULT_MIN_IDLE; 252 protected long _maxWait = GenericKeyedObjectPool.DEFAULT_MAX_WAIT; 253 protected byte _whenExhaustedAction = GenericKeyedObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION; 254 protected boolean _testOnBorrow = GenericKeyedObjectPool.DEFAULT_TEST_ON_BORROW; 255 protected boolean _testOnReturn = GenericKeyedObjectPool.DEFAULT_TEST_ON_RETURN; 256 protected boolean _testWhileIdle = GenericKeyedObjectPool.DEFAULT_TEST_WHILE_IDLE; 257 protected long _timeBetweenEvictionRunsMillis = GenericKeyedObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS; 258 protected int _numTestsPerEvictionRun = GenericKeyedObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN; 259 protected long _minEvictableIdleTimeMillis = GenericKeyedObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS; 260 protected KeyedPoolableObjectFactory _factory = null; 261 protected boolean _lifo = GenericKeyedObjectPool.DEFAULT_LIFO; 262 263 }