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
-basedKeyedObjectPool
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-usingObject
s 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:
Stack
-
-
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 newSimpleKeyedObjectPool
using the specifiedfactory
to create new instances.StackKeyedObjectPool(KeyedPoolableObjectFactory<K,V> factory, int max)
Create a newSimpleKeyedObjectPool
using the specifiedfactory
to create new instances.StackKeyedObjectPool(KeyedPoolableObjectFactory<K,V> factory, int max, int init)
Create a newSimpleKeyedObjectPool
using the specifiedfactory
to create new instances.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addObject(K key)
Create an object using thefactory
, passivate it, and then placed in the idle object pool.V
borrowObject(K key)
Borrows an object with the given key.void
clear()
Clears the pool, removing all pooled instances.void
clear(K key)
Clears the specified pool, removing all pooled instances corresponding to the givenkey
.void
close()
Close this pool, and free any resources associated with it.Map<K,Integer>
getActiveCount()
KeyedPoolableObjectFactory<K,V>
getFactory()
int
getInitSleepingCapacity()
int
getMaxSleeping()
int
getNumActive()
Returns the total number of instances current borrowed from this pool but not yet returned.int
getNumActive(K key)
Returns the number of instances currently borrowed from but not yet returned to the pool corresponding to the givenkey
.int
getNumIdle()
Returns the total number of instances currently idle in this pool.int
getNumIdle(K key)
Returns the number of instances corresponding to the givenkey
currently idle in this pool.Map<K,Stack<V>>
getPools()
int
getTotActive()
int
getTotIdle()
void
invalidateObject(K key, V obj)
Invalidates an object from the pool.void
returnObject(K key, V obj)
Returnsobj
to the pool underkey
.String
toString()
Returns a string representation of this StackKeyedObjectPool, including the number of pools, the keys and the size of each keyed pool.
-
-
-
Constructor Detail
-
StackKeyedObjectPool
public StackKeyedObjectPool()
Create a new pool using no factory. Clients must first set thefactory
or may populate the pool usingreturnObject
before they can beborrowed
.
-
StackKeyedObjectPool
public StackKeyedObjectPool(int max)
Create a new pool using no factory. Clients must first set thefactory
or may populate the pool usingreturnObject
before 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 thefactory
or may populate the pool usingreturnObject
before 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 newSimpleKeyedObjectPool
using the specifiedfactory
to create new instances.- Parameters:
factory
- theKeyedPoolableObjectFactory
used to populate the pool
-
StackKeyedObjectPool
public StackKeyedObjectPool(KeyedPoolableObjectFactory<K,V> factory, int max)
Create a newSimpleKeyedObjectPool
using the specifiedfactory
to create new instances. capping the number of "sleeping" instances tomax
- Parameters:
factory
- theKeyedPoolableObjectFactory
used 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 newSimpleKeyedObjectPool
using the specifiedfactory
to create new instances. capping the number of "sleeping" instances tomax
, and initially allocating a container capable of containing at leastinit
instances.- Parameters:
factory
- theKeyedPoolableObjectFactory
used 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 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:
borrowObject
in interfaceKeyedObjectPool<K,V>
- Specified by:
borrowObject
in classBaseKeyedObjectPool<K,V>
- Parameters:
key
- the pool key- Returns:
- keyed poolable object instance
- Throws:
IllegalStateException
- afterclose
has been called on this poolException
- whenmakeObject
throws an exceptionNoSuchElementException
- when the pool is exhausted and cannot or will not return another instance
-
returnObject
public void returnObject(K key, V obj) throws Exception
Returnsobj
to the pool underkey
. If adding the returning instance to the pool results inmaxSleeping
exceeded for the given key, the oldest instance in the idle object pool is destroyed to make room for the returning instance.- Specified by:
returnObject
in interfaceKeyedObjectPool<K,V>
- Specified by:
returnObject
in classBaseKeyedObjectPool<K,V>
- Parameters:
key
- the pool keyobj
- returning instance- Throws:
Exception
-
invalidateObject
public void invalidateObject(K key, V obj) throws Exception
Invalidates an object from the pool.
By contract,
obj
must have been obtained usingborrowObject
using akey
that is equivalent to the one used to borrow theObject
in 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:
invalidateObject
in interfaceKeyedObjectPool<K,V>
- Specified by:
invalidateObject
in classBaseKeyedObjectPool<K,V>
- Parameters:
key
- the key used to obtain the objectobj
- aborrowed
instance to be returned.- Throws:
Exception
-
addObject
public void addObject(K key) throws Exception
Create an object using thefactory
, passivate it, and then placed in the idle object pool.addObject
is useful for "pre-loading" a pool with idle objects.- Specified by:
addObject
in interfaceKeyedObjectPool<K,V>
- Overrides:
addObject
in classBaseKeyedObjectPool<K,V>
- Parameters:
key
- the key a new instance should be added to- Throws:
Exception
- whenKeyedPoolableObjectFactory.makeObject(K)
fails.IllegalStateException
- when nofactory
has 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:
getNumIdle
in interfaceKeyedObjectPool<K,V>
- Overrides:
getNumIdle
in 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:
getNumActive
in interfaceKeyedObjectPool<K,V>
- Overrides:
getNumActive
in 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:
getNumActive
in interfaceKeyedObjectPool<K,V>
- Overrides:
getNumActive
in classBaseKeyedObjectPool<K,V>
- Parameters:
key
- the key to query- Returns:
- the number of instances corresponding to the given
key
currently borrowed in this pool
-
getNumIdle
public int getNumIdle(K key)
Returns the number of instances corresponding to the givenkey
currently idle in this pool.- Specified by:
getNumIdle
in interfaceKeyedObjectPool<K,V>
- Overrides:
getNumIdle
in classBaseKeyedObjectPool<K,V>
- Parameters:
key
- the key to query- Returns:
- the number of instances corresponding to the given
key
currently idle in this pool
-
clear
public void clear()
Clears the pool, removing all pooled instances.- Specified by:
clear
in interfaceKeyedObjectPool<K,V>
- Overrides:
clear
in classBaseKeyedObjectPool<K,V>
-
clear
public void clear(K key)
Clears the specified pool, removing all pooled instances corresponding to the givenkey
.- Specified by:
clear
in interfaceKeyedObjectPool<K,V>
- Overrides:
clear
in classBaseKeyedObjectPool<K,V>
- Parameters:
key
- the key to clear
-
toString
public String toString()
Returns a string representation of this StackKeyedObjectPool, including the number of pools, the keys and the size of each keyed pool.
-
close
public void close() throws Exception
Close this pool, and free any resources associated with it.Calling
addObject
orborrowObject
after invoking this method on a pool will cause them to throw anIllegalStateException
.- Specified by:
close
in interfaceKeyedObjectPool<K,V>
- Overrides:
close
in classBaseKeyedObjectPool<K,V>
- Throws:
Exception
- deprecated: implementations should silently fail if not all resources can be freed.
-
getFactory
public KeyedPoolableObjectFactory<K,V> getFactory()
- Returns:
- the
KeyedPoolableObjectFactory
used by this pool to manage object instances. - Since:
- 1.5.5
-
getMaxSleeping
public int getMaxSleeping()
- Returns:
- the cap on the number of "sleeping" instances in
each
pool. - 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
-
-