Class PoolUtils
- java.lang.Object
-
- org.apache.commons.pool.PoolUtils
-
public final class PoolUtils extends Object
This class consists exclusively of static methods that operate on or return ObjectPool or KeyedObjectPool related interfaces.- Since:
- Pool 1.3
- Version:
- $Revision: 1222670 $ $Date: 2011-12-23 08:18:25 -0500 (Fri, 23 Dec 2011) $
- Author:
- Sandy McArthur
-
-
Constructor Summary
Constructors Constructor Description PoolUtils()
PoolUtils instances should NOT be constructed in standard programming.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <V> ObjectPool<V>
adapt(KeyedObjectPool<Object,V> keyedPool)
Adapt aKeyedObjectPool
instance to work where anObjectPool
is needed.static <V> ObjectPool<V>
adapt(KeyedObjectPool<Object,V> keyedPool, Object key)
Adapt aKeyedObjectPool
instance to work where anObjectPool
is needed using the specifiedkey
when delegating.static <V> PoolableObjectFactory<V>
adapt(KeyedPoolableObjectFactory<Object,V> keyedFactory)
Adapt aKeyedPoolableObjectFactory
instance to work where aPoolableObjectFactory
is needed.static <K,V>
PoolableObjectFactory<V>adapt(KeyedPoolableObjectFactory<K,V> keyedFactory, K key)
Adapt aKeyedPoolableObjectFactory
instance to work where aPoolableObjectFactory
is needed using the specifiedkey
when delegating.static <K,V>
KeyedObjectPool<K,V>adapt(ObjectPool<V> pool)
Adapt anObjectPool
to work where anKeyedObjectPool
is needed.static <K,V>
KeyedPoolableObjectFactory<K,V>adapt(PoolableObjectFactory<V> factory)
Adapt aPoolableObjectFactory
instance to work where aKeyedPoolableObjectFactory
is needed.static <K,V>
KeyedObjectPool<K,V>checkedPool(KeyedObjectPool<K,V> keyedPool, Class<V> type)
Wraps aKeyedObjectPool
and dynamically checks the type of objects borrowed and returned to the keyedPool.static <T> ObjectPool<T>
checkedPool(ObjectPool<T> pool, Class<T> type)
Wraps anObjectPool
and dynamically checks the type of objects borrowed and returned to the pool.static <K,V>
Map<K,TimerTask>checkMinIdle(KeyedObjectPool<K,V> keyedPool, Collection<? extends K> keys, int minIdle, long period)
Periodically check the idle object count for each key in theCollection
keys
in the keyedPool.static <K,V>
TimerTaskcheckMinIdle(KeyedObjectPool<K,V> keyedPool, K key, int minIdle, long period)
Periodically check the idle object count for the key in the keyedPool.static <T> TimerTask
checkMinIdle(ObjectPool<T> pool, int minIdle, long period)
Periodically check the idle object count for the pool.static void
checkRethrow(Throwable t)
Should the supplied Throwable be re-thrown (eg if it is an instance of one of the Throwables that should never be swallowed).static <K,V>
KeyedObjectPool<K,V>erodingPool(KeyedObjectPool<K,V> keyedPool)
Returns a pool that adaptively decreases it's size when idle objects are no longer needed.static <K,V>
KeyedObjectPool<K,V>erodingPool(KeyedObjectPool<K,V> keyedPool, float factor)
Returns a pool that adaptively decreases it's size when idle objects are no longer needed.static <K,V>
KeyedObjectPool<K,V>erodingPool(KeyedObjectPool<K,V> keyedPool, float factor, boolean perKey)
Returns a pool that adaptively decreases it's size when idle objects are no longer needed.static <T> ObjectPool<T>
erodingPool(ObjectPool<T> pool)
Returns a pool that adaptively decreases it's size when idle objects are no longer needed.static <T> ObjectPool<T>
erodingPool(ObjectPool<T> pool, float factor)
Returns a pool that adaptively decreases it's size when idle objects are no longer needed.static <K,V>
voidprefill(KeyedObjectPool<K,V> keyedPool, Collection<? extends K> keys, int count)
CalladdObject(Object)
onkeyedPool
with each key inkeys
forcount
number of times.static <K,V>
voidprefill(KeyedObjectPool<K,V> keyedPool, K key, int count)
CalladdObject(Object)
onkeyedPool
withkey
count
number of times.static <T> void
prefill(ObjectPool<T> pool, int count)
CalladdObject()
onpool
count
number of times.static <K,V>
KeyedObjectPool<K,V>synchronizedPool(KeyedObjectPool<K,V> keyedPool)
Returns a synchronized (thread-safe) KeyedObjectPool backed by the specified KeyedObjectPool.static <T> ObjectPool<T>
synchronizedPool(ObjectPool<T> pool)
Returns a synchronized (thread-safe) ObjectPool backed by the specified ObjectPool.static <K,V>
KeyedPoolableObjectFactory<K,V>synchronizedPoolableFactory(KeyedPoolableObjectFactory<K,V> keyedFactory)
Returns a synchronized (thread-safe) KeyedPoolableObjectFactory backed by the specified KeyedPoolableObjectFactory.static <T> PoolableObjectFactory<T>
synchronizedPoolableFactory(PoolableObjectFactory<T> factory)
Returns a synchronized (thread-safe) PoolableObjectFactory backed by the specified PoolableObjectFactory.
-
-
-
Method Detail
-
checkRethrow
public static void checkRethrow(Throwable t)
Should the supplied Throwable be re-thrown (eg if it is an instance of one of the Throwables that should never be swallowed). Used by the pool error handling for operations that throw exceptions that normally need to be ignored.- Parameters:
t
- The Throwable to check- Throws:
ThreadDeath
- if that is passed inVirtualMachineError
- if that is passed in- Since:
- Pool 1.5.5
-
adapt
public static <V> PoolableObjectFactory<V> adapt(KeyedPoolableObjectFactory<Object,V> keyedFactory) throws IllegalArgumentException
Adapt aKeyedPoolableObjectFactory
instance to work where aPoolableObjectFactory
is needed. This method is the equivalent of callingPoolUtils.adapt(aKeyedPoolableObjectFactory, new Object())
.- Type Parameters:
V
- the type of object- Parameters:
keyedFactory
- theKeyedPoolableObjectFactory
to delegate to.- Returns:
- a
PoolableObjectFactory
that delegates tokeyedFactory
with an internal key. - Throws:
IllegalArgumentException
- whenkeyedFactory
isnull
.- Since:
- Pool 1.3
- See Also:
adapt(KeyedPoolableObjectFactory, Object)
-
adapt
public static <K,V> PoolableObjectFactory<V> adapt(KeyedPoolableObjectFactory<K,V> keyedFactory, K key) throws IllegalArgumentException
Adapt aKeyedPoolableObjectFactory
instance to work where aPoolableObjectFactory
is needed using the specifiedkey
when delegating.- Type Parameters:
K
- the type of keyV
- the type of object- Parameters:
keyedFactory
- theKeyedPoolableObjectFactory
to delegate to.key
- the key to use when delegating.- Returns:
- a
PoolableObjectFactory
that delegates tokeyedFactory
with the specified key. - Throws:
IllegalArgumentException
- whenkeyedFactory
orkey
isnull
.- Since:
- Pool 1.3
- See Also:
adapt(KeyedPoolableObjectFactory)
-
adapt
public static <K,V> KeyedPoolableObjectFactory<K,V> adapt(PoolableObjectFactory<V> factory) throws IllegalArgumentException
Adapt aPoolableObjectFactory
instance to work where aKeyedPoolableObjectFactory
is needed. The key is ignored.- Type Parameters:
K
- the type of keyV
- the type of object- Parameters:
factory
- thePoolableObjectFactory
to delegate to.- Returns:
- a
KeyedPoolableObjectFactory
that delegates tofactory
ignoring the key. - Throws:
IllegalArgumentException
- whenfactory
isnull
.- Since:
- Pool 1.3
-
adapt
public static <V> ObjectPool<V> adapt(KeyedObjectPool<Object,V> keyedPool) throws IllegalArgumentException
Adapt aKeyedObjectPool
instance to work where anObjectPool
is needed. This is the equivalent of callingPoolUtils.adapt(aKeyedObjectPool, new Object())
.- Type Parameters:
V
- the type of object- Parameters:
keyedPool
- theKeyedObjectPool
to delegate to.- Returns:
- an
ObjectPool
that delegates tokeyedPool
with an internal key. - Throws:
IllegalArgumentException
- whenkeyedPool
isnull
.- Since:
- Pool 1.3
- See Also:
adapt(KeyedObjectPool, Object)
-
adapt
public static <V> ObjectPool<V> adapt(KeyedObjectPool<Object,V> keyedPool, Object key) throws IllegalArgumentException
Adapt aKeyedObjectPool
instance to work where anObjectPool
is needed using the specifiedkey
when delegating.- Type Parameters:
V
- the type of object- Parameters:
keyedPool
- theKeyedObjectPool
to delegate to.key
- the key to use when delegating.- Returns:
- an
ObjectPool
that delegates tokeyedPool
with the specified key. - Throws:
IllegalArgumentException
- whenkeyedPool
orkey
isnull
.- Since:
- Pool 1.3
- See Also:
adapt(KeyedObjectPool)
-
adapt
public static <K,V> KeyedObjectPool<K,V> adapt(ObjectPool<V> pool) throws IllegalArgumentException
Adapt anObjectPool
to work where anKeyedObjectPool
is needed. The key is ignored.- Type Parameters:
K
- the type of keyV
- the type of object- Parameters:
pool
- theObjectPool
to delegate to.- Returns:
- a
KeyedObjectPool
that delegates topool
ignoring the key. - Throws:
IllegalArgumentException
- whenpool
isnull
.- Since:
- Pool 1.3
-
checkedPool
public static <T> ObjectPool<T> checkedPool(ObjectPool<T> pool, Class<T> type)
Wraps anObjectPool
and dynamically checks the type of objects borrowed and returned to the pool. If an object is passed to the pool that isn't of typetype
aClassCastException
will be thrown.- Type Parameters:
T
- the type of object- Parameters:
pool
- the pool to enforce type safety ontype
- the class type to enforce.- Returns:
- an
ObjectPool
that will only allow objects oftype
- Since:
- Pool 1.3
-
checkedPool
public static <K,V> KeyedObjectPool<K,V> checkedPool(KeyedObjectPool<K,V> keyedPool, Class<V> type)
Wraps aKeyedObjectPool
and dynamically checks the type of objects borrowed and returned to the keyedPool. If an object is passed to the keyedPool that isn't of typetype
aClassCastException
will be thrown.- Type Parameters:
K
- the type of keyV
- the type of object- Parameters:
keyedPool
- the keyedPool to enforce type safety ontype
- the class type to enforce.- Returns:
- a
KeyedObjectPool
that will only allow objects oftype
- Since:
- Pool 1.3
-
checkMinIdle
public static <T> TimerTask checkMinIdle(ObjectPool<T> pool, int minIdle, long period) throws IllegalArgumentException
Periodically check the idle object count for the pool. At most one idle object will be added per period. If there is an exception when callingObjectPool.addObject()
then no more checks will be performed.- Type Parameters:
T
- the type of object- Parameters:
pool
- the pool to check periodically.minIdle
- if theObjectPool.getNumIdle()
is less than this then add an idle object.period
- the frequency to check the number of idle objects in a pool, seeTimer.schedule(TimerTask, long, long)
.- Returns:
- the
TimerTask
that will periodically check the pools idle object count. - Throws:
IllegalArgumentException
- whenpool
isnull
or whenminIdle
is negative or whenperiod
isn't valid forTimer.schedule(TimerTask, long, long)
.- Since:
- Pool 1.3
-
checkMinIdle
public static <K,V> TimerTask checkMinIdle(KeyedObjectPool<K,V> keyedPool, K key, int minIdle, long period) throws IllegalArgumentException
Periodically check the idle object count for the key in the keyedPool. At most one idle object will be added per period. If there is an exception when callingKeyedObjectPool.addObject(Object)
then no more checks for that key will be performed.- Type Parameters:
K
- the type of keyV
- the type of object- Parameters:
keyedPool
- the keyedPool to check periodically.key
- the key to check the idle count of.minIdle
- if theKeyedObjectPool.getNumIdle(Object)
is less than this then add an idle object.period
- the frequency to check the number of idle objects in a keyedPool, seeTimer.schedule(TimerTask, long, long)
.- Returns:
- the
TimerTask
that will periodically check the pools idle object count. - Throws:
IllegalArgumentException
- whenkeyedPool
,key
isnull
or whenminIdle
is negative or whenperiod
isn't valid forTimer.schedule(TimerTask, long, long)
.- Since:
- Pool 1.3
-
checkMinIdle
public static <K,V> Map<K,TimerTask> checkMinIdle(KeyedObjectPool<K,V> keyedPool, Collection<? extends K> keys, int minIdle, long period) throws IllegalArgumentException
Periodically check the idle object count for each key in theCollection
keys
in the keyedPool. At most one idle object will be added per period.- Type Parameters:
K
- the type of keyV
- the type of object- Parameters:
keyedPool
- the keyedPool to check periodically.keys
- a collection of keys to check the idle object count.minIdle
- if theKeyedObjectPool.getNumIdle(Object)
is less than this then add an idle object.period
- the frequency to check the number of idle objects in a keyedPool, seeTimer.schedule(TimerTask, long, long)
.- Returns:
- a
Map
of key andTimerTask
pairs that will periodically check the pools idle object count. - Throws:
IllegalArgumentException
- whenkeyedPool
,keys
, or any of the values in the collection isnull
or whenminIdle
is negative or whenperiod
isn't valid forTimer.schedule(TimerTask, long, long)
.- Since:
- Pool 1.3
- See Also:
checkMinIdle(KeyedObjectPool, Object, int, long)
-
prefill
public static <T> void prefill(ObjectPool<T> pool, int count) throws Exception, IllegalArgumentException
CalladdObject()
onpool
count
number of times.- Type Parameters:
T
- the type of object- Parameters:
pool
- the pool to prefill.count
- the number of idle objects to add.- Throws:
Exception
- whenObjectPool.addObject()
fails.IllegalArgumentException
- whenpool
isnull
.- Since:
- Pool 1.3
-
prefill
public static <K,V> void prefill(KeyedObjectPool<K,V> keyedPool, K key, int count) throws Exception, IllegalArgumentException
CalladdObject(Object)
onkeyedPool
withkey
count
number of times.- Type Parameters:
K
- the type of keyV
- the type of object- Parameters:
keyedPool
- the keyedPool to prefill.key
- the key to add objects for.count
- the number of idle objects to add forkey
.- Throws:
Exception
- whenKeyedObjectPool.addObject(Object)
fails.IllegalArgumentException
- whenkeyedPool
orkey
isnull
.- Since:
- Pool 1.3
-
prefill
public static <K,V> void prefill(KeyedObjectPool<K,V> keyedPool, Collection<? extends K> keys, int count) throws Exception, IllegalArgumentException
CalladdObject(Object)
onkeyedPool
with each key inkeys
forcount
number of times. This has the same effect as callingprefill(KeyedObjectPool, Object, int)
for each key in thekeys
collection.- Type Parameters:
K
- the type of keyV
- the type of object- Parameters:
keyedPool
- the keyedPool to prefill.keys
-Collection
of keys to add objects for.count
- the number of idle objects to add for eachkey
.- Throws:
Exception
- whenKeyedObjectPool.addObject(Object)
fails.IllegalArgumentException
- whenkeyedPool
,keys
, or any value inkeys
isnull
.- Since:
- Pool 1.3
- See Also:
prefill(KeyedObjectPool, Object, int)
-
synchronizedPool
public static <T> ObjectPool<T> synchronizedPool(ObjectPool<T> pool)
Returns a synchronized (thread-safe) ObjectPool backed by the specified ObjectPool.Note: This should not be used on pool implementations that already provide proper synchronization such as the pools provided in the Commons Pool library. Wrapping a pool that
waits
for poolable objects to be returned before allowing another one to be borrowed with another layer of synchronization will cause liveliness issues or a deadlock.- Type Parameters:
T
- the type of object- Parameters:
pool
- the ObjectPool to be "wrapped" in a synchronized ObjectPool.- Returns:
- a synchronized view of the specified ObjectPool.
- Since:
- Pool 1.3
-
synchronizedPool
public static <K,V> KeyedObjectPool<K,V> synchronizedPool(KeyedObjectPool<K,V> keyedPool)
Returns a synchronized (thread-safe) KeyedObjectPool backed by the specified KeyedObjectPool.Note: This should not be used on pool implementations that already provide proper synchronization such as the pools provided in the Commons Pool library. Wrapping a pool that
waits
for poolable objects to be returned before allowing another one to be borrowed with another layer of synchronization will cause liveliness issues or a deadlock.- Type Parameters:
K
- the type of keyV
- the type of object- Parameters:
keyedPool
- the KeyedObjectPool to be "wrapped" in a synchronized KeyedObjectPool.- Returns:
- a synchronized view of the specified KeyedObjectPool.
- Since:
- Pool 1.3
-
synchronizedPoolableFactory
public static <T> PoolableObjectFactory<T> synchronizedPoolableFactory(PoolableObjectFactory<T> factory)
Returns a synchronized (thread-safe) PoolableObjectFactory backed by the specified PoolableObjectFactory.- Type Parameters:
T
- the type of object- Parameters:
factory
- the PoolableObjectFactory to be "wrapped" in a synchronized PoolableObjectFactory.- Returns:
- a synchronized view of the specified PoolableObjectFactory.
- Since:
- Pool 1.3
-
synchronizedPoolableFactory
public static <K,V> KeyedPoolableObjectFactory<K,V> synchronizedPoolableFactory(KeyedPoolableObjectFactory<K,V> keyedFactory)
Returns a synchronized (thread-safe) KeyedPoolableObjectFactory backed by the specified KeyedPoolableObjectFactory.- Type Parameters:
K
- the type of keyV
- the type of object- Parameters:
keyedFactory
- the KeyedPoolableObjectFactory to be "wrapped" in a synchronized KeyedPoolableObjectFactory.- Returns:
- a synchronized view of the specified KeyedPoolableObjectFactory.
- Since:
- Pool 1.3
-
erodingPool
public static <T> ObjectPool<T> erodingPool(ObjectPool<T> pool)
Returns a pool that adaptively decreases it's size when idle objects are no longer needed. This is intended as an always thread-safe alternative to using an idle object evictor provided by many pool implementations. This is also an effective way to shrink FIFO ordered pools that experience load spikes.- Type Parameters:
T
- the type of object- Parameters:
pool
- the ObjectPool to be decorated so it shrinks it's idle count when possible.- Returns:
- a pool that adaptively decreases it's size when idle objects are no longer needed.
- Since:
- Pool 1.4
- See Also:
erodingPool(ObjectPool, float)
-
erodingPool
public static <T> ObjectPool<T> erodingPool(ObjectPool<T> pool, float factor)
Returns a pool that adaptively decreases it's size when idle objects are no longer needed. This is intended as an always thread-safe alternative to using an idle object evictor provided by many pool implementations. This is also an effective way to shrink FIFO ordered pools that experience load spikes.The factor parameter provides a mechanism to tweak the rate at which the pool tries to shrink it's size. Values between 0 and 1 cause the pool to try to shrink it's size more often. Values greater than 1 cause the pool to less frequently try to shrink it's size.
- Type Parameters:
T
- the type of object- Parameters:
pool
- the ObjectPool to be decorated so it shrinks it's idle count when possible.factor
- a positive value to scale the rate at which the pool tries to reduce it's size. If 0 < factor < 1 then the pool shrinks more aggressively. If 1 < factor then the pool shrinks less aggressively.- Returns:
- a pool that adaptively decreases it's size when idle objects are no longer needed.
- Since:
- Pool 1.4
- See Also:
erodingPool(ObjectPool)
-
erodingPool
public static <K,V> KeyedObjectPool<K,V> erodingPool(KeyedObjectPool<K,V> keyedPool)
Returns a pool that adaptively decreases it's size when idle objects are no longer needed. This is intended as an always thread-safe alternative to using an idle object evictor provided by many pool implementations. This is also an effective way to shrink FIFO ordered pools that experience load spikes.- Type Parameters:
K
- the type of keyV
- the type of object- Parameters:
keyedPool
- the KeyedObjectPool to be decorated so it shrinks it's idle count when possible.- Returns:
- a pool that adaptively decreases it's size when idle objects are no longer needed.
- Since:
- Pool 1.4
- See Also:
erodingPool(KeyedObjectPool, float)
,erodingPool(KeyedObjectPool, float, boolean)
-
erodingPool
public static <K,V> KeyedObjectPool<K,V> erodingPool(KeyedObjectPool<K,V> keyedPool, float factor)
Returns a pool that adaptively decreases it's size when idle objects are no longer needed. This is intended as an always thread-safe alternative to using an idle object evictor provided by many pool implementations. This is also an effective way to shrink FIFO ordered pools that experience load spikes.The factor parameter provides a mechanism to tweak the rate at which the pool tries to shrink it's size. Values between 0 and 1 cause the pool to try to shrink it's size more often. Values greater than 1 cause the pool to less frequently try to shrink it's size.
- Type Parameters:
K
- the type of keyV
- the type of object- Parameters:
keyedPool
- the KeyedObjectPool to be decorated so it shrinks it's idle count when possible.factor
- a positive value to scale the rate at which the pool tries to reduce it's size. If 0 < factor < 1 then the pool shrinks more aggressively. If 1 < factor then the pool shrinks less aggressively.- Returns:
- a pool that adaptively decreases it's size when idle objects are no longer needed.
- Since:
- Pool 1.4
- See Also:
erodingPool(KeyedObjectPool, float, boolean)
-
erodingPool
public static <K,V> KeyedObjectPool<K,V> erodingPool(KeyedObjectPool<K,V> keyedPool, float factor, boolean perKey)
Returns a pool that adaptively decreases it's size when idle objects are no longer needed. This is intended as an always thread-safe alternative to using an idle object evictor provided by many pool implementations. This is also an effective way to shrink FIFO ordered pools that experience load spikes.The factor parameter provides a mechanism to tweak the rate at which the pool tries to shrink it's size. Values between 0 and 1 cause the pool to try to shrink it's size more often. Values greater than 1 cause the pool to less frequently try to shrink it's size.
The perKey parameter determines if the pool shrinks on a whole pool basis or a per key basis. When perKey is false, the keys do not have an effect on the rate at which the pool tries to shrink it's size. When perKey is true, each key is shrunk independently.
- Type Parameters:
K
- the type of keyV
- the type of object- Parameters:
keyedPool
- the KeyedObjectPool to be decorated so it shrinks it's idle count when possible.factor
- a positive value to scale the rate at which the pool tries to reduce it's size. If 0 < factor < 1 then the pool shrinks more aggressively. If 1 < factor then the pool shrinks less aggressively.perKey
- when true, each key is treated independently.- Returns:
- a pool that adaptively decreases it's size when idle objects are no longer needed.
- Since:
- Pool 1.4
- See Also:
erodingPool(KeyedObjectPool)
,erodingPool(KeyedObjectPool, float)
-
-