K - the type of keys in this poolV - the type of objects held in this poolpublic class StackKeyedObjectPool<K,V> extends BaseKeyedObjectPool<K,V> implements 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.
Stack| Constructor and 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 new
SimpleKeyedObjectPool using
the specified factory to create new instances. |
StackKeyedObjectPool(KeyedPoolableObjectFactory<K,V> factory,
int max)
Create a new
SimpleKeyedObjectPool using
the specified factory to create new instances. |
StackKeyedObjectPool(KeyedPoolableObjectFactory<K,V> factory,
int max,
int init)
Create a new
SimpleKeyedObjectPool using
the specified factory to create new instances. |
| Modifier and Type | Method and Description |
|---|---|
void |
addObject(K key)
Create an object using the
factory,
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 given
key. |
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 given
key. |
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 given
key 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)
Returns
obj to the pool under key. |
String |
toString()
Returns a string representation of this StackKeyedObjectPool, including
the number of pools, the keys and the size of each keyed pool.
|
public StackKeyedObjectPool()
factory or
may populate the pool using returnObject
before they can be borrowed.public StackKeyedObjectPool(int max)
factory or
may populate the pool using returnObject
before they can be borrowed.max - cap on the number of "sleeping" instances in the poolStackKeyedObjectPool(KeyedPoolableObjectFactory, int),
setFactory(KeyedPoolableObjectFactory)public StackKeyedObjectPool(int max,
int init)
factory or
may populate the pool using returnObject
before they can be borrowed.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.)StackKeyedObjectPool(KeyedPoolableObjectFactory, int, int),
setFactory(KeyedPoolableObjectFactory)public StackKeyedObjectPool(KeyedPoolableObjectFactory<K,V> factory)
SimpleKeyedObjectPool using
the specified factory to create new instances.factory - the KeyedPoolableObjectFactory used to populate the poolpublic StackKeyedObjectPool(KeyedPoolableObjectFactory<K,V> factory, int max)
SimpleKeyedObjectPool using
the specified factory to create new instances.
capping the number of "sleeping" instances to maxfactory - the KeyedPoolableObjectFactory used to populate the poolmax - cap on the number of "sleeping" instances in the poolpublic StackKeyedObjectPool(KeyedPoolableObjectFactory<K,V> factory, int max, int init)
SimpleKeyedObjectPool using
the specified factory to create new instances.
capping the number of "sleeping" instances to max,
and initially allocating a container capable of containing
at least init instances.factory - the KeyedPoolableObjectFactory 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.)public void addObject(K key) throws Exception
factory,
passivate it, and then placed in the idle object pool.
addObject is useful for "pre-loading" a pool with idle objects.addObject in interface KeyedObjectPool<K,V>addObject in class BaseKeyedObjectPool<K,V>key - the key a new instance should be added toException - when KeyedPoolableObjectFactory.makeObject(K) fails.IllegalStateException - when no factory has been set or after close() has been called on this pool.public V borrowObject(K key) throws Exception
borrowObject in interface KeyedObjectPool<K,V>borrowObject in class BaseKeyedObjectPool<K,V>key - the pool keyIllegalStateException - after close has been called on this poolException - when makeObject throws an exceptionNoSuchElementException - when the pool is exhausted and cannot or will not return another instancepublic void clear()
clear in interface KeyedObjectPool<K,V>clear in class BaseKeyedObjectPool<K,V>public void clear(K key)
key.clear in interface KeyedObjectPool<K,V>clear in class BaseKeyedObjectPool<K,V>key - the key to clearpublic void close()
throws Exception
Calling addObject or borrowObject after invoking
this method on a pool will cause them to throw an IllegalStateException.
close in interface KeyedObjectPool<K,V>close in class BaseKeyedObjectPool<K,V>Exception - deprecated: implementations should silently fail if not all resources can be freed.public KeyedPoolableObjectFactory<K,V> getFactory()
KeyedPoolableObjectFactory used by this pool to manage object instances.public int getInitSleepingCapacity()
public int getMaxSleeping()
each pool.public int getNumActive()
getNumActive in interface KeyedObjectPool<K,V>getNumActive in class BaseKeyedObjectPool<K,V>public int getNumActive(K key)
key.getNumActive in interface KeyedObjectPool<K,V>getNumActive in class BaseKeyedObjectPool<K,V>key - the key to querykey currently borrowed in this poolpublic int getNumIdle()
getNumIdle in interface KeyedObjectPool<K,V>getNumIdle in class BaseKeyedObjectPool<K,V>public int getNumIdle(K key)
key currently idle in this pool.getNumIdle in interface KeyedObjectPool<K,V>getNumIdle in class BaseKeyedObjectPool<K,V>key - the key to querykey currently idle in this poolpublic int getTotActive()
public int getTotIdle()
public void invalidateObject(K key, V obj) throws Exception
Invalidates an object from the pool.
By contract, obj must have been obtained
using borrowObject using a key that is
equivalent to the one used to borrow the Object 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.
invalidateObject in interface KeyedObjectPool<K,V>invalidateObject in class BaseKeyedObjectPool<K,V>key - the key used to obtain the objectobj - a borrowed instance to be returned.Exceptionpublic void returnObject(K key, V obj) throws Exception
obj to the pool under key. If adding the
returning instance to the pool results in maxSleeping
exceeded for the given key, the oldest instance in the idle object pool
is destroyed to make room for the returning instance.returnObject in interface KeyedObjectPool<K,V>returnObject in class BaseKeyedObjectPool<K,V>key - the pool keyobj - returning instanceExceptionCopyright © 2001-2012 Apache Software Foundation. Documenation generated July 7 2016.