K
- the type of keys in this poolV
- the type of objects held in this poolpublic class StackKeyedObjectPool<K,V> extends BaseKeyedObjectPool<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 Object
s without introducing
artificial limits.
Stack
Modifier and Type | Field and Description |
---|---|
protected HashMap<K,Integer> |
_activeCount
Deprecated.
to be removed in pool 2.0. Use
getActiveCount() . |
protected KeyedPoolableObjectFactory<K,V> |
_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<K,Stack<V>> |
_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<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 . |
void |
setFactory(KeyedPoolableObjectFactory<K,V> 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, isClosed
protected static final int DEFAULT_MAX_SLEEPING
protected static final int DEFAULT_INIT_SLEEPING_CAPACITY
@Deprecated protected HashMap<K,Stack<V>> _pools
getPools()
@Deprecated protected KeyedPoolableObjectFactory<K,V> _factory
getFactory()
@Deprecated protected int _maxSleeping
getMaxSleeping()
each
pool.@Deprecated protected int _initSleepingCapacity
getInitSleepingCapacity()
.@Deprecated protected int _totActive
getTotActive()
.@Deprecated protected int _totIdle
getTotIdle()
.@Deprecated protected HashMap<K,Integer> _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<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 max
factory
- 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 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 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 instanceException
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.Exception
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 int getNumIdle()
getNumIdle
in interface KeyedObjectPool<K,V>
getNumIdle
in class BaseKeyedObjectPool<K,V>
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(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 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 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 KeyedObjectPool<K,V>
close
in class BaseKeyedObjectPool<K,V>
Exception
- deprecated: implementations should silently fail if not all resources can be freed.@Deprecated public void setFactory(KeyedPoolableObjectFactory<K,V> 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<K,V>
setFactory
in class BaseKeyedObjectPool<K,V>
factory
- the KeyedPoolableObjectFactory
used to manage object instancesIllegalStateException
- when the factory cannot be set at this timepublic KeyedPoolableObjectFactory<K,V> getFactory()
KeyedPoolableObjectFactory
used by this pool to manage object instances.public int getMaxSleeping()
each
pool.public int getInitSleepingCapacity()
public int getTotActive()
public int getTotIdle()
Copyright © 2000-2021 Apache Software Foundation. All Rights Reserved.