Class StackKeyedObjectPool<K,V>
- java.lang.Object
-
- org.apache.commons.pool.BaseKeyedObjectPool<K,V>
-
- org.apache.commons.pool.impl.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>
public class StackKeyedObjectPool<K,V> extends BaseKeyedObjectPool<K,V> implements KeyedObjectPool<K,V>
A simple,Stack-basedKeyedObjectPoolimplementation.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-usingObjects without introducing artificial limits.- Since:
- Pool 1.0
- Version:
- $Revision: 1222710 $ $Date: 2011-12-23 10:58:12 -0500 (Fri, 23 Dec 2011) $
- See Also:
Stack
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.HashMap<K,java.lang.Integer>_activeCountDeprecated.to be removed in pool 2.0.protected KeyedPoolableObjectFactory<K,V>_factoryDeprecated.to be removed in pool 2.0.protected int_initSleepingCapacityDeprecated.to be removed in pool 2.0.protected int_maxSleepingDeprecated.to be removed in pool 2.0.protected java.util.HashMap<K,java.util.Stack<V>>_poolsDeprecated.to be removed in pool 2.0.protected int_totActiveDeprecated.to be removed in pool 2.0.protected int_totIdleDeprecated.to be removed in pool 2.0.protected static intDEFAULT_INIT_SLEEPING_CAPACITYThe default initial size of the pool (this specifies the size of the container, it does not cause the pool to be pre-populated.)protected static intDEFAULT_MAX_SLEEPINGThe default cap on the number of "sleeping" instances in the pool.
-
Constructor Summary
Constructors Constructor Description StackKeyedObjectPool()Create 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
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddObject(K key)Create an object using thefactory, passivate it, and then placed in the idle object pool.VborrowObject(K key)Borrows an object with the given key.voidclear()Clears the pool, removing all pooled instances.voidclear(K key)Clears the specified pool, removing all pooled instances corresponding to the givenkey.voidclose()Close this pool, and free any resources associated with it.private voiddecrementActiveCount(K key)Decrements the active count for the given key.private voiddestroyStack(K key, java.util.Stack<V> stack)Destroys all instances in the stack and clears the stack.java.util.Map<K,java.lang.Integer>getActiveCount()private intgetActiveCount(K key)Returns the active instance count for the given key.KeyedPoolableObjectFactory<K,V>getFactory()intgetInitSleepingCapacity()intgetMaxSleeping()intgetNumActive()Returns 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.intgetNumIdle()Returns 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.java.util.Map<K,java.util.Stack<V>>getPools()intgetTotActive()intgetTotIdle()private voidincrementActiveCount(K key)Increment the active count for the given key.voidinvalidateObject(K key, V obj)Invalidates an object from the pool.voidreturnObject(K key, V obj)Returnsobjto the pool underkey.voidsetFactory(KeyedPoolableObjectFactory<K,V> factory)Deprecated.to be removed in pool 2.0java.lang.StringtoString()Returns a string representation of this StackKeyedObjectPool, including the number of pools, the keys and the size of each keyed pool.-
Methods inherited from class org.apache.commons.pool.BaseKeyedObjectPool
assertOpen, isClosed
-
-
-
-
Field Detail
-
DEFAULT_MAX_SLEEPING
protected static final int DEFAULT_MAX_SLEEPING
The default cap on the number of "sleeping" instances in the pool.- See Also:
- Constant Field Values
-
DEFAULT_INIT_SLEEPING_CAPACITY
protected static final int DEFAULT_INIT_SLEEPING_CAPACITY
The default initial size of the pool (this specifies the size of the container, it does not cause the pool to be pre-populated.)- See Also:
- Constant Field Values
-
_pools
@Deprecated protected java.util.HashMap<K,java.util.Stack<V>> _pools
Deprecated.to be removed in pool 2.0. UsegetPools()My named-set of pools.
-
_factory
@Deprecated protected KeyedPoolableObjectFactory<K,V> _factory
Deprecated.to be removed in pool 2.0. UsegetFactory()
-
_maxSleeping
@Deprecated protected int _maxSleeping
Deprecated.to be removed in pool 2.0. UsegetMaxSleeping()The cap on the number of "sleeping" instances ineachpool.
-
_initSleepingCapacity
@Deprecated protected int _initSleepingCapacity
Deprecated.to be removed in pool 2.0. UsegetInitSleepingCapacity().The initial capacity of each pool.
-
_totActive
@Deprecated protected int _totActive
Deprecated.to be removed in pool 2.0. UsegetTotActive().Total number of object borrowed and not yet returned for all pools.
-
_totIdle
@Deprecated protected int _totIdle
Deprecated.to be removed in pool 2.0. UsegetTotIdle().Total number of objects "sleeping" for all pools
-
_activeCount
@Deprecated protected java.util.HashMap<K,java.lang.Integer> _activeCount
Deprecated.to be removed in pool 2.0. UsegetActiveCount().Number of active objects borrowed and not yet returned by pool
-
-
Constructor Detail
-
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(KeyedPoolableObjectFactory, int),setFactory(KeyedPoolableObjectFactory)
-
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(KeyedPoolableObjectFactory, int, int),setFactory(KeyedPoolableObjectFactory)
-
StackKeyedObjectPool
public StackKeyedObjectPool(KeyedPoolableObjectFactory<K,V> factory)
Create a newSimpleKeyedObjectPoolusing the specifiedfactoryto create new instances.- Parameters:
factory- theKeyedPoolableObjectFactoryused to populate the pool
-
StackKeyedObjectPool
public StackKeyedObjectPool(KeyedPoolableObjectFactory<K,V> factory, int max)
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
public StackKeyedObjectPool(KeyedPoolableObjectFactory<K,V> factory, int max, int init)
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 Detail
-
borrowObject
public V borrowObject(K key) throws java.lang.Exception
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:
java.lang.IllegalStateException- afterclosehas been called on this pooljava.lang.Exception- whenmakeObjectthrows an exceptionjava.util.NoSuchElementException- when the pool is exhausted and cannot or will not return another instance
-
returnObject
public void returnObject(K key, V obj) throws java.lang.Exception
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:
java.lang.Exception
-
invalidateObject
public void invalidateObject(K key, V obj) throws java.lang.Exception
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:
java.lang.Exception
-
addObject
public void addObject(K key) throws java.lang.Exception
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:
java.lang.Exception- whenKeyedPoolableObjectFactory.makeObject(K)fails.java.lang.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
public int getNumActive(K key)
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
public int getNumIdle(K key)
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
public void clear(K key)
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
-
destroyStack
private void destroyStack(K key, java.util.Stack<V> stack)
Destroys all instances in the stack and clears the stack.- Parameters:
key- key passed to factory when destroying instancesstack- stack to destroy
-
toString
public java.lang.String toString()
Returns a string representation of this StackKeyedObjectPool, including the number of pools, the keys and the size of each keyed pool.- Overrides:
toStringin classjava.lang.Object- Returns:
- Keys and pool sizes
-
close
public void close() throws java.lang.ExceptionClose 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:
java.lang.Exception- deprecated: implementations should silently fail if not all resources can be freed.
-
setFactory
@Deprecated public void setFactory(KeyedPoolableObjectFactory<K,V> factory) throws java.lang.IllegalStateException
Deprecated.to be removed in pool 2.0Sets thefactorythe pool uses to create new instances. Trying to change thefactoryafter a pool has been used will frequently throw anUnsupportedOperationException.- Specified by:
setFactoryin interfaceKeyedObjectPool<K,V>- Overrides:
setFactoryin classBaseKeyedObjectPool<K,V>- Parameters:
factory- theKeyedPoolableObjectFactoryused to manage object instances- Throws:
java.lang.IllegalStateException- when the factory cannot be set at this time
-
getFactory
public KeyedPoolableObjectFactory<K,V> getFactory()
- Returns:
- the
KeyedPoolableObjectFactoryused by this pool to manage object instances. - Since:
- 1.5.5
-
getActiveCount
private int getActiveCount(K key)
Returns the active instance count for the given key.- Parameters:
key- pool key- Returns:
- active count
-
incrementActiveCount
private void incrementActiveCount(K key)
Increment the active count for the given key. Also increments the total active count.- Parameters:
key- pool key
-
decrementActiveCount
private void decrementActiveCount(K key)
Decrements the active count for the given key. Also decrements the total active count.- Parameters:
key- pool key
-
getPools
public java.util.Map<K,java.util.Stack<V>> 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
public java.util.Map<K,java.lang.Integer> getActiveCount()
- Returns:
- the _activeCount
- Since:
- 1.5.5
-
-