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| Modifier and Type | Field and Description |
|---|---|
protected HashMap |
_activeCount
Deprecated.
to be removed in pool 2.0. Use
getActiveCount(). |
protected KeyedPoolableObjectFactory |
_factory
Deprecated.
to be removed in pool 2.0. Use
getFactory() |
protected int |
_initSleepingCapacity
Deprecated.
to be removed in pool 2.0. Use
getInitSleepingCapacity(). |
protected int |
_maxSleeping
Deprecated.
to be removed in pool 2.0. Use
getMaxSleeping() |
protected HashMap |
_pools
Deprecated.
to be removed in pool 2.0. Use
getPools() |
protected int |
_totActive
Deprecated.
to be removed in pool 2.0. Use
getTotActive(). |
protected int |
_totIdle
Deprecated.
to be removed in pool 2.0. Use
getTotIdle(). |
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 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(Object key)
Create an object using the
factory,
passivate it, and then placed in the idle object pool. |
Object |
borrowObject(Object key)
Borrows an object with the given 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.
|
Map |
getActiveCount() |
KeyedPoolableObjectFactory |
getFactory() |
int |
getInitSleepingCapacity() |
int |
getMaxSleeping() |
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. |
Map |
getPools() |
int |
getTotActive() |
int |
getTotIdle() |
void |
invalidateObject(Object key,
Object obj) |
void |
returnObject(Object key,
Object obj)
Returns
obj to the pool under key. |
void |
setFactory(KeyedPoolableObjectFactory factory)
Deprecated.
to be removed in pool 2.0
|
String |
toString()
Returns a string representation of this StackKeyedObjectPool, including
the number of pools, the keys and the size of each keyed pool.
|
assertOpen, isClosedprotected static final int DEFAULT_MAX_SLEEPING
protected static final int DEFAULT_INIT_SLEEPING_CAPACITY
protected HashMap _pools
getPools()protected KeyedPoolableObjectFactory _factory
getFactory()protected int _maxSleeping
getMaxSleeping()each pool.protected int _initSleepingCapacity
getInitSleepingCapacity().protected int _totActive
getTotActive().protected int _totIdle
getTotIdle().protected HashMap _activeCount
getActiveCount().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 Object borrowObject(Object key) throws Exception
borrowObject in interface KeyedObjectPoolborrowObject in class BaseKeyedObjectPoolkey - the pool keyExceptionpublic void returnObject(Object key, Object 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 KeyedObjectPoolreturnObject in class BaseKeyedObjectPoolkey - the pool keyobj - returning instanceExceptionpublic void invalidateObject(Object key, Object obj) throws Exception
invalidateObject in interface KeyedObjectPoolinvalidateObject in class BaseKeyedObjectPoolExceptionpublic 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 KeyedObjectPooladdObject in class BaseKeyedObjectPoolkey - the key a new instance should be added toException - 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 KeyedObjectPoolgetNumIdle in class BaseKeyedObjectPoolpublic int getNumActive()
getNumActive in interface KeyedObjectPoolgetNumActive in class BaseKeyedObjectPoolpublic int getNumActive(Object key)
key.getNumActive in interface KeyedObjectPoolgetNumActive in class BaseKeyedObjectPoolkey - the key to querykey currently borrowed in this poolpublic int getNumIdle(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 clear()
clear in interface KeyedObjectPoolclear in class BaseKeyedObjectPoolpublic void clear(Object key)
key.clear in interface KeyedObjectPoolclear in class BaseKeyedObjectPoolkey - the key to clearpublic String toString()
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 KeyedObjectPoolclose in class BaseKeyedObjectPoolException - 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 KeyedObjectPoolsetFactory in class BaseKeyedObjectPoolfactory - the KeyedPoolableObjectFactory used to manage object instancesIllegalStateException - when the factory cannot be set at this timepublic KeyedPoolableObjectFactory getFactory()
KeyedPoolableObjectFactory used by this pool to manage object instances.public Map getPools()
public int getMaxSleeping()
each pool.public int getInitSleepingCapacity()
public int getTotActive()
public int getTotIdle()
public Map getActiveCount()
Copyright © 2016. All rights reserved.