|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.pool.BaseKeyedObjectPool org.apache.commons.pool.impl.StackKeyedObjectPool
public class StackKeyedObjectPool
A simple, 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 Object
s without introducing
artificial limits.
Stack
Field Summary | |
---|---|
protected HashMap |
_activeCount
Number of active objects borrowed and not yet returned by pool |
protected KeyedPoolableObjectFactory |
_factory
My KeyedPoolableObjectFactory . |
protected int |
_initSleepingCapacity
The initial capacity of each pool. |
protected int |
_maxSleeping
The cap on the number of "sleeping" instances in each pool. |
protected HashMap |
_pools
My named-set of pools. |
protected int |
_totActive
Total number of object borrowed and not yet retuened for all pools |
protected int |
_totIdle
Total number of objects "sleeping" for all pools |
protected static 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.) |
protected static int |
DEFAULT_MAX_SLEEPING
The default cap on the number of "sleeping" instances in the pool. |
Constructor Summary | |
---|---|
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. |
Method Summary | |
---|---|
void |
addObject(Object key)
Create an object using the factory ,
passivate it, and then placed in the idle object pool. |
Object |
borrowObject(Object key)
Obtains an instance from this pool for the specified key . |
void |
clear()
Clears the pool, removing all pooled instances. |
void |
clear(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(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(Object key)
Returns the number of instances corresponding to the given key currently idle in this pool. |
void |
invalidateObject(Object key,
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(Object key,
Object obj)
Return an instance to the pool. |
void |
setFactory(KeyedPoolableObjectFactory factory)
Sets the factory the pool uses
to create new instances. |
String |
toString()
|
Methods inherited from class org.apache.commons.pool.BaseKeyedObjectPool |
---|
assertOpen, isClosed |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected static final int DEFAULT_MAX_SLEEPING
protected static final int DEFAULT_INIT_SLEEPING_CAPACITY
protected HashMap _pools
protected KeyedPoolableObjectFactory _factory
KeyedPoolableObjectFactory
.
protected int _maxSleeping
each
pool.
protected int _initSleepingCapacity
protected int _totActive
protected int _totIdle
protected HashMap _activeCount
Constructor Detail |
---|
public StackKeyedObjectPool()
factory
or
may populate the pool using returnObject
before they can be borrowed
.
StackKeyedObjectPool(KeyedPoolableObjectFactory)
,
setFactory(KeyedPoolableObjectFactory)
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 max
factory
- 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.)Method Detail |
---|
public Object borrowObject(Object key) throws Exception
KeyedObjectPool
key
.
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 KeyedObjectPool
borrowObject
in class BaseKeyedObjectPool
key
- the key used to obtain the object
IllegalStateException
- after close
has been called on this pool
Exception
- when makeObject
throws an exception
NoSuchElementException
- when the pool is exhausted and cannot or will not return another instancepublic void returnObject(Object key, Object obj) throws Exception
KeyedObjectPool
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 instance in the first place.
returnObject
in interface KeyedObjectPool
returnObject
in class BaseKeyedObjectPool
key
- the key used to obtain the objectobj
- a borrowed
instance to be returned.
Exception
public void invalidateObject(Object key, Object obj) throws Exception
KeyedObjectPool
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.
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
invalidateObject
in class BaseKeyedObjectPool
key
- the key used to obtain the objectobj
- a borrowed
instance to be returned.
Exception
public void addObject(Object 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
addObject
in class BaseKeyedObjectPool
key
- the key a new instance should be added to
Exception
- when KeyedPoolableObjectFactory.makeObject(java.lang.Object)
fails.
IllegalStateException
- when no factory
has been set or after close()
has been called on this pool.public int getNumIdle()
getNumIdle
in interface KeyedObjectPool
getNumIdle
in class BaseKeyedObjectPool
public int getNumActive()
getNumActive
in interface KeyedObjectPool
getNumActive
in class BaseKeyedObjectPool
public int getNumActive(Object key)
key
.
getNumActive
in interface KeyedObjectPool
getNumActive
in class BaseKeyedObjectPool
key
- the key to query
key
currently borrowed in this poolpublic int getNumIdle(Object key)
key
currently idle in this pool.
getNumIdle
in interface KeyedObjectPool
getNumIdle
in class BaseKeyedObjectPool
key
- the key to query
key
currently idle in this poolpublic void clear()
clear
in interface KeyedObjectPool
clear
in class BaseKeyedObjectPool
public void clear(Object key)
key
.
clear
in interface KeyedObjectPool
clear
in class BaseKeyedObjectPool
key
- the key to clearpublic String toString()
toString
in class Object
public 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
close
in class BaseKeyedObjectPool
Exception
- deprecated: implementations should silently fail if not all resources can be freed.public void setFactory(KeyedPoolableObjectFactory factory) throws 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 KeyedObjectPool
setFactory
in class BaseKeyedObjectPool
factory
- the KeyedPoolableObjectFactory
used to create new instances.
IllegalStateException
- when the factory cannot be set at this time
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |