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.ObjectPool; 21 import org.apache.commons.pool.ObjectPoolFactory; 22 import org.apache.commons.pool.PoolableObjectFactory; 23 24 /** 25 * A factory for creating {@link GenericObjectPool} instances. 26 * 27 * @see GenericObjectPool 28 * @see ObjectPoolFactory 29 * 30 * @author Rodney Waldhoff 31 * @version $Revision: 777748 $ $Date: 2009-05-22 20:00:44 -0400 (Fri, 22 May 2009) $ 32 * @since Pool 1.0 33 */ 34 public class GenericObjectPoolFactory implements ObjectPoolFactory { 35 /** 36 * Create a new GenericObjectPoolFactory. 37 * 38 * @param factory the PoolableObjectFactory used by created pools. 39 * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory) 40 */ 41 public GenericObjectPoolFactory(PoolableObjectFactory factory) { 42 this(factory,GenericObjectPool.DEFAULT_MAX_ACTIVE,GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,GenericObjectPool.DEFAULT_MAX_WAIT,GenericObjectPool.DEFAULT_MAX_IDLE,GenericObjectPool.DEFAULT_MIN_IDLE,GenericObjectPool.DEFAULT_TEST_ON_BORROW,GenericObjectPool.DEFAULT_TEST_ON_RETURN,GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericObjectPool.DEFAULT_TEST_WHILE_IDLE); 43 } 44 45 /** 46 * Create a new GenericObjectPoolFactory. 47 * 48 * @param factory the PoolableObjectFactory used by created pools. 49 * @param config a non-<code>null</code> GenericObjectPool.Config describing the configuration. 50 * @throws NullPointerException when config is <code>null</code>. 51 * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, GenericObjectPool.Config) 52 */ 53 public GenericObjectPoolFactory(PoolableObjectFactory factory, GenericObjectPool.Config config) throws NullPointerException { 54 this(factory,config.maxActive,config.whenExhaustedAction,config.maxWait,config.maxIdle,config.minIdle,config.testOnBorrow,config.testOnReturn,config.timeBetweenEvictionRunsMillis,config.numTestsPerEvictionRun,config.minEvictableIdleTimeMillis,config.testWhileIdle,config.softMinEvictableIdleTimeMillis, config.lifo); 55 } 56 57 /** 58 * Create a new GenericObjectPoolFactory. 59 * 60 * @param factory the PoolableObjectFactory used by created pools. 61 * @param maxActive maximum number of objects that can be borrowed from created pools at one time. 62 * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int) 63 */ 64 public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive) { 65 this(factory,maxActive,GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,GenericObjectPool.DEFAULT_MAX_WAIT,GenericObjectPool.DEFAULT_MAX_IDLE,GenericObjectPool.DEFAULT_MIN_IDLE,GenericObjectPool.DEFAULT_TEST_ON_BORROW,GenericObjectPool.DEFAULT_TEST_ON_RETURN,GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericObjectPool.DEFAULT_TEST_WHILE_IDLE); 66 } 67 68 /** 69 * Create a new GenericObjectPoolFactory. 70 * 71 * @param factory the PoolableObjectFactory used by created pools. 72 * @param maxActive maximum number of objects that can be borrowed from created pools at one time. 73 * @param whenExhaustedAction the action to take when the pool is exhausted. 74 * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted. 75 * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long) 76 */ 77 public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait) { 78 this(factory,maxActive,whenExhaustedAction,maxWait,GenericObjectPool.DEFAULT_MAX_IDLE,GenericObjectPool.DEFAULT_MIN_IDLE,GenericObjectPool.DEFAULT_TEST_ON_BORROW,GenericObjectPool.DEFAULT_TEST_ON_RETURN,GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericObjectPool.DEFAULT_TEST_WHILE_IDLE); 79 } 80 81 /** 82 * Create a new GenericObjectPoolFactory. 83 * 84 * @param factory the PoolableObjectFactory used by created pools. 85 * @param maxActive maximum number of objects that can be borrowed from created pools at one time. 86 * @param whenExhaustedAction the action to take when the pool is exhausted. 87 * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted. 88 * @param testOnBorrow whether to validate objects before they are returned by the borrowObject. 89 * @param testOnReturn whether to validate objects after they are returned to the returnObject. 90 * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, boolean, boolean) 91 */ 92 public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, boolean testOnBorrow, boolean testOnReturn) { 93 this(factory,maxActive,whenExhaustedAction,maxWait,GenericObjectPool.DEFAULT_MAX_IDLE,GenericObjectPool.DEFAULT_MIN_IDLE,testOnBorrow,testOnReturn,GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericObjectPool.DEFAULT_TEST_WHILE_IDLE); 94 } 95 96 /** 97 * Create a new GenericObjectPoolFactory. 98 * 99 * @param factory the PoolableObjectFactory used by created pools. 100 * @param maxActive maximum number of objects that can be borrowed from created pools at one time. 101 * @param whenExhaustedAction the action to take when the pool is exhausted. 102 * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted. 103 * @param maxIdle the maximum number of idle objects in my pool. 104 * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, int) 105 */ 106 public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle) { 107 this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle,GenericObjectPool.DEFAULT_MIN_IDLE,GenericObjectPool.DEFAULT_TEST_ON_BORROW,GenericObjectPool.DEFAULT_TEST_ON_RETURN,GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericObjectPool.DEFAULT_TEST_WHILE_IDLE); 108 } 109 110 /** 111 * Create a new GenericObjectPoolFactory. 112 * 113 * @param factory the PoolableObjectFactory used by created pools. 114 * @param maxActive maximum number of objects that can be borrowed from created pools at one time. 115 * @param whenExhaustedAction the action to take when the pool is exhausted. 116 * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted. 117 * @param maxIdle the maximum number of idle objects in my pool. 118 * @param testOnBorrow whether to validate objects before they are returned by the borrowObject. 119 * @param testOnReturn whether to validate objects after they are returned to the returnObject. 120 * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, int, boolean, boolean) 121 */ 122 public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn) { 123 this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle,GenericObjectPool.DEFAULT_MIN_IDLE,testOnBorrow,testOnReturn,GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,GenericObjectPool.DEFAULT_TEST_WHILE_IDLE); 124 } 125 126 /** 127 * Create a new GenericObjectPoolFactory. 128 * 129 * @param factory the PoolableObjectFactory used by created pools. 130 * @param maxActive maximum number of objects that can be borrowed from created pools at one time. 131 * @param whenExhaustedAction the action to take when the pool is exhausted. 132 * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted. 133 * @param maxIdle the maximum number of idle objects in my pool. 134 * @param testOnBorrow whether to validate objects before they are returned by the borrowObject. 135 * @param testOnReturn whether to validate objects after they are returned to the returnObject. 136 * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction. 137 * @param numTestsPerEvictionRun the number of idle objects to examine per run within the idle object eviction thread. 138 * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction. 139 * @param testWhileIdle whether or not to validate objects in the idle object eviction thread. 140 * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, int, boolean, boolean, long, int, long, boolean) 141 */ 142 public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) { 143 this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle,GenericObjectPool.DEFAULT_MIN_IDLE,testOnBorrow,testOnReturn,timeBetweenEvictionRunsMillis,numTestsPerEvictionRun,minEvictableIdleTimeMillis,testWhileIdle, GenericObjectPool.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS); 144 } 145 146 /** 147 * Create a new GenericObjectPoolFactory. 148 * 149 * @param factory the PoolableObjectFactory used by created pools. 150 * @param maxActive maximum number of objects that can be borrowed from created pools at one time. 151 * @param whenExhaustedAction the action to take when the pool is exhausted. 152 * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted. 153 * @param maxIdle the maximum number of idle objects in my pool. 154 * @param minIdle the minimum number of idle objects in my pool. 155 * @param testOnBorrow whether to validate objects before they are returned by the borrowObject. 156 * @param testOnReturn whether to validate objects after they are returned to the returnObject. 157 * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction. 158 * @param numTestsPerEvictionRun the number of idle objects to examine per run within the idle object eviction thread. 159 * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction. 160 * @param testWhileIdle whether or not to validate objects in the idle object eviction thread. 161 * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, int, int, boolean, boolean, long, int, long, boolean) 162 */ 163 public GenericObjectPoolFactory(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) { 164 this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle,minIdle,testOnBorrow,testOnReturn,timeBetweenEvictionRunsMillis,numTestsPerEvictionRun,minEvictableIdleTimeMillis,testWhileIdle, GenericObjectPool.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS); 165 } 166 167 /** 168 * Create a new GenericObjectPoolFactory. 169 * 170 * @param factory the PoolableObjectFactory used by created pools. 171 * @param maxActive maximum number of objects that can be borrowed from created pools at one time. 172 * @param whenExhaustedAction the action to take when the pool is exhausted. 173 * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted. 174 * @param maxIdle the maximum number of idle objects in my pool. 175 * @param minIdle the minimum number of idle objects in my pool. 176 * @param testOnBorrow whether to validate objects before they are returned by the borrowObject. 177 * @param testOnReturn whether to validate objects after they are returned to the returnObject. 178 * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction. 179 * @param numTestsPerEvictionRun the number of idle objects to examine per run within the idle object eviction thread. 180 * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction. 181 * @param testWhileIdle whether or not to validate objects in the idle object eviction thread. 182 * @param softMinEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction with the extra condition that at least "minIdle" amount of object remain in the pool. 183 * @since Pool 1.3 184 * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, int, int, boolean, boolean, long, int, long, boolean, long) 185 */ 186 public GenericObjectPoolFactory(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) { 187 this(factory,maxActive,whenExhaustedAction,maxWait,maxIdle,minIdle,testOnBorrow,testOnReturn,timeBetweenEvictionRunsMillis,numTestsPerEvictionRun,minEvictableIdleTimeMillis,testWhileIdle,softMinEvictableIdleTimeMillis, GenericObjectPool.DEFAULT_LIFO); 188 } 189 190 /** 191 * Create a new GenericObjectPoolFactory. 192 * 193 * @param factory the PoolableObjectFactory used by created pools. 194 * @param maxActive maximum number of objects that can be borrowed from created pools at one time. 195 * @param whenExhaustedAction the action to take when the pool is exhausted. 196 * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted. 197 * @param maxIdle the maximum number of idle objects in my pool. 198 * @param minIdle the minimum number of idle objects in my pool. 199 * @param testOnBorrow whether to validate objects before they are returned by the borrowObject. 200 * @param testOnReturn whether to validate objects after they are returned to the returnObject. 201 * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction. 202 * @param numTestsPerEvictionRun the number of idle objects to examine per run within the idle object eviction thread. 203 * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction. 204 * @param testWhileIdle whether or not to validate objects in the idle object eviction thread. 205 * @param softMinEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction with the extra condition that at least "minIdle" amount of object remain in the pool. 206 * @param lifo whether or not objects are returned in last-in-first-out order from the idle object pool. 207 * @since Pool 1.4 208 * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, int, int, boolean, boolean, long, int, long, boolean, long, boolean) 209 */ 210 public GenericObjectPoolFactory(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, boolean lifo) { 211 _maxIdle = maxIdle; 212 _minIdle = minIdle; 213 _maxActive = maxActive; 214 _maxWait = maxWait; 215 _whenExhaustedAction = whenExhaustedAction; 216 _testOnBorrow = testOnBorrow; 217 _testOnReturn = testOnReturn; 218 _testWhileIdle = testWhileIdle; 219 _timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; 220 _numTestsPerEvictionRun = numTestsPerEvictionRun; 221 _minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; 222 _softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis; 223 _lifo = lifo; 224 _factory = factory; 225 } 226 227 public ObjectPool createPool() { 228 return new GenericObjectPool(_factory,_maxActive,_whenExhaustedAction,_maxWait,_maxIdle,_minIdle,_testOnBorrow,_testOnReturn,_timeBetweenEvictionRunsMillis,_numTestsPerEvictionRun,_minEvictableIdleTimeMillis,_testWhileIdle,_softMinEvictableIdleTimeMillis,_lifo); 229 } 230 231 protected int _maxIdle = GenericObjectPool.DEFAULT_MAX_IDLE; 232 protected int _minIdle = GenericObjectPool.DEFAULT_MIN_IDLE; 233 protected int _maxActive = GenericObjectPool.DEFAULT_MAX_ACTIVE; 234 protected long _maxWait = GenericObjectPool.DEFAULT_MAX_WAIT; 235 protected byte _whenExhaustedAction = GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION; 236 protected boolean _testOnBorrow = GenericObjectPool.DEFAULT_TEST_ON_BORROW; 237 protected boolean _testOnReturn = GenericObjectPool.DEFAULT_TEST_ON_RETURN; 238 protected boolean _testWhileIdle = GenericObjectPool.DEFAULT_TEST_WHILE_IDLE; 239 protected long _timeBetweenEvictionRunsMillis = GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS; 240 protected int _numTestsPerEvictionRun = GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN; 241 protected long _minEvictableIdleTimeMillis = GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS; 242 protected long _softMinEvictableIdleTimeMillis = GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS; 243 protected boolean _lifo = GenericObjectPool.DEFAULT_LIFO; 244 protected PoolableObjectFactory _factory = null; 245 246 247 }