K
- the type of keys in this poolV
- the type of objects held in this poolKeyedObjectPool<K,V>
public 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 Object
s without introducing
artificial limits.
Stack
Constructor | 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 | 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.
|
java.util.Map<K,java.lang.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. |
java.util.Map<K,java.util.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 . |
java.lang.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 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 void addObject(K 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 KeyedObjectPool<K,V>
addObject
in class BaseKeyedObjectPool<K,V>
key
- the key a new instance should be added tojava.lang.Exception
- when KeyedPoolableObjectFactory.makeObject(K)
fails.java.lang.IllegalStateException
- when no factory
has been set or after close()
has been called on this pool.public V borrowObject(K key) throws java.lang.Exception
borrowObject
in interface KeyedObjectPool<K,V>
borrowObject
in class BaseKeyedObjectPool<K,V>
key
- the pool keyjava.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 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 java.lang.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>
java.lang.Exception
- deprecated: implementations should silently fail if not all resources can be freed.public java.util.Map<K,java.lang.Integer> getActiveCount()
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 java.util.Map<K,java.util.Stack<V>> getPools()
public int getTotActive()
public int getTotIdle()
public void invalidateObject(K key, V obj) throws java.lang.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.java.lang.Exception
public void returnObject(K key, V obj) throws java.lang.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 instancejava.lang.Exception
public java.lang.String toString()
toString
in class java.lang.Object
Copyright © 2001-2012 Apache Software Foundation. Documenation generated September 14 2017.