Constructor: |
public GenericObjectPool() {
this(null, DEFAULT_MAX_ACTIVE, DEFAULT_WHEN_EXHAUSTED_ACTION, DEFAULT_MAX_WAIT, DEFAULT_MAX_IDLE,
DEFAULT_MIN_IDLE, DEFAULT_TEST_ON_BORROW, DEFAULT_TEST_ON_RETURN, DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
DEFAULT_NUM_TESTS_PER_EVICTION_RUN, DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS, DEFAULT_TEST_WHILE_IDLE);
}
Create a new GenericObjectPool with default properties. |
public GenericObjectPool(PoolableObjectFactory factory) {
this(factory, DEFAULT_MAX_ACTIVE, DEFAULT_WHEN_EXHAUSTED_ACTION, DEFAULT_MAX_WAIT, DEFAULT_MAX_IDLE,
DEFAULT_MIN_IDLE, DEFAULT_TEST_ON_BORROW, DEFAULT_TEST_ON_RETURN, DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
DEFAULT_NUM_TESTS_PER_EVICTION_RUN, DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS, DEFAULT_TEST_WHILE_IDLE);
}
Create a new GenericObjectPool using the specified factory. Parameters:
factory - the (possibly null)PoolableObjectFactory to use to create, validate and destroy objects
|
public GenericObjectPool(PoolableObjectFactory factory,
Config config) {
this(factory, config.maxActive, config.whenExhaustedAction, config.maxWait, config.maxIdle, config.minIdle,
config.testOnBorrow, config.testOnReturn, config.timeBetweenEvictionRunsMillis,
config.numTestsPerEvictionRun, config.minEvictableIdleTimeMillis, config.testWhileIdle,
config.softMinEvictableIdleTimeMillis, config.lifo);
}
Create a new GenericObjectPool using the specified values. Parameters:
factory - the (possibly null)PoolableObjectFactory to use to create, validate and destroy objects
config - a non-null GenericObjectPool.Config describing my configuration
|
public GenericObjectPool(PoolableObjectFactory factory,
int maxActive) {
this(factory, maxActive, DEFAULT_WHEN_EXHAUSTED_ACTION, DEFAULT_MAX_WAIT, DEFAULT_MAX_IDLE, DEFAULT_MIN_IDLE,
DEFAULT_TEST_ON_BORROW, DEFAULT_TEST_ON_RETURN, DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
DEFAULT_NUM_TESTS_PER_EVICTION_RUN, DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS, DEFAULT_TEST_WHILE_IDLE);
}
Create a new GenericObjectPool using the specified values. Parameters:
factory - the (possibly null)PoolableObjectFactory to use to create, validate and destroy objects
maxActive - the maximum number of objects that can be borrowed from me at one time (see #setMaxActive )
|
public GenericObjectPool(PoolableObjectFactory factory,
int maxActive,
byte whenExhaustedAction,
long maxWait) {
this(factory, maxActive, whenExhaustedAction, maxWait, DEFAULT_MAX_IDLE, DEFAULT_MIN_IDLE, DEFAULT_TEST_ON_BORROW,
DEFAULT_TEST_ON_RETURN, DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS, DEFAULT_NUM_TESTS_PER_EVICTION_RUN,
DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS, DEFAULT_TEST_WHILE_IDLE);
}
Create a new GenericObjectPool using the specified values. Parameters:
factory - the (possibly null)PoolableObjectFactory to use to create, validate and destroy objects
maxActive - the maximum number of objects that can be borrowed from me at one time (see #setMaxActive )
whenExhaustedAction - the action to take when the pool is exhausted (see #getWhenExhaustedAction )
maxWait - the maximum amount of time to wait for an idle object when the pool is exhausted an and
whenExhaustedAction is #WHEN_EXHAUSTED_BLOCK (otherwise ignored) (see #getMaxWait )
|
public GenericObjectPool(PoolableObjectFactory factory,
int maxActive,
byte whenExhaustedAction,
long maxWait,
int maxIdle) {
this(factory, maxActive, whenExhaustedAction, maxWait, maxIdle, DEFAULT_MIN_IDLE, DEFAULT_TEST_ON_BORROW,
DEFAULT_TEST_ON_RETURN, DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS, DEFAULT_NUM_TESTS_PER_EVICTION_RUN,
DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS, DEFAULT_TEST_WHILE_IDLE);
}
Create a new GenericObjectPool using the specified values. Parameters:
factory - the (possibly null)PoolableObjectFactory to use to create, validate and destroy objects
maxActive - the maximum number of objects that can be borrowed at one time (see #setMaxActive )
whenExhaustedAction - the action to take when the pool is exhausted (see #getWhenExhaustedAction )
maxWait - the maximum amount of time to wait for an idle object when the pool is exhausted and
whenExhaustedAction is #WHEN_EXHAUSTED_BLOCK (otherwise ignored) (see #getMaxWait )
maxIdle - the maximum number of idle objects in my pool (see #getMaxIdle )
|
public GenericObjectPool(PoolableObjectFactory factory,
int maxActive,
byte whenExhaustedAction,
long maxWait,
boolean testOnBorrow,
boolean testOnReturn) {
this(factory, maxActive, whenExhaustedAction, maxWait, DEFAULT_MAX_IDLE, DEFAULT_MIN_IDLE, testOnBorrow,
testOnReturn, DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS, DEFAULT_NUM_TESTS_PER_EVICTION_RUN,
DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS, DEFAULT_TEST_WHILE_IDLE);
}
Create a new GenericObjectPool using the specified values. Parameters:
factory - the (possibly null)PoolableObjectFactory to use to create, validate and destroy objects
maxActive - the maximum number of objects that can be borrowed at one time (see #setMaxActive )
whenExhaustedAction - the action to take when the pool is exhausted (see #getWhenExhaustedAction )
maxWait - the maximum amount of time to wait for an idle object when the pool is exhausted an and
whenExhaustedAction is #WHEN_EXHAUSTED_BLOCK (otherwise ignored) (see #getMaxWait )
testOnBorrow - whether or not to validate objects before they are returned by the #borrowObject method
(see #getTestOnBorrow )
testOnReturn - whether or not to validate objects after they are returned to the #returnObject method
(see #getTestOnReturn )
|
public GenericObjectPool(PoolableObjectFactory factory,
int maxActive,
byte whenExhaustedAction,
long maxWait,
int maxIdle,
boolean testOnBorrow,
boolean testOnReturn) {
this(factory, maxActive, whenExhaustedAction, maxWait, maxIdle, DEFAULT_MIN_IDLE, testOnBorrow, testOnReturn,
DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS, DEFAULT_NUM_TESTS_PER_EVICTION_RUN,
DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS, DEFAULT_TEST_WHILE_IDLE);
}
Create a new GenericObjectPool using the specified values. Parameters:
factory - the (possibly null)PoolableObjectFactory to use to create, validate and destroy objects
maxActive - the maximum number of objects that can be borrowed at one time (see #setMaxActive )
whenExhaustedAction - the action to take when the pool is exhausted (see #getWhenExhaustedAction )
maxWait - the maximum amount of time to wait for an idle object when the pool is exhausted and
whenExhaustedAction is #WHEN_EXHAUSTED_BLOCK (otherwise ignored) (see #getMaxWait )
maxIdle - the maximum number of idle objects in my pool (see #getMaxIdle )
testOnBorrow - whether or not to validate objects before they are returned by the #borrowObject method
(see #getTestOnBorrow )
testOnReturn - whether or not to validate objects after they are returned to the #returnObject method
(see #getTestOnReturn )
|
public GenericObjectPool(PoolableObjectFactory factory,
int maxActive,
byte whenExhaustedAction,
long maxWait,
int maxIdle,
boolean testOnBorrow,
boolean testOnReturn,
long timeBetweenEvictionRunsMillis,
int numTestsPerEvictionRun,
long minEvictableIdleTimeMillis,
boolean testWhileIdle) {
this(factory, maxActive, whenExhaustedAction, maxWait, maxIdle, DEFAULT_MIN_IDLE, testOnBorrow, testOnReturn,
timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle);
}
Create a new GenericObjectPool using the specified values. Parameters:
factory - the (possibly null)PoolableObjectFactory to use to create, validate and destroy objects
maxActive - the maximum number of objects that can be borrowed at one time (see #setMaxActive )
whenExhaustedAction - the action to take when the pool is exhausted (see #setWhenExhaustedAction )
maxWait - the maximum amount of time to wait for an idle object when the pool is exhausted and
whenExhaustedAction is #WHEN_EXHAUSTED_BLOCK (otherwise ignored) (see #setMaxWait )
maxIdle - the maximum number of idle objects in my pool (see #setMaxIdle )
testOnBorrow - whether or not to validate objects before they are returned by the #borrowObject
method (see #setTestOnBorrow )
testOnReturn - whether or not to validate objects after they are returned to the #returnObject method
(see #setTestOnReturn )
timeBetweenEvictionRunsMillis - the amount of time (in milliseconds) to sleep between examining idle objects
for eviction (see #setTimeBetweenEvictionRunsMillis )
numTestsPerEvictionRun - the number of idle objects to examine per run within the idle object eviction thread
(if any) (see #setNumTestsPerEvictionRun )
minEvictableIdleTimeMillis - the minimum number of milliseconds an object can sit idle in the pool before it
is eligible for eviction (see #setMinEvictableIdleTimeMillis )
testWhileIdle - whether or not to validate objects in the idle object eviction thread, if any
(see #setTestWhileIdle )
|
public GenericObjectPool(PoolableObjectFactory factory,
int maxActive,
byte whenExhaustedAction,
long maxWait,
int maxIdle,
int minIdle,
boolean testOnBorrow,
boolean testOnReturn,
long timeBetweenEvictionRunsMillis,
int numTestsPerEvictionRun,
long minEvictableIdleTimeMillis,
boolean testWhileIdle) {
this(factory, maxActive, whenExhaustedAction, maxWait, maxIdle, minIdle, testOnBorrow, testOnReturn,
timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle,
DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
}
Create a new GenericObjectPool using the specified values. Parameters:
factory - the (possibly null)PoolableObjectFactory to use to create, validate and destroy objects
maxActive - the maximum number of objects that can be borrowed at one time (see #setMaxActive )
whenExhaustedAction - the action to take when the pool is exhausted (see #setWhenExhaustedAction )
maxWait - the maximum amount of time to wait for an idle object when the pool is exhausted and
whenExhaustedAction is #WHEN_EXHAUSTED_BLOCK (otherwise ignored) (see #setMaxWait )
maxIdle - the maximum number of idle objects in my pool (see #setMaxIdle )
minIdle - the minimum number of idle objects in my pool (see #setMinIdle )
testOnBorrow - whether or not to validate objects before they are returned by the #borrowObject method
(see #setTestOnBorrow )
testOnReturn - whether or not to validate objects after they are returned to the #returnObject method
(see #setTestOnReturn )
timeBetweenEvictionRunsMillis - the amount of time (in milliseconds) to sleep between examining idle objects
for eviction (see #setTimeBetweenEvictionRunsMillis )
numTestsPerEvictionRun - the number of idle objects to examine per run within the idle object eviction thread
(if any) (see #setNumTestsPerEvictionRun )
minEvictableIdleTimeMillis - the minimum number of milliseconds an object can sit idle in the pool before
it is eligible for eviction (see #setMinEvictableIdleTimeMillis )
testWhileIdle - whether or not to validate objects in the idle object eviction thread, if any
(see #setTestWhileIdle )
|
public GenericObjectPool(PoolableObjectFactory factory,
int maxActive,
byte whenExhaustedAction,
long maxWait,
int maxIdle,
int minIdle,
boolean testOnBorrow,
boolean testOnReturn,
long timeBetweenEvictionRunsMillis,
int numTestsPerEvictionRun,
long minEvictableIdleTimeMillis,
boolean testWhileIdle,
long softMinEvictableIdleTimeMillis) {
this(factory, maxActive, whenExhaustedAction, maxWait, maxIdle, minIdle, testOnBorrow, testOnReturn,
timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle,
softMinEvictableIdleTimeMillis, DEFAULT_LIFO);
}
Create a new GenericObjectPool using the specified values. Parameters:
factory - the (possibly null)PoolableObjectFactory to use to create, validate and destroy objects
maxActive - the maximum number of objects that can be borrowed at one time (see #setMaxActive )
whenExhaustedAction - the action to take when the pool is exhausted (see #setWhenExhaustedAction )
maxWait - the maximum amount of time to wait for an idle object when the pool is exhausted and
whenExhaustedAction is #WHEN_EXHAUSTED_BLOCK (otherwise ignored) (see #setMaxWait )
maxIdle - the maximum number of idle objects in my pool (see #setMaxIdle )
minIdle - the minimum number of idle objects in my pool (see #setMinIdle )
testOnBorrow - whether or not to validate objects before they are returned by the #borrowObject
method (see #setTestOnBorrow )
testOnReturn - whether or not to validate objects after they are returned to the #returnObject
method (see #setTestOnReturn )
timeBetweenEvictionRunsMillis - the amount of time (in milliseconds) to sleep between examining idle objects
for eviction (see #setTimeBetweenEvictionRunsMillis )
numTestsPerEvictionRun - the number of idle objects to examine per run within the idle object eviction thread
(if any) (see #setNumTestsPerEvictionRun )
minEvictableIdleTimeMillis - the minimum number of milliseconds an object can sit idle in the pool before
it is eligible for eviction (see #setMinEvictableIdleTimeMillis )
testWhileIdle - whether or not to validate objects in the idle object eviction thread, if any
(see #setTestWhileIdle )
softMinEvictableIdleTimeMillis - the minimum number of milliseconds an object can sit idle in the pool before it is
eligible for eviction with the extra condition that at least "minIdle" amount of object remain in the pool.
(see #setSoftMinEvictableIdleTimeMillis )
- since:
Pool - 1.3
|
public GenericObjectPool(PoolableObjectFactory factory,
int maxActive,
byte whenExhaustedAction,
long maxWait,
int maxIdle,
int minIdle,
boolean testOnBorrow,
boolean testOnReturn,
long timeBetweenEvictionRunsMillis,
int numTestsPerEvictionRun,
long minEvictableIdleTimeMillis,
boolean testWhileIdle,
long softMinEvictableIdleTimeMillis,
boolean lifo) {
_factory = factory;
_maxActive = maxActive;
_lifo = lifo;
switch(whenExhaustedAction) {
case WHEN_EXHAUSTED_BLOCK:
case WHEN_EXHAUSTED_FAIL:
case WHEN_EXHAUSTED_GROW:
_whenExhaustedAction = whenExhaustedAction;
break;
default:
throw new IllegalArgumentException("whenExhaustedAction " + whenExhaustedAction + " not recognized.");
}
_maxWait = maxWait;
_maxIdle = maxIdle;
_minIdle = minIdle;
_testOnBorrow = testOnBorrow;
_testOnReturn = testOnReturn;
_timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
_numTestsPerEvictionRun = numTestsPerEvictionRun;
_minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
_softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis;
_testWhileIdle = testWhileIdle;
_pool = new CursorableLinkedList();
startEvictor(_timeBetweenEvictionRunsMillis);
}
Create a new GenericObjectPool using the specified values. Parameters:
factory - the (possibly null)PoolableObjectFactory to use to create, validate and destroy objects
maxActive - the maximum number of objects that can be borrowed at one time (see #setMaxActive )
whenExhaustedAction - the action to take when the pool is exhausted (see #setWhenExhaustedAction )
maxWait - the maximum amount of time to wait for an idle object when the pool is exhausted and
whenExhaustedAction is #WHEN_EXHAUSTED_BLOCK (otherwise ignored) (see #setMaxWait )
maxIdle - the maximum number of idle objects in my pool (see #setMaxIdle )
minIdle - the minimum number of idle objects in my pool (see #setMinIdle )
testOnBorrow - whether or not to validate objects before they are returned by the #borrowObject
method (see #setTestOnBorrow )
testOnReturn - whether or not to validate objects after they are returned to the #returnObject
method (see #setTestOnReturn )
timeBetweenEvictionRunsMillis - the amount of time (in milliseconds) to sleep between examining idle
objects for eviction (see #setTimeBetweenEvictionRunsMillis )
numTestsPerEvictionRun - the number of idle objects to examine per run within the idle object eviction
thread (if any) (see #setNumTestsPerEvictionRun )
minEvictableIdleTimeMillis - the minimum number of milliseconds an object can sit idle in the pool before
it is eligible for eviction (see #setMinEvictableIdleTimeMillis )
testWhileIdle - whether or not to validate objects in the idle object eviction thread, if any
(see #setTestWhileIdle )
softMinEvictableIdleTimeMillis - the minimum number of milliseconds an object can sit idle in the
pool before it is eligible for eviction with the extra condition that at least "minIdle" amount of object
remain in the pool. (see #setSoftMinEvictableIdleTimeMillis )
lifo - whether or not objects are returned in last-in-first-out order from the idle object pool
(see #setLifo )
- since:
Pool - 1.4
|
Method from org.apache.commons.pool.impl.GenericObjectPool Detail: |
public void addObject() throws Exception {
assertOpen();
if (_factory == null) {
throw new IllegalStateException("Cannot add objects without a factory.");
}
Object obj = _factory.makeObject();
try {
assertOpen();
addObjectToPool(obj, false);
} catch (IllegalStateException ex) { // Pool closed
try {
_factory.destroyObject(obj);
} catch (Exception ex2) {
// swallow
}
throw ex;
}
}
Create an object, and place it into the pool.
addObject() is useful for "pre-loading" a pool with idle objects. |
public Object borrowObject() throws Exception {
long starttime = System.currentTimeMillis();
Latch latch = new Latch();
byte whenExhaustedAction;
long maxWait;
synchronized (this) {
// Get local copy of current config. Can't sync when used later as
// it can result in a deadlock. Has the added advantage that config
// is consistent for entire method execution
whenExhaustedAction = _whenExhaustedAction;
maxWait = _maxWait;
// Add this request to the queue
_allocationQueue.add(latch);
// Work the allocation queue, allocating idle instances and
// instance creation permits in request arrival order
allocate();
}
for(;;) {
synchronized (this) {
assertOpen();
}
// If no object was allocated from the pool above
if(latch.getPair() == null) {
// check if we were allowed to create one
if(latch.mayCreate()) {
// allow new object to be created
} else {
// the pool is exhausted
switch(whenExhaustedAction) {
case WHEN_EXHAUSTED_GROW:
// allow new object to be created
synchronized (this) {
// Make sure another thread didn't allocate us an object
// or permit a new object to be created
if (latch.getPair() == null && !latch.mayCreate()) {
_allocationQueue.remove(latch);
_numInternalProcessing++;
}
}
break;
case WHEN_EXHAUSTED_FAIL:
synchronized (this) {
// Make sure allocate hasn't already assigned an object
// in a different thread or permitted a new object to be created
if (latch.getPair() != null || latch.mayCreate()) {
break;
}
_allocationQueue.remove(latch);
}
throw new NoSuchElementException("Pool exhausted");
case WHEN_EXHAUSTED_BLOCK:
try {
synchronized (latch) {
// Before we wait, make sure another thread didn't allocate us an object
// or permit a new object to be created
if (latch.getPair() == null && !latch.mayCreate()) {
if(maxWait < = 0) {
latch.wait();
} else {
// this code may be executed again after a notify then continue cycle
// so, need to calculate the amount of time to wait
final long elapsed = (System.currentTimeMillis() - starttime);
final long waitTime = maxWait - elapsed;
if (waitTime > 0)
{
latch.wait(waitTime);
}
}
} else {
break;
}
}
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
throw e;
}
if(maxWait > 0 && ((System.currentTimeMillis() - starttime) >= maxWait)) {
synchronized(this) {
// Make sure allocate hasn't already assigned an object
// in a different thread or permitted a new object to be created
if (latch.getPair() == null && !latch.mayCreate()) {
// Remove latch from the allocation queue
_allocationQueue.remove(latch);
} else {
break;
}
}
throw new NoSuchElementException("Timeout waiting for idle object");
} else {
continue; // keep looping
}
default:
throw new IllegalArgumentException("WhenExhaustedAction property " + whenExhaustedAction +
" not recognized.");
}
}
}
boolean newlyCreated = false;
if(null == latch.getPair()) {
try {
Object obj = _factory.makeObject();
latch.setPair(new ObjectTimestampPair(obj));
newlyCreated = true;
} finally {
if (!newlyCreated) {
// object cannot be created
synchronized (this) {
_numInternalProcessing--;
// No need to reset latch - about to throw exception
allocate();
}
}
}
}
// activate & validate the object
try {
_factory.activateObject(latch.getPair().value);
if(_testOnBorrow &&
!_factory.validateObject(latch.getPair().value)) {
throw new Exception("ValidateObject failed");
}
synchronized(this) {
_numInternalProcessing--;
_numActive++;
}
return latch.getPair().value;
}
catch (Throwable e) {
// object cannot be activated or is invalid
try {
_factory.destroyObject(latch.getPair().value);
} catch (Throwable e2) {
// cannot destroy broken object
}
synchronized (this) {
_numInternalProcessing--;
if (!newlyCreated) {
latch.reset();
_allocationQueue.add(0, latch);
}
allocate();
}
if(newlyCreated) {
throw new NoSuchElementException("Could not create a validated object, cause: " + e.getMessage());
}
else {
continue; // keep looping
}
}
}
}
Borrows an object from the pool.
If there is an idle instance available in the pool, then either the most-recently returned
(if lifo == true) or "oldest" (lifo == false) instance sitting idle in the pool
will be activated and returned. If activation fails, or testOnBorrow is set
to true and validation fails, the instance is destroyed and the next available instance is examined.
This continues until either a valid instance is returned or there are no more idle instances available.
If there are no idle instances available in the pool, behavior depends on the maxActive
and (if applicable) whenExhaustedAction and maxWait
properties. If the number of instances checked out from the pool is less than maxActive, a new
instance is created, activated and (if applicable) validated and returned to the caller.
If the pool is exhausted (no available idle instances and no capacity to create new ones),
this method will either block (#WHEN_EXHAUSTED_BLOCK ), throw a NoSuchElementException
(#WHEN_EXHAUSTED_FAIL ), or grow (#WHEN_EXHAUSTED_GROW - ignoring maxActive).
The length of time that this method will block when whenExhaustedAction == WHEN_EXHAUSTED_BLOCK
is determined by the maxWait property.
When the pool is exhausted, multiple calling threads may be simultaneously blocked waiting for instances
to become available. As of pool 1.5, a "fairness" algorithm has been implemented to ensure that threads receive
available instances in request arrival order.
|
public void clear() {
List toDestroy = new ArrayList();
synchronized(this) {
toDestroy.addAll(_pool);
_numInternalProcessing = _numInternalProcessing + _pool._size;
_pool.clear();
}
destroy(toDestroy);
}
Clears any objects sitting idle in the pool by removing them from the
idle instance pool and then invoking the configured
PoolableObjectFactory#destroyObject(Object) method on each idle
instance.
Implementation notes:
- This method does not destroy or effect in any way instances that are
checked out of the pool when it is invoked.
- Invoking this method does not prevent objects being
returned to the idle instance pool, even during its execution. It locks
the pool only during instance removal. Additional instances may be returned
while removed items are being destroyed.
|
public void close() throws Exception {
super.close();
synchronized (this) {
clear();
startEvictor(-1L);
}
}
|
synchronized String debugInfo() {
StringBuffer buf = new StringBuffer();
buf.append("Active: ").append(getNumActive()).append("\n");
buf.append("Idle: ").append(getNumIdle()).append("\n");
buf.append("Idle Objects:\n");
Iterator it = _pool.iterator();
long time = System.currentTimeMillis();
while(it.hasNext()) {
ObjectTimestampPair pair = (ObjectTimestampPair)(it.next());
buf.append("\t").append(pair.value).append("\t").append(time - pair.tstamp).append("\n");
}
return buf.toString();
}
|
public void evict() throws Exception {
assertOpen();
synchronized (this) {
if(_pool.isEmpty()) {
return;
}
if (null == _evictionCursor) {
_evictionCursor = (_pool.cursor(_lifo ? _pool.size() : 0));
}
}
for (int i=0,m=getNumTests();i< m;i++) {
final ObjectTimestampPair pair;
synchronized (this) {
if ((_lifo && !_evictionCursor.hasPrevious()) ||
!_lifo && !_evictionCursor.hasNext()) {
_evictionCursor.close();
_evictionCursor = _pool.cursor(_lifo ? _pool.size() : 0);
}
pair = _lifo ?
(ObjectTimestampPair) _evictionCursor.previous() :
(ObjectTimestampPair) _evictionCursor.next();
_evictionCursor.remove();
_numInternalProcessing++;
}
boolean removeObject = false;
final long idleTimeMilis = System.currentTimeMillis() - pair.tstamp;
if ((getMinEvictableIdleTimeMillis() > 0) &&
(idleTimeMilis > getMinEvictableIdleTimeMillis())) {
removeObject = true;
} else if ((getSoftMinEvictableIdleTimeMillis() > 0) &&
(idleTimeMilis > getSoftMinEvictableIdleTimeMillis()) &&
((getNumIdle() + 1) > getMinIdle())) { // +1 accounts for object we are processing
removeObject = true;
}
if(getTestWhileIdle() && !removeObject) {
boolean active = false;
try {
_factory.activateObject(pair.value);
active = true;
} catch(Exception e) {
removeObject=true;
}
if(active) {
if(!_factory.validateObject(pair.value)) {
removeObject=true;
} else {
try {
_factory.passivateObject(pair.value);
} catch(Exception e) {
removeObject=true;
}
}
}
}
if (removeObject) {
try {
_factory.destroyObject(pair.value);
} catch(Exception e) {
// ignored
}
}
synchronized (this) {
if(!removeObject) {
_evictionCursor.add(pair);
if (_lifo) {
// Skip over the element we just added back
_evictionCursor.previous();
}
}
_numInternalProcessing--;
}
}
}
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.
Successive activations of this method examine objects in
in sequence, cycling through objects in oldest-to-youngest order.
|
public synchronized boolean getLifo() {
return _lifo;
}
Whether or not the idle object pool acts as a LIFO queue. True means
that borrowObject returns the most recently used ("last in") idle object
in the pool (if there are idle instances available). False means that
the pool behaves as a FIFO queue - objects are taken from the idle object
pool in the order that they are returned to the pool. |
public synchronized int getMaxActive() {
return _maxActive;
}
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.
When non-positive, there is no limit to the number of objects that can
be managed by the pool at one time. |
public synchronized int getMaxIdle() {
return _maxIdle;
}
Returns the cap on the number of "idle" instances in the pool. |
public synchronized long getMaxWait() {
return _maxWait;
}
|
public synchronized long getMinEvictableIdleTimeMillis() {
return _minEvictableIdleTimeMillis;
}
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). |
public synchronized int getMinIdle() {
return _minIdle;
}
Returns the minimum number of objects allowed in the pool
before the evictor thread (if active) spawns new objects.
(Note no objects are created when: numActive + numIdle >= maxActive) |
public synchronized int getNumActive() {
return _numActive;
}
Return the number of instances currently borrowed from this pool. |
public synchronized int getNumIdle() {
return _pool.size();
}
Return the number of instances currently idle in this pool. |
public synchronized int getNumTestsPerEvictionRun() {
return _numTestsPerEvictionRun;
}
Returns the max number of objects to examine during each run of the
idle object evictor thread (if any). |
public synchronized long getSoftMinEvictableIdleTimeMillis() {
return _softMinEvictableIdleTimeMillis;
}
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), with the extra condition that at least
"minIdle" amount of object remain in the pool. |
public boolean getTestOnBorrow() {
return _testOnBorrow;
}
When true, objects will be
validated
before being returned by the #borrowObject
method. If the object fails to validate,
it will be dropped from the pool, and we will attempt
to borrow another. |
public boolean getTestOnReturn() {
return _testOnReturn;
}
|
public synchronized boolean getTestWhileIdle() {
return _testWhileIdle;
}
When true, objects will be
validated
by the idle object evictor (if any). If an object
fails to validate, it will be dropped from the pool. |
public synchronized long getTimeBetweenEvictionRunsMillis() {
return _timeBetweenEvictionRunsMillis;
}
Returns the number of milliseconds to sleep between runs of the
idle object evictor thread.
When non-positive, no idle object evictor thread will be
run. |
public synchronized byte getWhenExhaustedAction() {
return _whenExhaustedAction;
}
Returns the action to take when the #borrowObject method
is invoked when the pool is exhausted (the maximum number
of "active" objects has been reached). |
public void invalidateObject(Object obj) throws Exception {
try {
if (_factory != null) {
_factory.destroyObject(obj);
}
} finally {
synchronized (this) {
_numActive--;
allocate();
}
}
}
|
public void returnObject(Object obj) throws Exception {
try {
addObjectToPool(obj, true);
} catch (Exception e) {
if (_factory != null) {
try {
_factory.destroyObject(obj);
} catch (Exception e2) {
// swallowed
}
// TODO: Correctness here depends on control in addObjectToPool.
// These two methods should be refactored, removing the
// "behavior flag", decrementNumActive, from addObjectToPool.
synchronized(this) {
_numActive--;
allocate();
}
}
}
}
Returns an object instance to the pool.
If maxIdle is set to a positive value and the number of idle instances
has reached this value, the returning instance is destroyed.
If testOnReturn == true, the returning instance is validated before being returned
to the idle instance pool. In this case, if validation fails, the instance is destroyed.
Note: There is no guard to prevent an object
being returned to the pool multiple times. Clients are expected to
discard references to returned objects and ensure that an object is not
returned to the pool multiple times in sequence (i.e., without being
borrowed again between returns). Violating this contract will result in
the same object appearing multiple times in the pool and pool counters
(numActive, numIdle) returning incorrect values.
|
public synchronized void setConfig(Config conf) {
setMaxIdle(conf.maxIdle);
setMinIdle(conf.minIdle);
setMaxActive(conf.maxActive);
setMaxWait(conf.maxWait);
setWhenExhaustedAction(conf.whenExhaustedAction);
setTestOnBorrow(conf.testOnBorrow);
setTestOnReturn(conf.testOnReturn);
setTestWhileIdle(conf.testWhileIdle);
setNumTestsPerEvictionRun(conf.numTestsPerEvictionRun);
setMinEvictableIdleTimeMillis(conf.minEvictableIdleTimeMillis);
setTimeBetweenEvictionRunsMillis(conf.timeBetweenEvictionRunsMillis);
setSoftMinEvictableIdleTimeMillis(conf.softMinEvictableIdleTimeMillis);
setLifo(conf.lifo);
allocate();
}
|
public void setFactory(PoolableObjectFactory factory) throws IllegalStateException {
List toDestroy = new ArrayList();
synchronized (this) {
assertOpen();
if(0 < getNumActive()) {
throw new IllegalStateException("Objects are already active");
} else {
toDestroy.addAll(_pool);
_numInternalProcessing = _numInternalProcessing + _pool._size;
_pool.clear();
}
_factory = factory;
}
destroy(toDestroy);
}
Sets the factory this pool uses
to create new instances. Trying to change
the factory while there are borrowed objects will
throw an IllegalStateException . |
public synchronized void setLifo(boolean lifo) {
this._lifo = lifo;
}
Sets the LIFO property of the pool. True means that borrowObject returns
the most recently used ("last in") idle object in the pool (if there are
idle instances available). False means that the pool behaves as a FIFO
queue - objects are taken from the idle object pool in the order that
they are returned to the pool. |
public synchronized void setMaxActive(int maxActive) {
_maxActive = maxActive;
allocate();
}
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. Use
a negative value for no limit. |
public synchronized void setMaxIdle(int maxIdle) {
_maxIdle = maxIdle;
allocate();
}
Sets the cap on the number of "idle" instances in the pool.
If maxIdle is set too low on heavily loaded systems it is possible you
will see objects being destroyed and almost immediately new objects
being created. This is a result of the active threads momentarily
returning objects faster than they are requesting them them, causing the
number of idle objects to rise above maxIdle. The best value for maxIdle
for heavily loaded system will vary but the default is a good starting
point. |
public synchronized void setMaxWait(long maxWait) {
_maxWait = maxWait;
allocate();
}
|
public synchronized void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
_minEvictableIdleTimeMillis = 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).
When non-positive, no objects will be evicted from the pool
due to idle time alone. |
public synchronized void setMinIdle(int minIdle) {
_minIdle = minIdle;
allocate();
}
Sets the minimum number of objects allowed in the pool
before the evictor thread (if active) spawns new objects.
Note that no objects are created when
numActive + numIdle >= maxActive.
This setting has no effect if the idle object evictor is disabled
(i.e. if timeBetweenEvictionRunsMillis <= 0 ). |
public synchronized void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
_numTestsPerEvictionRun = numTestsPerEvictionRun;
}
Sets the max number of objects to examine during each run of the
idle object evictor thread (if any).
When a negative value is supplied, ceil(#getNumIdle )/abs(#getNumTestsPerEvictionRun )
tests will be run. That is, when the value is -n, roughly one nth of the
idle objects will be tested per run. When the value is positive, the number of tests
actually performed in each run will be the minimum of this value and the number of instances
idle in the pool. |
public synchronized void setSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis) {
_softMinEvictableIdleTimeMillis = 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), with the extra condition that at least
"minIdle" object instances remain in the pool.
When non-positive, no objects will be evicted from the pool
due to idle time alone. |
public void setTestOnBorrow(boolean testOnBorrow) {
_testOnBorrow = testOnBorrow;
}
When true, objects will be
validated
before being returned by the #borrowObject
method. If the object fails to validate,
it will be dropped from the pool, and we will attempt
to borrow another. |
public void setTestOnReturn(boolean testOnReturn) {
_testOnReturn = testOnReturn;
}
|
public synchronized void setTestWhileIdle(boolean testWhileIdle) {
_testWhileIdle = testWhileIdle;
}
When true, objects will be
validated
by the idle object evictor (if any). If an object
fails to validate, it will be dropped from the pool. |
public synchronized void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
_timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
startEvictor(_timeBetweenEvictionRunsMillis);
}
Sets the number of milliseconds to sleep between runs of the
idle object evictor thread.
When non-positive, no idle object evictor thread will be
run. |
public synchronized void setWhenExhaustedAction(byte whenExhaustedAction) {
switch(whenExhaustedAction) {
case WHEN_EXHAUSTED_BLOCK:
case WHEN_EXHAUSTED_FAIL:
case WHEN_EXHAUSTED_GROW:
_whenExhaustedAction = whenExhaustedAction;
allocate();
break;
default:
throw new IllegalArgumentException("whenExhaustedAction " + whenExhaustedAction + " not recognized.");
}
}
Sets the action to take when the #borrowObject method
is invoked when the pool is exhausted (the maximum number
of "active" objects has been reached). |
protected synchronized void startEvictor(long delay) {
if(null != _evictor) {
EvictionTimer.cancel(_evictor);
_evictor = null;
}
if(delay > 0) {
_evictor = new Evictor();
EvictionTimer.schedule(_evictor, delay, delay);
}
}
Start the eviction thread or service, or when
delay is non-positive, stop it
if it is already running. |