public final class PoolUtils extends Object
Constructor and Description |
---|
PoolUtils()
PoolUtils instances should NOT be constructed in standard programming.
|
Modifier and Type | Method and Description |
---|---|
static <V> ObjectPool<V> |
adapt(KeyedObjectPool<Object,V> keyedPool)
Adapt a
KeyedObjectPool instance to work where an ObjectPool is needed. |
static <V> ObjectPool<V> |
adapt(KeyedObjectPool<Object,V> keyedPool,
Object key)
Adapt a
KeyedObjectPool instance to work where an ObjectPool is needed using the
specified key when delegating. |
static <K,V> PoolableObjectFactory<V> |
adapt(KeyedPoolableObjectFactory<K,V> keyedFactory,
K key)
Adapt a
KeyedPoolableObjectFactory instance to work where a PoolableObjectFactory is
needed using the specified key when delegating. |
static <V> PoolableObjectFactory<V> |
adapt(KeyedPoolableObjectFactory<Object,V> keyedFactory)
Adapt a
KeyedPoolableObjectFactory instance to work where a PoolableObjectFactory is
needed. |
static <K,V> KeyedObjectPool<K,V> |
adapt(ObjectPool<V> pool)
Adapt an
ObjectPool to work where an KeyedObjectPool is needed. |
static <K,V> KeyedPoolableObjectFactory<K,V> |
adapt(PoolableObjectFactory<V> factory)
Adapt a
PoolableObjectFactory instance to work where a KeyedPoolableObjectFactory is
needed. |
static <K,V> KeyedObjectPool<K,V> |
checkedPool(KeyedObjectPool<K,V> keyedPool,
Class<V> type)
Wraps a
KeyedObjectPool 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 an
ObjectPool 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 the
Collection keys in the keyedPool. |
static <K,V> TimerTask |
checkMinIdle(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> void |
prefill(KeyedObjectPool<K,V> keyedPool,
Collection<? extends K> keys,
int count)
Call
addObject(Object) on keyedPool with each key in keys for
count number of times. |
static <K,V> void |
prefill(KeyedObjectPool<K,V> keyedPool,
K key,
int count)
Call
addObject(Object) on keyedPool with key count
number of times. |
static <T> void |
prefill(ObjectPool<T> pool,
int count)
Call
addObject() on pool 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.
|
public PoolUtils()
public static void checkRethrow(Throwable t)
t
- The Throwable to checkThreadDeath
- if that is passed inVirtualMachineError
- if that is passed inpublic static <V> PoolableObjectFactory<V> adapt(KeyedPoolableObjectFactory<Object,V> keyedFactory) throws IllegalArgumentException
KeyedPoolableObjectFactory
instance to work where a PoolableObjectFactory
is
needed. This method is the equivalent of calling
PoolUtils.adapt(aKeyedPoolableObjectFactory, new Object())
.V
- the type of objectkeyedFactory
- the KeyedPoolableObjectFactory
to delegate to.PoolableObjectFactory
that delegates to keyedFactory
with an internal key.IllegalArgumentException
- when keyedFactory
is null
.adapt(KeyedPoolableObjectFactory, Object)
public static <K,V> PoolableObjectFactory<V> adapt(KeyedPoolableObjectFactory<K,V> keyedFactory, K key) throws IllegalArgumentException
KeyedPoolableObjectFactory
instance to work where a PoolableObjectFactory
is
needed using the specified key
when delegating.K
- the type of keyV
- the type of objectkeyedFactory
- the KeyedPoolableObjectFactory
to delegate to.key
- the key to use when delegating.PoolableObjectFactory
that delegates to keyedFactory
with the specified key.IllegalArgumentException
- when keyedFactory
or key
is null
.adapt(KeyedPoolableObjectFactory)
public static <K,V> KeyedPoolableObjectFactory<K,V> adapt(PoolableObjectFactory<V> factory) throws IllegalArgumentException
PoolableObjectFactory
instance to work where a KeyedPoolableObjectFactory
is
needed. The key is ignored.K
- the type of keyV
- the type of objectfactory
- the PoolableObjectFactory
to delegate to.KeyedPoolableObjectFactory
that delegates to factory
ignoring the key.IllegalArgumentException
- when factory
is null
.public static <V> ObjectPool<V> adapt(KeyedObjectPool<Object,V> keyedPool) throws IllegalArgumentException
KeyedObjectPool
instance to work where an ObjectPool
is needed. This is the
equivalent of calling PoolUtils.adapt(aKeyedObjectPool, new Object())
.V
- the type of objectkeyedPool
- the KeyedObjectPool
to delegate to.ObjectPool
that delegates to keyedPool
with an internal key.IllegalArgumentException
- when keyedPool
is null
.adapt(KeyedObjectPool, Object)
public static <V> ObjectPool<V> adapt(KeyedObjectPool<Object,V> keyedPool, Object key) throws IllegalArgumentException
KeyedObjectPool
instance to work where an ObjectPool
is needed using the
specified key
when delegating.V
- the type of objectkeyedPool
- the KeyedObjectPool
to delegate to.key
- the key to use when delegating.ObjectPool
that delegates to keyedPool
with the specified key.IllegalArgumentException
- when keyedPool
or key
is null
.adapt(KeyedObjectPool)
public static <K,V> KeyedObjectPool<K,V> adapt(ObjectPool<V> pool) throws IllegalArgumentException
ObjectPool
to work where an KeyedObjectPool
is needed.
The key is ignored.K
- the type of keyV
- the type of objectpool
- the ObjectPool
to delegate to.KeyedObjectPool
that delegates to pool
ignoring the key.IllegalArgumentException
- when pool
is null
.public static <T> ObjectPool<T> checkedPool(ObjectPool<T> pool, Class<T> type)
ObjectPool
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 type type
a ClassCastException
will be thrown.T
- the type of objectpool
- the pool to enforce type safety ontype
- the class type to enforce.ObjectPool
that will only allow objects of type
public static <K,V> KeyedObjectPool<K,V> checkedPool(KeyedObjectPool<K,V> keyedPool, Class<V> type)
KeyedObjectPool
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 type type
a ClassCastException
will be thrown.K
- the type of keyV
- the type of objectkeyedPool
- the keyedPool to enforce type safety ontype
- the class type to enforce.KeyedObjectPool
that will only allow objects of type
public static <T> TimerTask checkMinIdle(ObjectPool<T> pool, int minIdle, long period) throws IllegalArgumentException
ObjectPool.addObject()
then no more checks will be performed.T
- the type of objectpool
- the pool to check periodically.minIdle
- if the ObjectPool.getNumIdle()
is less than this then add an idle object.period
- the frequency to check the number of idle objects in a pool, see
Timer.schedule(TimerTask, long, long)
.TimerTask
that will periodically check the pools idle object count.IllegalArgumentException
- when pool
is null
or
when minIdle
is negative or when period
isn't
valid for Timer.schedule(TimerTask, long, long)
.public static <K,V> TimerTask checkMinIdle(KeyedObjectPool<K,V> keyedPool, K key, int minIdle, long period) throws IllegalArgumentException
KeyedObjectPool.addObject(Object)
then no more checks for that key
will be performed.K
- the type of keyV
- the type of objectkeyedPool
- the keyedPool to check periodically.key
- the key to check the idle count of.minIdle
- if the KeyedObjectPool.getNumIdle(Object)
is less than this then add an idle object.period
- the frequency to check the number of idle objects in a keyedPool, see
Timer.schedule(TimerTask, long, long)
.TimerTask
that will periodically check the pools idle object count.IllegalArgumentException
- when keyedPool
, key
is null
or
when minIdle
is negative or when period
isn't
valid for Timer.schedule(TimerTask, long, long)
.public static <K,V> Map<K,TimerTask> checkMinIdle(KeyedObjectPool<K,V> keyedPool, Collection<? extends K> keys, int minIdle, long period) throws IllegalArgumentException
Collection
keys
in the keyedPool.
At most one idle object will be added per period.K
- the type of keyV
- the type of objectkeyedPool
- the keyedPool to check periodically.keys
- a collection of keys to check the idle object count.minIdle
- if the KeyedObjectPool.getNumIdle(Object)
is less than this then add an idle object.period
- the frequency to check the number of idle objects in a keyedPool, see
Timer.schedule(TimerTask, long, long)
.Map
of key and TimerTask
pairs that will periodically check the pools idle object count.IllegalArgumentException
- when keyedPool
, keys
, or any of the values in the
collection is null
or when minIdle
is negative or when period
isn't
valid for Timer.schedule(TimerTask, long, long)
.checkMinIdle(KeyedObjectPool, Object, int, long)
public static <T> void prefill(ObjectPool<T> pool, int count) throws Exception, IllegalArgumentException
addObject()
on pool
count
number of times.T
- the type of objectpool
- the pool to prefill.count
- the number of idle objects to add.Exception
- when ObjectPool.addObject()
fails.IllegalArgumentException
- when pool
is null
.public static <K,V> void prefill(KeyedObjectPool<K,V> keyedPool, K key, int count) throws Exception, IllegalArgumentException
addObject(Object)
on keyedPool
with key
count
number of times.K
- the type of keyV
- the type of objectkeyedPool
- the keyedPool to prefill.key
- the key to add objects for.count
- the number of idle objects to add for key
.Exception
- when KeyedObjectPool.addObject(Object)
fails.IllegalArgumentException
- when keyedPool
or key
is null
.public static <K,V> void prefill(KeyedObjectPool<K,V> keyedPool, Collection<? extends K> keys, int count) throws Exception, IllegalArgumentException
addObject(Object)
on keyedPool
with each key in keys
for
count
number of times. This has the same effect as calling
prefill(KeyedObjectPool, Object, int)
for each key in the keys
collection.K
- the type of keyV
- the type of objectkeyedPool
- the keyedPool to prefill.keys
- Collection
of keys to add objects for.count
- the number of idle objects to add for each key
.Exception
- when KeyedObjectPool.addObject(Object)
fails.IllegalArgumentException
- when keyedPool
, keys
, or
any value in keys
is null
.prefill(KeyedObjectPool, Object, int)
public static <T> ObjectPool<T> synchronizedPool(ObjectPool<T> pool)
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.
T
- the type of objectpool
- the ObjectPool to be "wrapped" in a synchronized ObjectPool.public static <K,V> KeyedObjectPool<K,V> synchronizedPool(KeyedObjectPool<K,V> keyedPool)
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.
K
- the type of keyV
- the type of objectkeyedPool
- the KeyedObjectPool to be "wrapped" in a synchronized KeyedObjectPool.public static <T> PoolableObjectFactory<T> synchronizedPoolableFactory(PoolableObjectFactory<T> factory)
T
- the type of objectfactory
- the PoolableObjectFactory to be "wrapped" in a synchronized PoolableObjectFactory.public static <K,V> KeyedPoolableObjectFactory<K,V> synchronizedPoolableFactory(KeyedPoolableObjectFactory<K,V> keyedFactory)
K
- the type of keyV
- the type of objectkeyedFactory
- the KeyedPoolableObjectFactory to be "wrapped" in a synchronized KeyedPoolableObjectFactory.public static <T> ObjectPool<T> erodingPool(ObjectPool<T> pool)
T
- the type of objectpool
- the ObjectPool to be decorated so it shrinks it's idle count when possible.erodingPool(ObjectPool, float)
public static <T> ObjectPool<T> erodingPool(ObjectPool<T> pool, float factor)
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.
T
- the type of objectpool
- 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.erodingPool(ObjectPool)
public static <K,V> KeyedObjectPool<K,V> erodingPool(KeyedObjectPool<K,V> keyedPool)
K
- the type of keyV
- the type of objectkeyedPool
- the KeyedObjectPool to be decorated so it shrinks it's idle count when
possible.erodingPool(KeyedObjectPool, float)
,
erodingPool(KeyedObjectPool, float, boolean)
public static <K,V> KeyedObjectPool<K,V> erodingPool(KeyedObjectPool<K,V> keyedPool, float factor)
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.
K
- the type of keyV
- the type of objectkeyedPool
- 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.erodingPool(KeyedObjectPool, float, boolean)
public static <K,V> KeyedObjectPool<K,V> erodingPool(KeyedObjectPool<K,V> keyedPool, float factor, boolean perKey)
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.
K
- the type of keyV
- the type of objectkeyedPool
- 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.erodingPool(KeyedObjectPool)
,
erodingPool(KeyedObjectPool, float)
Copyright © 2000-2021 Apache Software Foundation. All Rights Reserved.