Class GenericKeyedObjectPool<K,V>
- Type Parameters:
K- the type of keys in this poolV- the type of objects held in this pool
- All Implemented Interfaces:
KeyedObjectPool<K,V>
KeyedObjectPool implementation.
When coupled with the appropriate KeyedPoolableObjectFactory,
GenericKeyedObjectPool provides robust pooling functionality for
keyed objects. A GenericKeyedObjectPool can be viewed as a map
of pools, keyed on the (unique) key values provided to the
preparePool, addObject or
borrowObject methods. Each time a new key value is
provided to one of these methods, a new pool is created under the given key
to be managed by the containing GenericKeyedObjectPool.
A GenericKeyedObjectPool provides a number of configurable
parameters:
-
maxActivecontrols the maximum number of objects (per key) that can allocated by the pool (checked out to client threads, or idle in the pool) at one time. When non-positive, there is no limit to the number of objects per key. WhenmaxActiveis reached, the keyed pool is said to be exhausted. The default setting for this parameter is 8. -
maxTotalsets a global limit on the number of objects that can be in circulation (active or idle) within the combined set of pools. When non-positive, there is no limit to the total number of objects in circulation. WhenmaxTotalis exceeded, all keyed pools are exhausted. WhenmaxTotalis set to a positive value andborrowObjectis invoked when at the limit with no idle instances available, an attempt is made to create room by clearing the oldest 15% of the elements from the keyed pools. The default setting for this parameter is -1 (no limit). -
maxIdlecontrols the maximum number of objects that can sit idle in the pool (per key) at any time. When negative, there is no limit to the number of objects that may be idle per key. The default setting for this parameter is 8. -
whenExhaustedActionspecifies the behavior of theborrowObjectmethod when a keyed pool is exhausted:-
When
whenExhaustedActionisWHEN_EXHAUSTED_FAIL,borrowObjectwill throw aNoSuchElementException -
When
whenExhaustedActionisWHEN_EXHAUSTED_GROW,borrowObjectwill create a new object and return it (essentially makingmaxActivemeaningless.) -
When
whenExhaustedActionisWHEN_EXHAUSTED_BLOCK,borrowObjectwill block (invokewaituntil a new or idle object is available. If a positivemaxWaitvalue is supplied, theborrowObjectwill block for at most that many milliseconds, after which aNoSuchElementExceptionwill be thrown. IfmaxWaitis non-positive, theborrowObjectmethod will block indefinitely.
whenExhaustedActionsetting isWHEN_EXHAUSTED_BLOCK. -
When
-
When
testOnBorrowis set, the pool will attempt to validate each object before it is returned from theborrowObjectmethod. (Using the provided factory'svalidateObjectmethod.) Objects that fail to validate will be dropped from the pool, and a different object will be borrowed. The default setting for this parameter isfalse. -
When
testOnReturnis set, the pool will attempt to validate each object before it is returned to the pool in thereturnObjectmethod. (Using the provided factory'svalidateObjectmethod.) Objects that fail to validate will be dropped from the pool. The default setting for this parameter isfalse.
Optionally, one may configure the pool to examine and possibly evict objects as they sit idle in the pool and to ensure that a minimum number of idle objects is maintained for each key. This is performed by an "idle object eviction" thread, which runs asynchronously. Caution should be used when configuring this optional feature. Eviction runs contend with client threads for access to objects in the pool, so if they run too frequently performance issues may result. The idle object eviction thread may be configured using the following attributes:
-
timeBetweenEvictionRunsMillisindicates how long the eviction thread should sleep before "runs" of examining idle objects. When non-positive, no eviction thread will be launched. The default setting for this parameter is -1 (i.e., by default, idle object eviction is disabled). -
minEvictableIdleTimeMillisspecifies the minimum amount of time that an object may sit idle in the pool before it is eligible for eviction due to idle time. When non-positive, no object will be dropped from the pool due to idle time alone. This setting has no effect unlesstimeBetweenEvictionRunsMillis > 0.The default setting for this parameter is 30 minutes. -
testWhileIdleindicates whether or not idle objects should be validated using the factory'svalidateObjectmethod during idle object eviction runs. Objects that fail to validate will be dropped from the pool. This setting has no effect unlesstimeBetweenEvictionRunsMillis > 0.The default setting for this parameter isfalse. -
minIdlesets a target value for the minimum number of idle objects (per key) that should always be available. If this parameter is set to a positive number andtimeBetweenEvictionRunsMillis > 0,each time the idle object eviction thread runs, it will try to create enough idle instances so that there will beminIdleidle instances available under each key. This parameter is also used bypreparePooliftrueis provided as that method'spopulateImmediatelyparameter. The default setting for this parameter is 0.
The pools can be configured to behave as LIFO queues with respect to idle objects - always returning the most recently used object from the pool, or as FIFO queues, where borrowObject always returns the oldest object in the idle object pool.
-
Lifodetermines whether or not the pools return idle objects in last-in-first-out order. The default setting for this parameter istrue.
GenericKeyedObjectPool is not usable without a KeyedPoolableObjectFactory. A
non-null factory must be provided either as a constructor argument
or via a call to setFactory before the pool is used.
Implementation note: To prevent possible deadlocks, care has been taken to ensure that no call to a factory method will occur within a synchronization block. See POOL-125 and DBCP-44 for more information.
- Since:
- Pool 1.0
- Version:
- $Revision: 1222396 $ $Date: 2011-12-22 14:02:25 -0500 (Thu, 22 Dec 2011) $
- Author:
- Rodney Waldhoff, Dirk Verbeeck, Sandy McArthur
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA simple "struct" encapsulating the configuration information for aGenericKeyedObjectPool. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final booleanThe default LIFO status.static final intThe default cap on the total number of active instances (per key) from the pool.static final intThe default cap on the number of idle instances (per key) in the pool.static final intThe default cap on the the overall maximum number of objects that can exist at one time.static final longThe default maximum amount of time (in milliseconds) theborrowObject(K)method should block before throwing an exception when the pool is exhausted and the"when exhausted" actionisWHEN_EXHAUSTED_BLOCK.static final longThe default value forgetMinEvictableIdleTimeMillis().static final intThe default minimum level of idle objects in the pool.static final intThe default number of objects to examine per run in the idle object evictor.static final booleanThe default "test on borrow" value.static final booleanThe default "test on return" value.static final booleanThe default "test while idle" value.static final longThe default "time between eviction runs" value.static final byteThe default "when exhausted action" for the pool.static final byteA "when exhausted action" type indicating that when the pool is exhausted (i.e., the maximum number of active objects has been reached), theborrowObject(K)method should block until a new object is available, or themaximum wait timehas been reached.static final byteA "when exhausted action" type indicating that when the pool is exhausted (i.e., the maximum number of active objects has been reached), theborrowObject(K)method should fail, throwing aNoSuchElementException.static final byteA "when exhausted action" type indicating that when the pool is exhausted (i.e., the maximum number of active objects has been reached), theborrowObject(K)method should simply create a new object anyway. -
Constructor Summary
ConstructorsConstructorDescriptionCreate a newGenericKeyedObjectPoolwith no factory.GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory) Create a newGenericKeyedObjectPoolusing the specified values.GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory, int maxActive) Create a newGenericKeyedObjectPoolusing the specified values.GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory, int maxActive, byte whenExhaustedAction, long maxWait) Create a newGenericKeyedObjectPoolusing the specified values.GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory, int maxActive, byte whenExhaustedAction, long maxWait, boolean testOnBorrow, boolean testOnReturn) Create a newGenericKeyedObjectPoolusing the specified values.GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle) Create a newGenericKeyedObjectPoolusing the specified values.GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn) Create a newGenericKeyedObjectPoolusing the specified values.GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) Create a newGenericKeyedObjectPoolusing the specified values.GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int maxTotal, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) Create a newGenericKeyedObjectPoolusing the specified values.GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> 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) Create a newGenericKeyedObjectPoolusing the specified values.GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> 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) Create a newGenericKeyedObjectPoolusing the specified values.GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory, GenericKeyedObjectPool.Config config) Create a newGenericKeyedObjectPoolusing the specified values. -
Method Summary
Modifier and TypeMethodDescriptionvoidCreate an object using thefactory, passivate it, and then place it in the idle object pool.borrowObject(K key) Borrows an object from the keyed pool associated with the given key.voidclear()Clears any objects sitting idle in the pool by removing them from the idle instance pool and then invoking the configured PoolableObjectFactory'sKeyedPoolableObjectFactory.destroyObject(Object, Object)method on each idle instance.voidClears the specified pool, removing all pooled instances corresponding to the givenkey.voidClears oldest 15% of objects in pool.voidclose()Closes the keyed object pool.voidevict()PerformnumTestsidle object eviction tests, evicting examined objects that meet the criteria for eviction.booleangetLifo()Whether or not the idle object pools act as LIFO queues.intReturns the cap on the number of object instances allocated by the pool (checked out or idle), per key.intReturns the cap on the number of "idle" instances per key.intReturns the overall maximum number of objects (across pools) that can exist at one time.longReturns the maximum amount of time (in milliseconds) theborrowObject(K)method should block before throwing an exception when the pool is exhausted and the"when exhausted" actionisWHEN_EXHAUSTED_BLOCK.longReturns the minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any).intReturns the minimum number of idle objects to maintain in each of the keyed pools.intReturns the total number of instances current borrowed from this pool but not yet returned.intgetNumActive(Object key) Returns the number of instances currently borrowed from but not yet returned to the pool corresponding to the givenkey.intReturns the total number of instances currently idle in this pool.intgetNumIdle(Object key) Returns the number of instances corresponding to the givenkeycurrently idle in this pool.intReturns the max number of objects to examine during each run of the idle object evictor thread (if any).booleanbooleanWhentrue, objects will bevalidatedbefore being returned to the pool within thereturnObject(K, V).booleanWhentrue, objects will bevalidatedby the idle object evictor (if any).longReturns the number of milliseconds to sleep between runs of the idle object evictor thread.byteReturns the action to take when theborrowObject(K)method is invoked when the pool is exhausted (the maximum number of "active" objects has been reached).voidinvalidateObject(K key, V obj) Invalidates an object from the pool.voidpreparePool(K key, boolean populateImmediately) Registers a key for pool control.voidreturnObject(K key, V obj) Returns an object to a keyed pool.voidSets the configuration.voidsetLifo(boolean lifo) Sets the LIFO property of the pools.voidsetMaxActive(int maxActive) Sets the cap on the number of object instances managed by the pool per key.voidsetMaxIdle(int maxIdle) Sets the cap on the number of "idle" instances in the pool.voidsetMaxTotal(int maxTotal) Sets the cap on the total number of instances from all pools combined.voidsetMaxWait(long maxWait) Sets the maximum amount of time (in milliseconds) theborrowObject(K)method should block before throwing an exception when the pool is exhausted and the"when exhausted" actionisWHEN_EXHAUSTED_BLOCK.voidsetMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) Sets the minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any).voidsetMinIdle(int poolSize) Sets the minimum number of idle objects to maintain in each of the keyed pools.voidsetNumTestsPerEvictionRun(int numTestsPerEvictionRun) Sets the max number of objects to examine during each run of the idle object evictor thread (if any).voidsetTestOnBorrow(boolean testOnBorrow) voidsetTestOnReturn(boolean testOnReturn) Whentrue, objects will bevalidatedbefore being returned to the pool within thereturnObject(K, V).voidsetTestWhileIdle(boolean testWhileIdle) Whentrue, objects will bevalidatedby the idle object evictor (if any).voidsetTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) Sets the number of milliseconds to sleep between runs of the idle object evictor thread.voidsetWhenExhaustedAction(byte whenExhaustedAction) Sets the action to take when theborrowObject(K)method is invoked when the pool is exhausted (the maximum number of "active" objects has been reached).
-
Field Details
-
WHEN_EXHAUSTED_FAIL
public static final byte WHEN_EXHAUSTED_FAILA "when exhausted action" type indicating that when the pool is exhausted (i.e., the maximum number of active objects has been reached), theborrowObject(K)method should fail, throwing aNoSuchElementException. -
WHEN_EXHAUSTED_BLOCK
public static final byte WHEN_EXHAUSTED_BLOCKA "when exhausted action" type indicating that when the pool is exhausted (i.e., the maximum number of active objects has been reached), theborrowObject(K)method should block until a new object is available, or themaximum wait timehas been reached. -
WHEN_EXHAUSTED_GROW
public static final byte WHEN_EXHAUSTED_GROWA "when exhausted action" type indicating that when the pool is exhausted (i.e., the maximum number of active objects has been reached), theborrowObject(K)method should simply create a new object anyway. -
DEFAULT_MAX_IDLE
public static final int DEFAULT_MAX_IDLEThe default cap on the number of idle instances (per key) in the pool.- See Also:
-
DEFAULT_MAX_ACTIVE
public static final int DEFAULT_MAX_ACTIVEThe default cap on the total number of active instances (per key) from the pool. -
DEFAULT_MAX_TOTAL
public static final int DEFAULT_MAX_TOTALThe default cap on the the overall maximum number of objects that can exist at one time. -
DEFAULT_WHEN_EXHAUSTED_ACTION
public static final byte DEFAULT_WHEN_EXHAUSTED_ACTIONThe default "when exhausted action" for the pool. -
DEFAULT_MAX_WAIT
public static final long DEFAULT_MAX_WAITThe default maximum amount of time (in milliseconds) theborrowObject(K)method should block before throwing an exception when the pool is exhausted and the"when exhausted" actionisWHEN_EXHAUSTED_BLOCK. -
DEFAULT_TEST_ON_BORROW
public static final boolean DEFAULT_TEST_ON_BORROWThe default "test on borrow" value. -
DEFAULT_TEST_ON_RETURN
public static final boolean DEFAULT_TEST_ON_RETURNThe default "test on return" value. -
DEFAULT_TEST_WHILE_IDLE
public static final boolean DEFAULT_TEST_WHILE_IDLEThe default "test while idle" value. -
DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS
public static final long DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLISThe default "time between eviction runs" value. -
DEFAULT_NUM_TESTS_PER_EVICTION_RUN
public static final int DEFAULT_NUM_TESTS_PER_EVICTION_RUNThe default number of objects to examine per run in the idle object evictor. -
DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS
public static final long DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLISThe default value forgetMinEvictableIdleTimeMillis(). -
DEFAULT_MIN_IDLE
public static final int DEFAULT_MIN_IDLEThe default minimum level of idle objects in the pool.- Since:
- Pool 1.3
- See Also:
-
DEFAULT_LIFO
public static final boolean DEFAULT_LIFOThe default LIFO status. True means that borrowObject returns the most recently used ("last in") idle object in a pool (if there are idle instances available). False means that pools behave as FIFO queues - objects are taken from idle object pools in the order that they are returned.- See Also:
-
-
Constructor Details
-
GenericKeyedObjectPool
public GenericKeyedObjectPool()Create a newGenericKeyedObjectPoolwith no factory. -
GenericKeyedObjectPool
Create a newGenericKeyedObjectPoolusing the specified values.- Parameters:
factory- theKeyedPoolableObjectFactoryto use to create, validate, and destroy objects if notnull
-
GenericKeyedObjectPool
public GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory, GenericKeyedObjectPool.Config config) Create a newGenericKeyedObjectPoolusing the specified values.- Parameters:
factory- theKeyedPoolableObjectFactoryto use to create, validate, and destroy objects if notnullconfig- a non-nullGenericKeyedObjectPool.Configdescribing the configuration
-
GenericKeyedObjectPool
Create a newGenericKeyedObjectPoolusing the specified values.- Parameters:
factory- theKeyedPoolableObjectFactoryto use to create, validate, and destroy objects if notnullmaxActive- the maximum number of objects that can be borrowed from me at one time (seesetMaxActive(int))
-
GenericKeyedObjectPool
public GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory, int maxActive, byte whenExhaustedAction, long maxWait) Create a newGenericKeyedObjectPoolusing the specified values.- Parameters:
factory- theKeyedPoolableObjectFactoryto use to create, validate, and destroy objects if notnullmaxActive- the maximum number of objects that can be borrowed from me at one time (seesetMaxActive(int))whenExhaustedAction- the action to take when the pool is exhausted (seesetWhenExhaustedAction(byte))maxWait- the maximum amount of time to wait for an idle object when the pool is exhausted andwhenExhaustedActionisWHEN_EXHAUSTED_BLOCK(otherwise ignored) (seesetMaxWait(long))
-
GenericKeyedObjectPool
public GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory, int maxActive, byte whenExhaustedAction, long maxWait, boolean testOnBorrow, boolean testOnReturn) Create a newGenericKeyedObjectPoolusing the specified values.- Parameters:
factory- theKeyedPoolableObjectFactoryto use to create, validate, and destroy objects if notnullmaxActive- the maximum number of objects that can be borrowed from me at one time (seesetMaxActive(int))maxWait- the maximum amount of time to wait for an idle object when the pool is exhausted andwhenExhaustedActionisWHEN_EXHAUSTED_BLOCK(otherwise ignored) (seesetMaxWait(long))whenExhaustedAction- the action to take when the pool is exhausted (seesetWhenExhaustedAction(byte))testOnBorrow- whether or not to validate objects before they are returned by theborrowObject(K)method (seesetTestOnBorrow(boolean))testOnReturn- whether or not to validate objects after they are returned to thereturnObject(K, V)method (seesetTestOnReturn(boolean))
-
GenericKeyedObjectPool
public GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle) Create a newGenericKeyedObjectPoolusing the specified values.- Parameters:
factory- theKeyedPoolableObjectFactoryto use to create, validate, and destroy objects if notnullmaxActive- the maximum number of objects that can be borrowed from me at one time (seesetMaxActive(int))whenExhaustedAction- the action to take when the pool is exhausted (seesetWhenExhaustedAction(byte))maxWait- the maximum amount of time to wait for an idle object when the pool is exhausted andwhenExhaustedActionisWHEN_EXHAUSTED_BLOCK(otherwise ignored) (seesetMaxWait(long))maxIdle- the maximum number of idle objects in my pool (seesetMaxIdle(int))
-
GenericKeyedObjectPool
public GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn) Create a newGenericKeyedObjectPoolusing the specified values.- Parameters:
factory- theKeyedPoolableObjectFactoryto use to create, validate, and destroy objects if notnullmaxActive- the maximum number of objects that can be borrowed from me at one time (seesetMaxActive(int))whenExhaustedAction- the action to take when the pool is exhausted (seesetWhenExhaustedAction(byte))maxWait- the maximum amount of time to wait for an idle object when the pool is exhausted andwhenExhaustedActionisWHEN_EXHAUSTED_BLOCK(otherwise ignored) (seegetMaxWait())maxIdle- the maximum number of idle objects in my pool (seesetMaxIdle(int))testOnBorrow- whether or not to validate objects before they are returned by theborrowObject(K)method (seesetTestOnBorrow(boolean))testOnReturn- whether or not to validate objects after they are returned to thereturnObject(K, V)method (seesetTestOnReturn(boolean))
-
GenericKeyedObjectPool
public GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) Create a newGenericKeyedObjectPoolusing the specified values.- Parameters:
factory- theKeyedPoolableObjectFactoryto use to create, validate, and destroy objects if notnullmaxActive- the maximum number of objects that can be borrowed from me at one time (seesetMaxActive(int))whenExhaustedAction- the action to take when the pool is exhausted (seesetWhenExhaustedAction(byte))maxWait- the maximum amount of time to wait for an idle object when the pool is exhausted andwhenExhaustedActionisWHEN_EXHAUSTED_BLOCK(otherwise ignored) (seesetMaxWait(long))maxIdle- the maximum number of idle objects in my pool (seesetMaxIdle(int))testOnBorrow- whether or not to validate objects before they are returned by theborrowObject(K)method (seesetTestOnBorrow(boolean))testOnReturn- whether or not to validate objects after they are returned to thereturnObject(K, V)method (seesetTestOnReturn(boolean))timeBetweenEvictionRunsMillis- the amount of time (in milliseconds) to sleep between examining idle objects for eviction (seesetTimeBetweenEvictionRunsMillis(long))numTestsPerEvictionRun- the number of idle objects to examine per run within the idle object eviction thread (if any) (seesetNumTestsPerEvictionRun(int))minEvictableIdleTimeMillis- the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction (seesetMinEvictableIdleTimeMillis(long))testWhileIdle- whether or not to validate objects in the idle object eviction thread, if any (seesetTestWhileIdle(boolean))
-
GenericKeyedObjectPool
public GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int maxTotal, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) Create a newGenericKeyedObjectPoolusing the specified values.- Parameters:
factory- theKeyedPoolableObjectFactoryto use to create, validate, and destroy objects if notnullmaxActive- the maximum number of objects that can be borrowed from me at one time (seesetMaxActive(int))whenExhaustedAction- the action to take when the pool is exhausted (seesetWhenExhaustedAction(byte))maxWait- the maximum amount of time to wait for an idle object when the pool is exhausted andwhenExhaustedActionisWHEN_EXHAUSTED_BLOCK(otherwise ignored) (seesetMaxWait(long))maxIdle- the maximum number of idle objects in my pool (seesetMaxIdle(int))maxTotal- the maximum number of objects that can exists at one time (seesetMaxTotal(int))testOnBorrow- whether or not to validate objects before they are returned by theborrowObject(K)method (seesetTestOnBorrow(boolean))testOnReturn- whether or not to validate objects after they are returned to thereturnObject(K, V)method (seesetTestOnReturn(boolean))timeBetweenEvictionRunsMillis- the amount of time (in milliseconds) to sleep between examining idle objects for eviction (seesetTimeBetweenEvictionRunsMillis(long))numTestsPerEvictionRun- the number of idle objects to examine per run within the idle object eviction thread (if any) (seesetNumTestsPerEvictionRun(int))minEvictableIdleTimeMillis- the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction (seesetMinEvictableIdleTimeMillis(long))testWhileIdle- whether or not to validate objects in the idle object eviction thread, if any (seesetTestWhileIdle(boolean))
-
GenericKeyedObjectPool
public GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> 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) Create a newGenericKeyedObjectPoolusing the specified values.- Parameters:
factory- theKeyedPoolableObjectFactoryto use to create, validate, and destroy objects if notnullmaxActive- the maximum number of objects that can be borrowed at one time (seesetMaxActive(int))whenExhaustedAction- the action to take when the pool is exhausted (seesetWhenExhaustedAction(byte))maxWait- the maximum amount of time to wait for an idle object when the pool is exhausted andwhenExhaustedActionisWHEN_EXHAUSTED_BLOCK(otherwise ignored) (seesetMaxWait(long))maxIdle- the maximum number of idle objects in my pool (seesetMaxIdle(int))maxTotal- the maximum number of objects that can exists at one time (seesetMaxTotal(int))minIdle- the minimum number of idle objects to have in the pool at any one time (seesetMinIdle(int))testOnBorrow- whether or not to validate objects before they are returned by theborrowObject(K)method (seesetTestOnBorrow(boolean))testOnReturn- whether or not to validate objects after they are returned to thereturnObject(K, V)method (seesetTestOnReturn(boolean))timeBetweenEvictionRunsMillis- the amount of time (in milliseconds) to sleep between examining idle objects for eviction (seesetTimeBetweenEvictionRunsMillis(long))numTestsPerEvictionRun- the number of idle objects to examine per run within the idle object eviction thread (if any) (seesetNumTestsPerEvictionRun(int))minEvictableIdleTimeMillis- the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction (seesetMinEvictableIdleTimeMillis(long))testWhileIdle- whether or not to validate objects in the idle object eviction thread, if any (seesetTestWhileIdle(boolean))- Since:
- Pool 1.3
-
GenericKeyedObjectPool
public GenericKeyedObjectPool(KeyedPoolableObjectFactory<K, V> 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) Create a newGenericKeyedObjectPoolusing the specified values.- Parameters:
factory- theKeyedPoolableObjectFactoryto use to create, validate, and destroy objects if notnullmaxActive- the maximum number of objects that can be borrowed at one time (seesetMaxActive(int))whenExhaustedAction- the action to take when the pool is exhausted (seesetWhenExhaustedAction(byte))maxWait- the maximum amount of time to wait for an idle object when the pool is exhausted andwhenExhaustedActionisWHEN_EXHAUSTED_BLOCK(otherwise ignored) (seesetMaxWait(long))maxIdle- the maximum number of idle objects in my pool (seesetMaxIdle(int))maxTotal- the maximum number of objects that can exists at one time (seesetMaxTotal(int))minIdle- the minimum number of idle objects to have in the pool at any one time (seesetMinIdle(int))testOnBorrow- whether or not to validate objects before they are returned by theborrowObject(K)method (seesetTestOnBorrow(boolean))testOnReturn- whether or not to validate objects after they are returned to thereturnObject(K, V)method (seesetTestOnReturn(boolean))timeBetweenEvictionRunsMillis- the amount of time (in milliseconds) to sleep between examining idle objects for eviction (seesetTimeBetweenEvictionRunsMillis(long))numTestsPerEvictionRun- the number of idle objects to examine per run within the idle object eviction thread (if any) (seesetNumTestsPerEvictionRun(int))minEvictableIdleTimeMillis- the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction (seesetMinEvictableIdleTimeMillis(long))testWhileIdle- whether or not to validate objects in the idle object eviction thread, if any (seesetTestWhileIdle(boolean))lifo- whether or not the pools behave as LIFO (last in first out) queues (seesetLifo(boolean))- Since:
- Pool 1.4
-
-
Method Details
-
getMaxActive
public int getMaxActive()Returns the cap on the number of object instances allocated by the pool (checked out or idle), per key. A negative value indicates no limit.- Returns:
- the cap on the number of active instances per key.
- See Also:
-
setMaxActive
public void setMaxActive(int maxActive) Sets the cap on the number of object instances managed by the pool per key.- Parameters:
maxActive- The cap on the number of object instances per key. Use a negative value for no limit.- See Also:
-
getMaxTotal
public int getMaxTotal()Returns the overall maximum number of objects (across pools) that can exist at one time. A negative value indicates no limit.- Returns:
- the maximum number of instances in circulation at one time.
- See Also:
-
setMaxTotal
public void setMaxTotal(int maxTotal) Sets the cap on the total number of instances from all pools combined. WhenmaxTotalis set to a positive value andborrowObjectis invoked when at the limit with no idle instances available, an attempt is made to create room by clearing the oldest 15% of the elements from the keyed pools.- Parameters:
maxTotal- The cap on the total number of instances across pools. Use a negative value for no limit.- See Also:
-
getWhenExhaustedAction
public byte getWhenExhaustedAction()Returns the action to take when theborrowObject(K)method is invoked when the pool is exhausted (the maximum number of "active" objects has been reached).- Returns:
- one of
WHEN_EXHAUSTED_BLOCK,WHEN_EXHAUSTED_FAILorWHEN_EXHAUSTED_GROW - See Also:
-
setWhenExhaustedAction
public void setWhenExhaustedAction(byte whenExhaustedAction) Sets the action to take when theborrowObject(K)method is invoked when the pool is exhausted (the maximum number of "active" objects has been reached).- Parameters:
whenExhaustedAction- the action code, which must be one ofWHEN_EXHAUSTED_BLOCK,WHEN_EXHAUSTED_FAIL, orWHEN_EXHAUSTED_GROW- See Also:
-
getMaxWait
public long getMaxWait()Returns the maximum amount of time (in milliseconds) theborrowObject(K)method should block before throwing an exception when the pool is exhausted and the"when exhausted" actionisWHEN_EXHAUSTED_BLOCK. When less than or equal to 0, theborrowObject(K)method may block indefinitely.- Returns:
- the maximum number of milliseconds borrowObject will block.
- See Also:
-
setMaxWait
public void setMaxWait(long maxWait) Sets the maximum amount of time (in milliseconds) theborrowObject(K)method should block before throwing an exception when the pool is exhausted and the"when exhausted" actionisWHEN_EXHAUSTED_BLOCK. When less than or equal to 0, theborrowObject(K)method may block indefinitely.- Parameters:
maxWait- the maximum number of milliseconds borrowObject will block or negative for indefinitely.- See Also:
-
getMaxIdle
public int getMaxIdle()Returns the cap on the number of "idle" instances per key.- Returns:
- the maximum number of "idle" instances that can be held in a given keyed pool.
- See Also:
-
setMaxIdle
public void setMaxIdle(int maxIdle) Sets the cap on the number of "idle" instances in the pool. If maxIdle is set too low on heavily loaded systems it is possible you will see objects being destroyed and almost immediately new objects being created. This is a result of the active threads momentarily returning objects faster than they are requesting them them, causing the number of idle objects to rise above maxIdle. The best value for maxIdle for heavily loaded system will vary but the default is a good starting point.- Parameters:
maxIdle- the maximum number of "idle" instances that can be held in a given keyed pool. Use a negative value for no limit.- See Also:
-
setMinIdle
public void setMinIdle(int poolSize) Sets the minimum number of idle objects to maintain in each of the keyed pools. This setting has no effect unlesstimeBetweenEvictionRunsMillis > 0and attempts to ensure that each pool has the required minimum number of instances are only made during idle object eviction runs.- Parameters:
poolSize- - The minimum size of the each keyed pool- Since:
- Pool 1.3
- See Also:
-
getMinIdle
public int getMinIdle()Returns the minimum number of idle objects to maintain in each of the keyed pools. This setting has no effect unlesstimeBetweenEvictionRunsMillis > 0and attempts to ensure that each pool has the required minimum number of instances are only made during idle object eviction runs.- Returns:
- minimum size of the each keyed pool
- Since:
- Pool 1.3
- See Also:
-
getTestOnBorrow
public boolean getTestOnBorrow()Whentrue, objects will bevalidatedbefore being returned by theborrowObject(K)method. If the object fails to validate, it will be dropped from the pool, and we will attempt to borrow another.- Returns:
trueif objects are validated before being borrowed.- See Also:
-
setTestOnBorrow
public void setTestOnBorrow(boolean testOnBorrow) Whentrue, objects will bevalidatedbefore being returned by theborrowObject(K)method. If the object fails to validate, it will be dropped from the pool, and we will attempt to borrow another.- Parameters:
testOnBorrow- whether object should be validated before being returned by borrowObject.- See Also:
-
getTestOnReturn
public boolean getTestOnReturn()Whentrue, objects will bevalidatedbefore being returned to the pool within thereturnObject(K, V).- Returns:
truewhen objects will be validated before being returned.- See Also:
-
setTestOnReturn
public void setTestOnReturn(boolean testOnReturn) Whentrue, objects will bevalidatedbefore being returned to the pool within thereturnObject(K, V).- Parameters:
testOnReturn-trueso objects will be validated before being returned.- See Also:
-
getTimeBetweenEvictionRunsMillis
public long getTimeBetweenEvictionRunsMillis()Returns the number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread will be run.- Returns:
- milliseconds to sleep between evictor runs.
- See Also:
-
setTimeBetweenEvictionRunsMillis
public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) Sets the number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread will be run.- Parameters:
timeBetweenEvictionRunsMillis- milliseconds to sleep between evictor runs.- See Also:
-
getNumTestsPerEvictionRun
public int getNumTestsPerEvictionRun()Returns the max number of objects to examine during each run of the idle object evictor thread (if any).- Returns:
- number of objects to examine each eviction run.
- See Also:
-
setNumTestsPerEvictionRun
public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) Sets the max number of objects to examine during each run of the idle object evictor thread (if any).When a negative value is supplied,
ceil(tests will be run. I.e., when the value isgetNumIdle())/abs(getNumTestsPerEvictionRun())-n, roughly onenth of the idle objects will be tested per run. When the value is positive, the number of tests actually performed in each run will be the minimum of this value and the number of instances idle in the pools.- Parameters:
numTestsPerEvictionRun- number of objects to examine each eviction run.- See Also:
-
getMinEvictableIdleTimeMillis
public long getMinEvictableIdleTimeMillis()Returns the minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any).- Returns:
- minimum amount of time an object may sit idle in the pool before it is eligible for eviction.
- See Also:
-
setMinEvictableIdleTimeMillis
public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) Sets the minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any). When non-positive, no objects will be evicted from the pool due to idle time alone.- Parameters:
minEvictableIdleTimeMillis- minimum amount of time an object may sit idle in the pool before it is eligible for eviction.- See Also:
-
getTestWhileIdle
public boolean getTestWhileIdle()Whentrue, objects will bevalidatedby the idle object evictor (if any). If an object fails to validate, it will be dropped from the pool.- Returns:
truewhen objects are validated when borrowed.- See Also:
-
setTestWhileIdle
public void setTestWhileIdle(boolean testWhileIdle) Whentrue, objects will bevalidatedby the idle object evictor (if any). If an object fails to validate, it will be dropped from the pool.- Parameters:
testWhileIdle-trueso objects are validated when borrowed.- See Also:
-
setConfig
Sets the configuration.- Parameters:
conf- the new configuration to use.- See Also:
-
getLifo
public boolean getLifo()Whether or not the idle object pools act as LIFO queues. True means that borrowObject returns the most recently used ("last in") idle object in a pool (if there are idle instances available). False means that the pools behave as FIFO queues - objects are taken from idle object pools in the order that they are returned.- Returns:
trueif the pools are configured to act as LIFO queues- Since:
- 1.4
-
setLifo
public void setLifo(boolean lifo) Sets the LIFO property of the pools. True means that borrowObject returns the most recently used ("last in") idle object in a pool (if there are idle instances available). False means that the pools behave as FIFO queues - objects are taken from idle object pools in the order that they are returned.- Parameters:
lifo- the new value for the lifo property- Since:
- 1.4
-
borrowObject
Borrows an object from the keyed pool associated with the given key.
If there is an idle instance available in the pool associated with the given key, then either the most-recently returned (if
lifo== true) or "oldest" (lifo == false) instance sitting idle in the pool will be activated and returned. If activation fails, ortestOnBorrowis set to true and validation fails, the instance is destroyed and the next available instance is examined. This continues until either a valid instance is returned or there are no more idle instances available.If there are no idle instances available in the pool associated with the given key, behavior depends on the
maxActive,maxTotal, and (if applicable)whenExhaustedActionandmaxWaitproperties. If the number of instances checked out from the pool under the given key is less thanmaxActiveand the total number of instances in circulation (under all keys) is less thanmaxTotal, a new instance is created, activated and (if applicable) validated and returned to the caller.If the associated keyed pool is exhausted (no available idle instances and no capacity to create new ones), this method will either block (
WHEN_EXHAUSTED_BLOCK), throw aNoSuchElementException(WHEN_EXHAUSTED_FAIL), or grow (WHEN_EXHAUSTED_GROW- ignoring maxActive, maxTotal properties). The length of time that this method will block whenwhenExhaustedAction == WHEN_EXHAUSTED_BLOCKis determined by themaxWaitproperty.When the pool is exhausted, multiple calling threads may be simultaneously blocked waiting for instances to become available. As of pool 1.5, a "fairness" algorithm has been implemented to ensure that threads receive available instances in request arrival order.
- Specified by:
borrowObjectin interfaceKeyedObjectPool<K,V> - Specified by:
borrowObjectin classBaseKeyedObjectPool<K,V> - Parameters:
key- pool key- Returns:
- object instance from the keyed pool
- Throws:
NoSuchElementException- if a keyed object instance cannot be returned.IllegalStateException- afterclosehas been called on this poolException- whenmakeObjectthrows an exception
-
clear
public void clear()Clears any objects sitting idle in the pool by removing them from the idle instance pool and then invoking the configured PoolableObjectFactory'sKeyedPoolableObjectFactory.destroyObject(Object, Object)method on each idle instance.Implementation notes:
- This method does not destroy or effect in any way instances that are checked out when it is invoked.
- Invoking this method does not prevent objects being returned to the idle instance pool, even during its execution. It locks the pool only during instance removal. Additional instances may be returned while removed items are being destroyed.
- Exceptions encountered destroying idle instances are swallowed.
- Specified by:
clearin interfaceKeyedObjectPool<K,V> - Overrides:
clearin classBaseKeyedObjectPool<K,V>
-
clearOldest
public void clearOldest()Clears oldest 15% of objects in pool. The method sorts the objects into a TreeMap and then iterates the first 15% for removal.- Since:
- Pool 1.3
-
clear
Clears the specified pool, removing all pooled instances corresponding to the givenkey.- Specified by:
clearin interfaceKeyedObjectPool<K,V> - Overrides:
clearin classBaseKeyedObjectPool<K,V> - Parameters:
key- the key to clear
-
getNumActive
public int getNumActive()Returns the total number of instances current borrowed from this pool but not yet returned.- Specified by:
getNumActivein interfaceKeyedObjectPool<K,V> - Overrides:
getNumActivein classBaseKeyedObjectPool<K,V> - Returns:
- the total number of instances currently borrowed from this pool
-
getNumIdle
public int getNumIdle()Returns the total number of instances currently idle in this pool.- Specified by:
getNumIdlein interfaceKeyedObjectPool<K,V> - Overrides:
getNumIdlein classBaseKeyedObjectPool<K,V> - Returns:
- the total number of instances currently idle in this pool
-
getNumActive
Returns the number of instances currently borrowed from but not yet returned to the pool corresponding to the givenkey.- Specified by:
getNumActivein interfaceKeyedObjectPool<K,V> - Overrides:
getNumActivein classBaseKeyedObjectPool<K,V> - Parameters:
key- the key to query- Returns:
- the number of instances corresponding to the given
keycurrently borrowed in this pool
-
getNumIdle
Returns the number of instances corresponding to the givenkeycurrently idle in this pool.- Specified by:
getNumIdlein interfaceKeyedObjectPool<K,V> - Overrides:
getNumIdlein classBaseKeyedObjectPool<K,V> - Parameters:
key- the key to query- Returns:
- the number of instances corresponding to the given
keycurrently idle in this pool
-
returnObject
Returns an object to a keyed pool.
For the pool to function correctly, the object instance must have been borrowed from the pool (under the same key) and not yet returned. Repeated
returnObjectcalls on the same object/key pair (with noborrowObjectcalls in between) will result in multiple references to the object in the idle instance pool.If
maxIdleis set to a positive value and the number of idle instances under the given key has reached this value, the returning instance is destroyed.If
testOnReturn== true, the returning instance is validated before being returned to the idle instance pool under the given key. In this case, if validation fails, the instance is destroyed.- Specified by:
returnObjectin interfaceKeyedObjectPool<K,V> - Specified by:
returnObjectin classBaseKeyedObjectPool<K,V> - Parameters:
key- pool keyobj- instance to return to the keyed pool- Throws:
Exception
-
invalidateObject
Invalidates an object from the pool.
By contract,
objmust have been obtained usingborrowObjectusing akeythat is equivalent to the one used to borrow theObjectin the first place.This method should be used when an object that has been borrowed is determined (due to an exception or other problem) to be invalid.
Activation of this method decrements the active count associated with the given keyed pool and attempts to destroy
obj.- Specified by:
invalidateObjectin interfaceKeyedObjectPool<K,V> - Specified by:
invalidateObjectin classBaseKeyedObjectPool<K,V> - Parameters:
key- pool keyobj- instance to invalidate- Throws:
Exception- if an exception occurs destroying the object
-
addObject
Create an object using thefactory, passivate it, and then place it in the idle object pool.addObjectis useful for "pre-loading" a pool with idle objects.- Specified by:
addObjectin interfaceKeyedObjectPool<K,V> - Overrides:
addObjectin classBaseKeyedObjectPool<K,V> - Parameters:
key- the key a new instance should be added to- Throws:
Exception- whenKeyedPoolableObjectFactory.makeObject(K)fails.IllegalStateException- when nofactoryhas been set or afterclose()has been called on this pool.
-
preparePool
Registers a key for pool control. IfpopulateImmediatelyistrueandminIdle > 0,the pool under the given key will be populated immediately withminIdleidle instances.- Parameters:
key- - The key to register for pool control.populateImmediately- - If this istrue, the pool will be populated immediately.- Since:
- Pool 1.3
-
close
Closes the keyed object pool. Once the pool is closed,
borrowObject(Object)will fail with IllegalStateException, butreturnObject(Object, Object)andinvalidateObject(Object, Object)will continue to work, with returned objects destroyed on return.Destroys idle instances in the pool by invoking
clear().- Specified by:
closein interfaceKeyedObjectPool<K,V> - Overrides:
closein classBaseKeyedObjectPool<K,V> - Throws:
Exception
-
evict
Perform
numTestsidle object eviction tests, evicting examined objects that meet the criteria for eviction. IftestWhileIdleis true, examined objects are validated when visited (and removed if invalid); otherwise only objects that have been idle for more thanminEvicableIdletimeMillisare removed.Successive activations of this method examine objects in keyed pools in sequence, cycling through the keys and examining objects in oldest-to-youngest order within the keyed pools.
- Throws:
Exception- when there is a problem evicting idle objects.
-