public class StackKeyedObjectPool extends BaseKeyedObjectPool implements KeyedObjectPool
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 factory)
Create a new
SimpleKeyedObjectPool using
the specified factory to create new instances. |
StackKeyedObjectPool(KeyedPoolableObjectFactory factory,
int max)
Create a new
SimpleKeyedObjectPool using
the specified factory to create new instances. |
StackKeyedObjectPool(KeyedPoolableObjectFactory 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(java.lang.Object key)
Create an object using the
factory,
passivate it, and then placed in the idle object pool. |
java.lang.Object |
borrowObject(java.lang.Object key)
Obtains an instance from this pool for the specified
key. |
void |
clear()
Clears the pool, removing all pooled instances.
|
void |
clear(java.lang.Object 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.
|
int |
getNumActive()
Returns the total number of instances current borrowed from this pool but not yet returned.
|
int |
getNumActive(java.lang.Object 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(java.lang.Object key)
Returns the number of instances corresponding to the given
key currently idle in this pool. |
void |
invalidateObject(java.lang.Object key,
java.lang.Object obj)
Invalidates an object from the pool
By contract,
obj must have been obtained
using borrowObject
or a related method as defined in an implementation
or sub-interface
using a key that is equivalent to the one used to
borrow the Object in the first place. |
void |
returnObject(java.lang.Object key,
java.lang.Object obj)
Return an instance to the pool.
|
void |
setFactory(KeyedPoolableObjectFactory factory)
Sets the
factory the pool uses
to create new instances. |
java.lang.String |
toString() |
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 factory)
SimpleKeyedObjectPool using
the specified factory to create new instances.factory - the KeyedPoolableObjectFactory used to populate the poolpublic StackKeyedObjectPool(KeyedPoolableObjectFactory 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 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(java.lang.Object key)
throws java.lang.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 KeyedObjectPooladdObject in class BaseKeyedObjectPoolkey - the key a new instance should be added tojava.lang.Exception - when KeyedPoolableObjectFactory.makeObject(java.lang.Object) fails.java.lang.IllegalStateException - when no factory has been set or after close() has been called on this pool.public java.lang.Object borrowObject(java.lang.Object key)
throws java.lang.Exception
KeyedObjectPoolkey.
Instances returned from this method will have been either newly created with
makeObject or will be a previously idle object and
have been activated with activateObject and
then validated with validateObject.
By contract, clients must return the borrowed object using
returnObject, invalidateObject, or a related method
as defined in an implementation or sub-interface,
using a key that is equivalent to the one used to
borrow the instance in the first place.
The behaviour of this method when the pool has been exhausted
is not strictly specified (although it may be specified by implementations).
Older versions of this method would return null to indicate exhaustion,
newer versions are encouraged to throw a NoSuchElementException.
borrowObject in interface KeyedObjectPoolborrowObject in class BaseKeyedObjectPoolkey - the key used to obtain the objectjava.lang.IllegalStateException - after close has been called on this pooljava.lang.Exception - when makeObject throws an exceptionjava.util.NoSuchElementException - when the pool is exhausted and cannot or will not return another instancepublic void clear()
clear in interface KeyedObjectPoolclear in class BaseKeyedObjectPoolpublic void clear(java.lang.Object key)
key.clear in interface KeyedObjectPoolclear in class BaseKeyedObjectPoolkey - the key to clearpublic void close()
throws java.lang.Exception
Calling addObject or borrowObject after invoking
this method on a pool will cause them to throw an IllegalStateException.
close in interface KeyedObjectPoolclose in class BaseKeyedObjectPooljava.lang.Exception - deprecated: implementations should silently fail if not all resources can be freed.public int getNumActive()
getNumActive in interface KeyedObjectPoolgetNumActive in class BaseKeyedObjectPoolpublic int getNumActive(java.lang.Object key)
key.getNumActive in interface KeyedObjectPoolgetNumActive in class BaseKeyedObjectPoolkey - the key to querykey currently borrowed in this poolpublic int getNumIdle()
getNumIdle in interface KeyedObjectPoolgetNumIdle in class BaseKeyedObjectPoolpublic int getNumIdle(java.lang.Object key)
key currently idle in this pool.getNumIdle in interface KeyedObjectPoolgetNumIdle in class BaseKeyedObjectPoolkey - the key to querykey currently idle in this poolpublic void invalidateObject(java.lang.Object key,
java.lang.Object obj)
throws java.lang.Exception
KeyedObjectPoolobj must have been obtained
using borrowObject
or a related method as defined in an implementation
or sub-interface
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 KeyedObjectPoolinvalidateObject in class BaseKeyedObjectPoolkey - the key used to obtain the objectobj - a borrowed instance to be returned.java.lang.Exceptionpublic void returnObject(java.lang.Object key,
java.lang.Object obj)
throws java.lang.Exception
KeyedObjectPoolobj must have been obtained
using borrowObject
or a related method as defined in an implementation
or sub-interface
using a key that is equivalent to the one used to
borrow the instance in the first place.returnObject in interface KeyedObjectPoolreturnObject in class BaseKeyedObjectPoolkey - the key used to obtain the objectobj - a borrowed instance to be returned.java.lang.Exceptionpublic void setFactory(KeyedPoolableObjectFactory factory) throws java.lang.IllegalStateException
factory the pool uses
to create new instances.
Trying to change the factory after a pool has been used will frequently
throw an UnsupportedOperationException.setFactory in interface KeyedObjectPoolsetFactory in class BaseKeyedObjectPoolfactory - the KeyedPoolableObjectFactory used to create new instances.java.lang.IllegalStateException - when the factory cannot be set at this timepublic java.lang.String toString()
toString in class java.lang.ObjectCopyright © 2001-2003 Apache Software Foundation. Documenation generated November 21 2014.