Class StackKeyedObjectPool<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>
Stack-based KeyedObjectPool implementation.
Given a KeyedPoolableObjectFactory, this class will maintain
a simple pool of instances. A finite number of "sleeping"
or inactive instances is enforced, but when the pool is
empty, new instances are created to support the new load.
Hence this class places no limit on the number of "active"
instances created by the pool, but is quite useful for
re-using Objects without introducing
artificial limits.
- Since:
- Pool 1.0
- Version:
- $Revision: 1222710 $ $Date: 2011-12-23 10:58:12 -0500 (Fri, 23 Dec 2011) $
- Author:
- Rodney Waldhoff, Sandy McArthur
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a new pool using no factory.StackKeyedObjectPool(int max) Create a new pool using no factory.StackKeyedObjectPool(int max, int init) Create a new pool using no factory.StackKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory) Create a newSimpleKeyedObjectPoolusing the specifiedfactoryto create new instances.StackKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory, int max) Create a newSimpleKeyedObjectPoolusing the specifiedfactoryto create new instances.StackKeyedObjectPool(KeyedPoolableObjectFactory<K, V> factory, int max, int init) Create a newSimpleKeyedObjectPoolusing the specifiedfactoryto create new instances. -
Method Summary
Modifier and TypeMethodDescriptionvoidCreate an object using thefactory, passivate it, and then placed in the idle object pool.borrowObject(K key) Borrows an object with the given key.voidclear()Clears the pool, removing all pooled instances.voidClears the specified pool, removing all pooled instances corresponding to the givenkey.voidclose()Close this pool, and free any resources associated with it.intintintReturns the total number of instances current borrowed from this pool but not yet returned.intgetNumActive(K 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(K key) Returns the number of instances corresponding to the givenkeycurrently idle in this pool.getPools()intintvoidinvalidateObject(K key, V obj) Invalidates an object from the pool.voidreturnObject(K key, V obj) Returnsobjto the pool underkey.toString()Returns a string representation of this StackKeyedObjectPool, including the number of pools, the keys and the size of each keyed pool.
-
Constructor Details
-
StackKeyedObjectPool
public StackKeyedObjectPool()Create a new pool using no factory. Clients must first set thefactoryor may populate the pool usingreturnObjectbefore they can beborrowed. -
StackKeyedObjectPool
public StackKeyedObjectPool(int max) Create a new pool using no factory. Clients must first set thefactoryor may populate the pool usingreturnObjectbefore they can beborrowed.- Parameters:
max- cap on the number of "sleeping" instances in the pool- See Also:
-
StackKeyedObjectPool
public StackKeyedObjectPool(int max, int init) Create a new pool using no factory. Clients must first set thefactoryor may populate the pool usingreturnObjectbefore they can beborrowed.- Parameters:
max- cap on the number of "sleeping" instances in the poolinit- initial size of the pool (this specifies the size of the container, it does not cause the pool to be pre-populated.)- See Also:
-
StackKeyedObjectPool
Create a newSimpleKeyedObjectPoolusing the specifiedfactoryto create new instances.- Parameters:
factory- theKeyedPoolableObjectFactoryused to populate the pool
-
StackKeyedObjectPool
Create a newSimpleKeyedObjectPoolusing the specifiedfactoryto create new instances. capping the number of "sleeping" instances tomax- Parameters:
factory- theKeyedPoolableObjectFactoryused to populate the poolmax- cap on the number of "sleeping" instances in the pool
-
StackKeyedObjectPool
Create a newSimpleKeyedObjectPoolusing the specifiedfactoryto create new instances. capping the number of "sleeping" instances tomax, and initially allocating a container capable of containing at leastinitinstances.- Parameters:
factory- theKeyedPoolableObjectFactoryused to populate the poolmax- cap on the number of "sleeping" instances in the poolinit- initial size of the pool (this specifies the size of the container, it does not cause the pool to be pre-populated.)
-
-
Method Details
-
borrowObject
Borrows an object with the given key. If there are no idle instances under the given key, a new one is created.- Specified by:
borrowObjectin interfaceKeyedObjectPool<K,V> - Specified by:
borrowObjectin classBaseKeyedObjectPool<K,V> - Parameters:
key- the pool key- Returns:
- keyed poolable object instance
- Throws:
IllegalStateException- afterclosehas been called on this poolException- whenmakeObjectthrows an exceptionNoSuchElementException- when the pool is exhausted and cannot or will not return another instance
-
returnObject
Returnsobjto the pool underkey. If adding the returning instance to the pool results inmaxSleepingexceeded for the given key, the oldest instance in the idle object pool is destroyed to make room for the returning instance.- Specified by:
returnObjectin interfaceKeyedObjectPool<K,V> - Specified by:
returnObjectin classBaseKeyedObjectPool<K,V> - Parameters:
key- the pool keyobj- returning instance- 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.
- Specified by:
invalidateObjectin interfaceKeyedObjectPool<K,V> - Specified by:
invalidateObjectin classBaseKeyedObjectPool<K,V> - Parameters:
key- the key used to obtain the objectobj- aborrowedinstance to be returned.- Throws:
Exception
-
addObject
Create an object using thefactory, passivate it, and then placed 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.
-
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
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
-
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
-
clear
public void clear()Clears the pool, removing all pooled instances.- Specified by:
clearin interfaceKeyedObjectPool<K,V> - Overrides:
clearin classBaseKeyedObjectPool<K,V>
-
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
-
toString
Returns a string representation of this StackKeyedObjectPool, including the number of pools, the keys and the size of each keyed pool. -
close
Close this pool, and free any resources associated with it.Calling
addObjectorborrowObjectafter invoking this method on a pool will cause them to throw anIllegalStateException.- Specified by:
closein interfaceKeyedObjectPool<K,V> - Overrides:
closein classBaseKeyedObjectPool<K,V> - Throws:
Exception- deprecated: implementations should silently fail if not all resources can be freed.
-
getFactory
- Returns:
- the
KeyedPoolableObjectFactoryused by this pool to manage object instances. - Since:
- 1.5.5
-
getPools
- Returns:
- map of keyed pools
- Since:
- 1.5.5
-
getMaxSleeping
public int getMaxSleeping()- Returns:
- the cap on the number of "sleeping" instances in
eachpool. - Since:
- 1.5.5
-
getInitSleepingCapacity
public int getInitSleepingCapacity()- Returns:
- the initial capacity of each pool.
- Since:
- 1.5.5
-
getTotActive
public int getTotActive()- Returns:
- the _totActive
-
getTotIdle
public int getTotIdle()- Returns:
- the _totIdle
-
getActiveCount
- Returns:
- the _activeCount
- Since:
- 1.5.5
-