T - Type of element pooled in this pool.
This class is intended to be thread-safe.public abstract class BaseGenericObjectPool<T>
extends java.lang.Object
GenericObjectPool
and GenericKeyedObjectPool. The primary reason this class exists is
reduce code duplication between the two pool implementations.| Modifier and Type | Class and Description |
|---|---|
(package private) class |
BaseGenericObjectPool.EvictionIterator
The idle object eviction iterator.
|
(package private) class |
BaseGenericObjectPool.Evictor
The idle object evictor
TimerTask. |
(package private) static class |
BaseGenericObjectPool.IdentityWrapper<T>
Wrapper for objects under management by the pool.
|
private class |
BaseGenericObjectPool.StatsStore
Maintains a cache of values for a single metric and reports
statistics on the cached values.
|
| Modifier and Type | Field and Description |
|---|---|
private BaseGenericObjectPool.StatsStore |
activeTimes |
private boolean |
blockWhenExhausted |
private java.util.concurrent.atomic.AtomicLong |
borrowedCount |
(package private) boolean |
closed |
(package private) java.lang.Object |
closeLock |
(package private) java.util.concurrent.atomic.AtomicLong |
createdCount |
private java.lang.String |
creationStackTrace |
(package private) java.util.concurrent.atomic.AtomicLong |
destroyedByBorrowValidationCount |
(package private) java.util.concurrent.atomic.AtomicLong |
destroyedByEvictorCount |
(package private) java.util.concurrent.atomic.AtomicLong |
destroyedCount |
(package private) BaseGenericObjectPool.EvictionIterator |
evictionIterator |
(package private) java.lang.Object |
evictionLock |
private EvictionPolicy<T> |
evictionPolicy |
private BaseGenericObjectPool.Evictor |
evictor |
private java.lang.ref.WeakReference<java.lang.ClassLoader> |
factoryClassLoader |
private boolean |
fairness |
private BaseGenericObjectPool.StatsStore |
idleTimes |
private boolean |
lifo |
private java.util.concurrent.atomic.AtomicLong |
maxBorrowWaitTimeMillis |
private int |
maxTotal |
private long |
maxWaitMillis |
static int |
MEAN_TIMING_STATS_CACHE_SIZE
The size of the caches used to store historical data for some attributes
so that rolling means may be calculated.
|
private long |
minEvictableIdleTimeMillis |
private int |
numTestsPerEvictionRun |
private javax.management.ObjectName |
oname |
private java.util.concurrent.atomic.AtomicLong |
returnedCount |
private long |
softMinEvictableIdleTimeMillis |
private SwallowedExceptionListener |
swallowedExceptionListener |
private boolean |
testOnBorrow |
private boolean |
testOnCreate |
private boolean |
testOnReturn |
private boolean |
testWhileIdle |
private long |
timeBetweenEvictionRunsMillis |
private BaseGenericObjectPool.StatsStore |
waitTimes |
| Constructor and Description |
|---|
BaseGenericObjectPool(BaseObjectPoolConfig config,
java.lang.String jmxNameBase,
java.lang.String jmxNamePrefix)
Handles JMX registration (if required) and the initialization required for
monitoring.
|
| Modifier and Type | Method and Description |
|---|---|
(package private) void |
assertOpen()
Verifies that the pool is open.
|
abstract void |
close()
Closes the pool, destroys the remaining idle objects and, if registered
in JMX, deregisters it.
|
(package private) abstract void |
ensureMinIdle()
Tries to ensure that the configured minimum number of idle instances are
available in the pool.
|
abstract void |
evict()
Perform
numTests idle object eviction tests, evicting
examined objects that meet the criteria for eviction. |
boolean |
getBlockWhenExhausted()
Returns whether to block when the
borrowObject() method is
invoked when the pool is exhausted (the maximum number of "active"
objects has been reached). |
long |
getBorrowedCount()
The total number of objects successfully borrowed from this pool over the
lifetime of the pool.
|
long |
getCreatedCount()
The total number of objects created for this pool over the lifetime of
the pool.
|
java.lang.String |
getCreationStackTrace()
Provides the stack trace for the call that created this pool.
|
long |
getDestroyedByBorrowValidationCount()
The total number of objects destroyed by this pool as a result of failing
validation during
borrowObject() over the lifetime of the
pool. |
long |
getDestroyedByEvictorCount()
The total number of objects destroyed by the evictor associated with this
pool over the lifetime of the pool.
|
long |
getDestroyedCount()
The total number of objects destroyed by this pool over the lifetime of
the pool.
|
protected EvictionPolicy<T> |
getEvictionPolicy()
Returns the
EvictionPolicy defined for this pool. |
java.lang.String |
getEvictionPolicyClassName()
Returns the name of the
EvictionPolicy implementation that is
used by this pool. |
boolean |
getFairness()
Returns whether or not the pool serves threads waiting to borrow objects fairly.
|
javax.management.ObjectName |
getJmxName()
Provides the name under which the pool has been registered with the
platform MBean server or
null if the pool has not been
registered. |
boolean |
getLifo()
Returns whether the pool has LIFO (last in, first out) behaviour with
respect to idle objects - always returning the most recently used object
from the pool, or as a FIFO (first in, first out) queue, where the pool
always returns the oldest object in the idle object pool.
|
long |
getMaxBorrowWaitTimeMillis()
The maximum time a thread has waited to borrow objects from the pool.
|
int |
getMaxTotal()
Returns the maximum number of objects that can be allocated by the pool
(checked out to clients, or idle awaiting checkout) at a given time.
|
long |
getMaxWaitMillis()
Returns the maximum amount of time (in milliseconds) the
borrowObject() method should block before throwing an
exception when the pool is exhausted and
getBlockWhenExhausted() is true. |
long |
getMeanActiveTimeMillis()
The mean time objects are active for based on the last
MEAN_TIMING_STATS_CACHE_SIZE objects returned to the pool. |
long |
getMeanBorrowWaitTimeMillis()
The mean time threads wait to borrow an object based on the last
MEAN_TIMING_STATS_CACHE_SIZE objects borrowed from the pool. |
long |
getMeanIdleTimeMillis()
The mean time objects are idle for based on the last
MEAN_TIMING_STATS_CACHE_SIZE objects borrowed from the pool. |
long |
getMinEvictableIdleTimeMillis()
Returns the minimum amount of time an object may sit idle in the pool
before it is eligible for eviction by the idle object evictor (if any -
see
setTimeBetweenEvictionRunsMillis(long)). |
abstract int |
getNumIdle()
The number of instances currently idle in this pool.
|
int |
getNumTestsPerEvictionRun()
Returns the maximum number of objects to examine during each run (if any)
of the idle object evictor thread.
|
long |
getReturnedCount()
The total number of objects returned to this pool over the lifetime of
the pool.
|
long |
getSoftMinEvictableIdleTimeMillis()
Returns the minimum amount of time an object may sit idle in the pool
before it is eligible for eviction by the idle object evictor (if any -
see
setTimeBetweenEvictionRunsMillis(long)),
with the extra condition that at least minIdle object
instances remain in the pool. |
private java.lang.String |
getStackTrace(java.lang.Exception e)
Gets the stack trace of an exception as a string.
|
SwallowedExceptionListener |
getSwallowedExceptionListener()
The listener used (if any) to receive notifications of exceptions
unavoidably swallowed by the pool.
|
boolean |
getTestOnBorrow()
Returns whether objects borrowed from the pool will be validated before
being returned from the
borrowObject() method. |
boolean |
getTestOnCreate()
Returns whether objects created for the pool will be validated before
being returned from the
borrowObject() method. |
boolean |
getTestOnReturn()
Returns whether objects borrowed from the pool will be validated when
they are returned to the pool via the
returnObject() method. |
boolean |
getTestWhileIdle()
Returns whether objects sitting idle in the pool will be validated by the
idle object evictor (if any - see
setTimeBetweenEvictionRunsMillis(long)). |
long |
getTimeBetweenEvictionRunsMillis()
Returns the number of milliseconds to sleep between runs of the idle
object evictor thread.
|
boolean |
isClosed()
Has this pool instance been closed.
|
private javax.management.ObjectName |
jmxRegister(BaseObjectPoolConfig config,
java.lang.String jmxNameBase,
java.lang.String jmxNamePrefix)
Registers the pool with the platform MBean server.
|
(package private) void |
jmxUnregister()
Unregisters this pool's MBean.
|
void |
setBlockWhenExhausted(boolean blockWhenExhausted)
Sets whether to block when the
borrowObject() method is
invoked when the pool is exhausted (the maximum number of "active"
objects has been reached). |
void |
setEvictionPolicyClassName(java.lang.String evictionPolicyClassName)
Sets the name of the
EvictionPolicy implementation that is
used by this pool. |
void |
setLifo(boolean lifo)
Sets whether the pool has LIFO (last in, first out) behaviour with
respect to idle objects - always returning the most recently used object
from the pool, or as a FIFO (first in, first out) queue, where the pool
always returns the oldest object in the idle object pool.
|
void |
setMaxTotal(int maxTotal)
Sets the cap on the number of objects that can be allocated by the pool
(checked out to clients, or idle awaiting checkout) at a given time.
|
void |
setMaxWaitMillis(long maxWaitMillis)
Sets the maximum amount of time (in milliseconds) the
borrowObject() method should block before throwing an
exception when the pool is exhausted and
getBlockWhenExhausted() is true. |
void |
setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
Sets the minimum amount of time an object may sit idle in the pool
before it is eligible for eviction by the idle object evictor (if any -
see
setTimeBetweenEvictionRunsMillis(long)). |
void |
setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
Sets the maximum number of objects to examine during each run (if any)
of the idle object evictor thread.
|
void |
setSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis)
Sets the minimum amount of time an object may sit idle in the pool
before it is eligible for eviction by the idle object evictor (if any -
see
setTimeBetweenEvictionRunsMillis(long)),
with the extra condition that at least minIdle object
instances remain in the pool. |
void |
setSwallowedExceptionListener(SwallowedExceptionListener swallowedExceptionListener)
The listener used (if any) to receive notifications of exceptions
unavoidably swallowed by the pool.
|
void |
setTestOnBorrow(boolean testOnBorrow)
Sets whether objects borrowed from the pool will be validated before
being returned from the
borrowObject() method. |
void |
setTestOnCreate(boolean testOnCreate)
Sets whether objects created for the pool will be validated before
being returned from the
borrowObject() method. |
void |
setTestOnReturn(boolean testOnReturn)
Sets whether objects borrowed from the pool will be validated when
they are returned to the pool via the
returnObject() method. |
void |
setTestWhileIdle(boolean testWhileIdle)
Returns whether objects sitting idle in the pool will be validated by the
idle object evictor (if any - see
setTimeBetweenEvictionRunsMillis(long)). |
void |
setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
Sets the number of milliseconds to sleep between runs of the idle
object evictor thread.
|
(package private) void |
startEvictor(long delay)
Starts the evictor with the given delay.
|
(package private) void |
swallowException(java.lang.Exception e)
Swallows an exception and notifies the configured listener for swallowed
exceptions queue.
|
(package private) void |
updateStatsBorrow(PooledObject<T> p,
long waitTime)
Updates statistics after an object is borrowed from the pool.
|
(package private) void |
updateStatsReturn(long activeTime)
Updates statistics after an object is returned to the pool.
|
public static final int MEAN_TIMING_STATS_CACHE_SIZE
private volatile int maxTotal
private volatile boolean blockWhenExhausted
private volatile long maxWaitMillis
private volatile boolean lifo
private final boolean fairness
private volatile boolean testOnCreate
private volatile boolean testOnBorrow
private volatile boolean testOnReturn
private volatile boolean testWhileIdle
private volatile long timeBetweenEvictionRunsMillis
private volatile int numTestsPerEvictionRun
private volatile long minEvictableIdleTimeMillis
private volatile long softMinEvictableIdleTimeMillis
private volatile EvictionPolicy<T> evictionPolicy
final java.lang.Object closeLock
volatile boolean closed
final java.lang.Object evictionLock
private BaseGenericObjectPool.Evictor evictor
BaseGenericObjectPool.EvictionIterator evictionIterator
private final java.lang.ref.WeakReference<java.lang.ClassLoader> factoryClassLoader
private final javax.management.ObjectName oname
private final java.lang.String creationStackTrace
private final java.util.concurrent.atomic.AtomicLong borrowedCount
private final java.util.concurrent.atomic.AtomicLong returnedCount
final java.util.concurrent.atomic.AtomicLong createdCount
final java.util.concurrent.atomic.AtomicLong destroyedCount
final java.util.concurrent.atomic.AtomicLong destroyedByEvictorCount
final java.util.concurrent.atomic.AtomicLong destroyedByBorrowValidationCount
private final BaseGenericObjectPool.StatsStore activeTimes
private final BaseGenericObjectPool.StatsStore idleTimes
private final BaseGenericObjectPool.StatsStore waitTimes
private final java.util.concurrent.atomic.AtomicLong maxBorrowWaitTimeMillis
private volatile SwallowedExceptionListener swallowedExceptionListener
public BaseGenericObjectPool(BaseObjectPoolConfig config, java.lang.String jmxNameBase, java.lang.String jmxNamePrefix)
config - Pool configurationjmxNameBase - The default base JMX name for the new pool unless
overridden by the configjmxNamePrefix - Prefix to be used for JMX name for the new poolpublic final int getMaxTotal()
setMaxTotal(int)public final void setMaxTotal(int maxTotal)
maxTotal - The cap on the total number of object instances managed
by the pool. Negative values mean that there is no limit
to the number of objects allocated by the pool.getMaxTotal()public final boolean getBlockWhenExhausted()
borrowObject() method is
invoked when the pool is exhausted (the maximum number of "active"
objects has been reached).true if borrowObject() should block
when the pool is exhaustedsetBlockWhenExhausted(boolean)public final void setBlockWhenExhausted(boolean blockWhenExhausted)
borrowObject() method is
invoked when the pool is exhausted (the maximum number of "active"
objects has been reached).blockWhenExhausted - true if
borrowObject() should block
when the pool is exhaustedgetBlockWhenExhausted()public final long getMaxWaitMillis()
borrowObject() method should block before throwing an
exception when the pool is exhausted and
getBlockWhenExhausted() is true. When less than 0, the
borrowObject() method may block indefinitely.borrowObject()
will block.setMaxWaitMillis(long),
setBlockWhenExhausted(boolean)public final void setMaxWaitMillis(long maxWaitMillis)
borrowObject() method should block before throwing an
exception when the pool is exhausted and
getBlockWhenExhausted() is true. When less than 0, the
borrowObject() method may block indefinitely.maxWaitMillis - the maximum number of milliseconds
borrowObject() will block or negative
for indefinitely.getMaxWaitMillis(),
setBlockWhenExhausted(boolean)public final boolean getLifo()
true if the pool is configured with LIFO behaviour
or false if the pool is configured with FIFO
behavioursetLifo(boolean)public final boolean getFairness()
true if waiting threads are to be served
by the pool in arrival orderpublic final void setLifo(boolean lifo)
lifo - true if the pool is to be configured with LIFO
behaviour or false if the pool is to be
configured with FIFO behaviourgetLifo()public final boolean getTestOnCreate()
borrowObject() method. Validation is
performed by the validateObject() method of the factory
associated with the pool. If the object fails to validate, then
borrowObject() will fail.true if newly created objects are validated before
being returned from the borrowObject() methodsetTestOnCreate(boolean)public final void setTestOnCreate(boolean testOnCreate)
borrowObject() method. Validation is
performed by the validateObject() method of the factory
associated with the pool. If the object fails to validate, then
borrowObject() will fail.testOnCreate - true if newly created objects should be
validated before being returned from the
borrowObject() methodgetTestOnCreate()public final boolean getTestOnBorrow()
borrowObject() method. Validation is
performed by the validateObject() method of the factory
associated with the pool. If the object fails to validate, it will be
removed from the pool and destroyed, and a new attempt will be made to
borrow an object from the pool.true if objects are validated before being returned
from the borrowObject() methodsetTestOnBorrow(boolean)public final void setTestOnBorrow(boolean testOnBorrow)
borrowObject() method. Validation is
performed by the validateObject() method of the factory
associated with the pool. If the object fails to validate, it will be
removed from the pool and destroyed, and a new attempt will be made to
borrow an object from the pool.testOnBorrow - true if objects should be validated
before being returned from the
borrowObject() methodgetTestOnBorrow()public final boolean getTestOnReturn()
returnObject() method.
Validation is performed by the validateObject() method of
the factory associated with the pool. Returning objects that fail validation
are destroyed rather then being returned the pool.true if objects are validated on return to
the pool via the returnObject() methodsetTestOnReturn(boolean)public final void setTestOnReturn(boolean testOnReturn)
returnObject() method.
Validation is performed by the validateObject() method of
the factory associated with the pool. Returning objects that fail validation
are destroyed rather then being returned the pool.testOnReturn - true if objects are validated on
return to the pool via the
returnObject() methodgetTestOnReturn()public final boolean getTestWhileIdle()
setTimeBetweenEvictionRunsMillis(long)). Validation is performed
by the validateObject() method of the factory associated
with the pool. If the object fails to validate, it will be removed from
the pool and destroyed.true if objects will be validated by the evictorsetTestWhileIdle(boolean),
setTimeBetweenEvictionRunsMillis(long)public final void setTestWhileIdle(boolean testWhileIdle)
setTimeBetweenEvictionRunsMillis(long)). Validation is performed
by the validateObject() method of the factory associated
with the pool. If the object fails to validate, it will be removed from
the pool and destroyed. Note that setting this property has no effect
unless the idle object evictor is enabled by setting
timeBetweenEvictionRunsMillis to a positive value.testWhileIdle - true so objects will be validated by the evictorgetTestWhileIdle(),
setTimeBetweenEvictionRunsMillis(long)public final long getTimeBetweenEvictionRunsMillis()
setTimeBetweenEvictionRunsMillis(long)public final void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
timeBetweenEvictionRunsMillis - number of milliseconds to sleep between evictor runsgetTimeBetweenEvictionRunsMillis()public final int getNumTestsPerEvictionRun()
ceil(getNumIdle()/
abs(getNumTestsPerEvictionRun())) which means that when the
value is -n roughly one nth of the idle objects will be
tested per run.setNumTestsPerEvictionRun(int),
setTimeBetweenEvictionRunsMillis(long)public final void setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
ceil(getNumIdle()/
abs(getNumTestsPerEvictionRun())) which means that when the
value is -n roughly one nth of the idle objects will be
tested per run.numTestsPerEvictionRun - max number of objects to examine during each evictor rungetNumTestsPerEvictionRun(),
setTimeBetweenEvictionRunsMillis(long)public final long getMinEvictableIdleTimeMillis()
setTimeBetweenEvictionRunsMillis(long)). When non-positive,
no objects will be evicted from the pool due to idle time alone.setMinEvictableIdleTimeMillis(long),
setTimeBetweenEvictionRunsMillis(long)public final void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
setTimeBetweenEvictionRunsMillis(long)). When non-positive,
no objects will be evicted from the pool due to idle time alone.minEvictableIdleTimeMillis - minimum amount of time an object may sit idle in the pool
before it is eligible for evictiongetMinEvictableIdleTimeMillis(),
setTimeBetweenEvictionRunsMillis(long)public final long getSoftMinEvictableIdleTimeMillis()
setTimeBetweenEvictionRunsMillis(long)),
with the extra condition that at least minIdle object
instances remain in the pool. This setting is overridden by
getMinEvictableIdleTimeMillis() (that is, if
getMinEvictableIdleTimeMillis() is positive, then
getSoftMinEvictableIdleTimeMillis() is ignored).setSoftMinEvictableIdleTimeMillis(long)public final void setSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis)
setTimeBetweenEvictionRunsMillis(long)),
with the extra condition that at least minIdle object
instances remain in the pool. This setting is overridden by
getMinEvictableIdleTimeMillis() (that is, if
getMinEvictableIdleTimeMillis() is positive, then
getSoftMinEvictableIdleTimeMillis() is ignored).softMinEvictableIdleTimeMillis - minimum amount of time an object may sit idle in the pool
before it is eligible for eviction if minIdle instances are
availablegetSoftMinEvictableIdleTimeMillis()public final java.lang.String getEvictionPolicyClassName()
EvictionPolicy implementation that is
used by this pool.EvictionPolicysetEvictionPolicyClassName(String)public final void setEvictionPolicyClassName(java.lang.String evictionPolicyClassName)
EvictionPolicy implementation that is
used by this pool. The Pool will attempt to load the class using the
thread context class loader. If that fails, the Pool will attempt to load
the class using the class loader that loaded this class.evictionPolicyClassName - the fully qualified class name of the
new eviction policygetEvictionPolicyClassName()public abstract void close()
public final boolean isClosed()
true when this pool has been closed.public abstract void evict()
throws java.lang.Exception
Perform numTests idle object eviction tests, evicting
examined objects that meet the criteria for eviction. If
testWhileIdle is true, examined objects are validated
when visited (and removed if invalid); otherwise only objects that
have been idle for more than minEvicableIdleTimeMillis
are removed.
java.lang.Exception - when there is a problem evicting idle objects.protected EvictionPolicy<T> getEvictionPolicy()
EvictionPolicy defined for this pool.final void assertOpen()
throws java.lang.IllegalStateException
java.lang.IllegalStateException - if the pool is closed.final void startEvictor(long delay)
Starts the evictor with the given delay. If there is an evictor running when this method is called, it is stopped and replaced with a new evictor with the specified delay.
This method needs to be final, since it is called from a constructor. See POOL-195.
delay - time in milliseconds before start and between eviction runsabstract void ensureMinIdle()
throws java.lang.Exception
java.lang.Exception - if an error occurs creating idle instancespublic final javax.management.ObjectName getJmxName()
null if the pool has not been
registered.public final java.lang.String getCreationStackTrace()
close() method.
This method is provided to assist with identifying code that creates but
does not close it thereby creating a memory leak.public final long getBorrowedCount()
public final long getReturnedCount()
public final long getCreatedCount()
public final long getDestroyedCount()
public final long getDestroyedByEvictorCount()
public final long getDestroyedByBorrowValidationCount()
borrowObject() over the lifetime of the
pool.public final long getMeanActiveTimeMillis()
MEAN_TIMING_STATS_CACHE_SIZE objects returned to the pool.public final long getMeanIdleTimeMillis()
MEAN_TIMING_STATS_CACHE_SIZE objects borrowed from the pool.public final long getMeanBorrowWaitTimeMillis()
MEAN_TIMING_STATS_CACHE_SIZE objects borrowed from the pool.public final long getMaxBorrowWaitTimeMillis()
public abstract int getNumIdle()
public final SwallowedExceptionListener getSwallowedExceptionListener()
null for no listenerpublic final void setSwallowedExceptionListener(SwallowedExceptionListener swallowedExceptionListener)
swallowedExceptionListener - The listener or null
for no listenerfinal void swallowException(java.lang.Exception e)
e - exception to be swallowedfinal void updateStatsBorrow(PooledObject<T> p, long waitTime)
p - object borrowed from the poolwaitTime - time (in milliseconds) that the borrowing thread had to waitfinal void updateStatsReturn(long activeTime)
activeTime - the amount of time (in milliseconds) that the returning
object was checked outfinal void jmxUnregister()
private javax.management.ObjectName jmxRegister(BaseObjectPoolConfig config, java.lang.String jmxNameBase, java.lang.String jmxNamePrefix)
jmxNameBase + jmxNamePrefix + i where i is the least
integer greater than or equal to 1 such that the name is not already
registered. Swallows MBeanRegistrationException, NotCompliantMBeanException
returning null.config - Pool configurationjmxNameBase - default base JMX name for this pooljmxNamePrefix - name prefixprivate java.lang.String getStackTrace(java.lang.Exception e)
e - exception to trace