| Constructor: |
public GenericObjectPool() {
//--- constructors -----------------------------------------------
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. |
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 values. Parameters:
factory - the (possibly null)PoolableObjectFactory to use to create, validate and destroy objects
|
public GenericObjectPool(PoolableObjectFactory factory,
GenericObjectPool.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 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 )
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 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 )
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 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 )
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 from me 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 an 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 from me 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 an 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 from me 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 an 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 from me 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 an 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 synchronized 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();
for(;;) {
ObjectTimestampPair pair = null;
synchronized (this) {
assertOpen();
// if there are any sleeping, just grab one of those
try {
pair = (ObjectTimestampPair)(_pool.removeFirst());
} catch(NoSuchElementException e) {
; /* ignored */
}
// otherwise
if(null == pair) {
// check if we can create one
// (note we know that the num sleeping is 0, else we wouldn't be here)
if(_maxActive < 0 || _numActive < _maxActive) {
// allow new object to be created
} else {
// the pool is exhausted
switch(_whenExhaustedAction) {
case WHEN_EXHAUSTED_GROW:
// allow new object to be created
break;
case WHEN_EXHAUSTED_FAIL:
throw new NoSuchElementException("Pool exhausted");
case WHEN_EXHAUSTED_BLOCK:
try {
if(_maxWait < = 0) {
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)
{
wait(waitTime);
}
}
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
throw e;
}
if(_maxWait > 0 && ((System.currentTimeMillis() - starttime) >= _maxWait)) {
throw new NoSuchElementException("Timeout waiting for idle object");
} else {
continue; // keep looping
}
default:
throw new IllegalArgumentException("WhenExhaustedAction property " + _whenExhaustedAction + " not recognized.");
}
}
}
_numActive++;
}
// create new object when needed
boolean newlyCreated = false;
if(null == pair) {
try {
Object obj = _factory.makeObject();
pair = new ObjectTimestampPair(obj);
newlyCreated = true;
} finally {
if (!newlyCreated) {
// object cannot be created
synchronized (this) {
_numActive--;
notifyAll();
}
}
}
}
// activate & validate the object
try {
_factory.activateObject(pair.value);
if(_testOnBorrow && !_factory.validateObject(pair.value)) {
throw new Exception("ValidateObject failed");
}
return pair.value;
}
catch (Throwable e) {
// object cannot be activated or is invalid
try {
_factory.destroyObject(pair.value);
} catch (Throwable e2) {
// cannot destroy broken object
}
synchronized (this) {
_numActive--;
notifyAll();
}
if(newlyCreated) {
throw new NoSuchElementException("Could not create a validated object, cause: " + e.getMessage());
}
else {
continue; // keep looping
}
}
}
}
|
public synchronized void clear() {
for(Iterator it = _pool.iterator(); it.hasNext(); ) {
try {
_factory.destroyObject(((ObjectTimestampPair)(it.next())).value);
} catch(Exception e) {
// ignore error, keep destroying the rest
}
it.remove();
}
_pool.clear();
notifyAll(); // num sleeping has changed
}
Clears any objects sitting idle in the pool. |
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 synchronized void evict() throws Exception {
assertOpen();
if(!_pool.isEmpty()) {
if (null == _evictionCursor) {
_evictionCursor = (_pool.cursor(_lifo ? _pool.size() : 0));
}
for (int i=0,m=getNumTests();i< m;i++) {
if ((_lifo && !_evictionCursor.hasPrevious()) ||
!_lifo && !_evictionCursor.hasNext()) {
_evictionCursor.close();
_evictionCursor = _pool.cursor(_lifo ? _pool.size() : 0);
}
boolean removeObject = false;
final ObjectTimestampPair pair = _lifo ?
(ObjectTimestampPair) _evictionCursor.previous() :
(ObjectTimestampPair) _evictionCursor.next();
final long idleTimeMilis = System.currentTimeMillis() - pair.tstamp;
if ((_minEvictableIdleTimeMillis > 0)
&& (idleTimeMilis > _minEvictableIdleTimeMillis)) {
removeObject = true;
} else if ((_softMinEvictableIdleTimeMillis > 0)
&& (idleTimeMilis > _softMinEvictableIdleTimeMillis)
&& (getNumIdle() > getMinIdle())) {
removeObject = true;
}
if(_testWhileIdle && !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 {
_evictionCursor.remove();
_factory.destroyObject(pair.value);
} catch(Exception e) {
// ignored
}
}
}
} // if !empty
}
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 cap on the total number of active instances from the pool. |
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--;
notifyAll(); // _numActive has changed
}
}
}
|
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--;
notifyAll();
}
}
}
}
{@inheritDoc}
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(GenericObjectPool.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);
notifyAll();
}
|
public synchronized void setFactory(PoolableObjectFactory factory) throws IllegalStateException {
assertOpen();
if(0 < getNumActive()) {
throw new IllegalStateException("Objects are already active");
} else {
clear();
_factory = factory;
}
}
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;
notifyAll();
}
Sets the cap on the total number of active instances from the pool. |
public synchronized void setMaxIdle(int maxIdle) {
_maxIdle = maxIdle;
notifyAll();
}
Sets the cap on the number of "idle" instances in the pool. |
public synchronized void setMaxWait(long maxWait) {
_maxWait = maxWait;
notifyAll();
}
|
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;
notifyAll();
}
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. I.e., when the value is -n, roughly one nth of the
idle objects will be tested per run. |
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" amount of object 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;
notifyAll();
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. |