Author: markt Date: Mon Apr 30 10:34:37 2012 New Revision: 1332147 URL: http://svn.apache.org/viewvc?rev=1332147&view=rev Log: Pull up timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, evictor, evictionLock, evictionIterator and startEvictor()
Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java?rev=1332147&r1=1332146&r2=1332147&view=diff ============================================================================== --- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java (original) +++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java Mon Apr 30 10:34:37 2012 @@ -21,6 +21,7 @@ import java.io.StringWriter; import java.io.Writer; import java.util.ArrayList; import java.util.Deque; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.TimerTask; @@ -39,8 +40,10 @@ import javax.management.ObjectName; * Base class that provides common functionality for {@link GenericObjectPool} * and {@link GenericKeyedObjectPool}. The primary reason this class exists is * reduce code duplication between the two pool implementations. + * + * @param <T> Type of element pooled in this pool. */ -public abstract class BaseGenericObjectPool implements NotificationEmitter { +public abstract class BaseGenericObjectPool<T> implements NotificationEmitter { // Constants /** @@ -62,12 +65,18 @@ public abstract class BaseGenericObjectP GenericObjectPoolConfig.DEFAULT_TEST_ON_BORROW; private volatile boolean testOnReturn = GenericObjectPoolConfig.DEFAULT_TEST_ON_RETURN; + private volatile long timeBetweenEvictionRunsMillis = + GenericObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS; + private volatile int numTestsPerEvictionRun = + GenericObjectPoolConfig.DEFAULT_NUM_TESTS_PER_EVICTION_RUN; // Internal (primarily state) attributes volatile boolean closed = false; - - /** + private Evictor evictor = null; // @GuardedBy("evictionLock") + protected final Object evictionLock = new Object(); + protected Iterator<PooledObject<T>> evictionIterator = null; // @GuardedBy("evictionLock") + /* * Class loader for evictor thread to use since in a J2EE or similar * environment the context class loader for the evictor thread may have * visibility of the correct factory. See POOL-161. @@ -195,7 +204,7 @@ public abstract class BaseGenericObjectP * being returned from the <code>borrowObject()</code> method. Validation is * performed by the factory associated with the pool. If the object fails to * validate, it will be dropped from the pool and destroyed, and a new - * attempt will be made to borrow an object from the pool. + * attempt will be made to borrow an object from the pool. * * @return <code>true</code> if objects are validated before being returned * from the <code>borrowObject()</code> method @@ -210,7 +219,7 @@ public abstract class BaseGenericObjectP * being returned from the <code>borrowObject()</code> method. Validation is * performed by the factory associated with the pool. If the object fails to * validate, it will be dropped from the pool and destroyed, and a new - * attempt will be made to borrow an object from the pool. + * attempt will be made to borrow an object from the pool. * * @param testOnBorrow <code>true</code> if objects should be validated * before being returned from the @@ -249,8 +258,72 @@ public abstract class BaseGenericObjectP public void setTestOnReturn(boolean testOnReturn) { this.testOnReturn = testOnReturn; } - - + + /** + * 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. + * + * @return number of milliseconds to sleep between evictor runs. + * @see #setTimeBetweenEvictionRunsMillis + */ + public long getTimeBetweenEvictionRunsMillis() { + return 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. + * + * @param timeBetweenEvictionRunsMillis + * number of milliseconds to sleep between evictor runs. + * @see #getTimeBetweenEvictionRunsMillis + */ + public void setTimeBetweenEvictionRunsMillis( + long timeBetweenEvictionRunsMillis) { + this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; + startEvictor(timeBetweenEvictionRunsMillis); + } + + /** + * Returns the maximum number of objects to examine during each run (if any) + * of the idle object evictor thread. When positive, the number of tests + * performed for a run will be the minimum of the configured value and the + * number of idle instances in the pool. When negative, the number of tests + * performed will be <code>ceil({@link #getNumIdle}/ + * abs({@link #getNumTestsPerEvictionRun})) whch means that when the value + * is <code>-n</code> roughly one nth of the idle objects will be tested per + * run. + * + * @return max number of objects to examine during each evictor run. + * @see #setNumTestsPerEvictionRun + * @see #setTimeBetweenEvictionRunsMillis + */ + public int getNumTestsPerEvictionRun() { + return numTestsPerEvictionRun; + } + + /** + * Sets the maximum number of objects to examine during each run (if any) + * of the idle object evictor thread. When positive, the number of tests + * performed for a run will be the minimum of the configured value and the + * number of idle instances in the pool. When negative, the number of tests + * performed will be <code>ceil({@link #getNumIdle}/ + * abs({@link #getNumTestsPerEvictionRun})) whch means that when the value + * is <code>-n</code> roughly one nth of the idle objects will be tested per + * run. + * + * @param numTestsPerEvictionRun + * max number of objects to examine during each evictor run. + * @see #getNumTestsPerEvictionRun + * @see #setTimeBetweenEvictionRunsMillis + */ + public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) { + this.numTestsPerEvictionRun = numTestsPerEvictionRun; + } + + /** * Closes the pool, destroys the remaining idle objects and, if registered * in JMX, deregisters it. @@ -290,6 +363,29 @@ public abstract class BaseGenericObjectP } } + /** + * Start the eviction thread or service, or when <i>delay</i> is + * non-positive, stop it if it is already running. + * + * @param delay + * milliseconds between evictor runs. + */ + // Needs to be final; see POOL-195. Make protected method final as it is + // called from a constructor. + protected final void startEvictor(long delay) { + synchronized (evictionLock) { + if (null != evictor) { + EvictionTimer.cancel(evictor); + evictor = null; + evictionIterator = null; + } + if (delay > 0) { + evictor = new Evictor(); + EvictionTimer.schedule(evictor, delay, delay); + } + } + } + protected abstract void ensureMinIdle() throws Exception; Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java?rev=1332147&r1=1332146&r2=1332147&view=diff ============================================================================== --- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java (original) +++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java Mon Apr 30 10:34:37 2012 @@ -25,7 +25,6 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.NoSuchElementException; -import java.util.TimerTask; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; @@ -197,7 +196,7 @@ import org.apache.commons.pool2.PoolUtil * @version $Revision$ $Date$ * @since Pool 1.0 */ -public class GenericKeyedObjectPool<K,T> extends BaseGenericObjectPool +public class GenericKeyedObjectPool<K,T> extends BaseGenericObjectPool<T> implements KeyedObjectPool<K,T>, GenericKeyedObjectPoolMBean<K> { /** @@ -366,66 +365,6 @@ public class GenericKeyedObjectPool<K,T> } /** - * 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. - * - * @return milliseconds to sleep between evictor runs. - * @see #setTimeBetweenEvictionRunsMillis - */ - @Override - public long getTimeBetweenEvictionRunsMillis() { - return 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. - * - * @param timeBetweenEvictionRunsMillis milliseconds to sleep between evictor runs. - * @see #getTimeBetweenEvictionRunsMillis - */ - public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) { - this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; - startEvictor(timeBetweenEvictionRunsMillis); - } - - /** - * Returns the max number of objects to examine during each run of the - * idle object evictor thread (if any). - * - * @return number of objects to examine each eviction run. - * @see #setNumTestsPerEvictionRun - * @see #setTimeBetweenEvictionRunsMillis - */ - @Override - public int getNumTestsPerEvictionRun() { - return numTestsPerEvictionRun; - } - - /** - * Sets the max number of objects to examine during each run of the - * idle object evictor thread (if any). - * <p> - * When a negative value is supplied, - * <code>ceil({@link #getNumIdle()})/abs({@link #getNumTestsPerEvictionRun})</code> - * tests will be run. I.e., when the value is <code>-n</code>, roughly one <code>n</code>th 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 pools. - * - * @param numTestsPerEvictionRun number of objects to examine each eviction run. - * @see #setNumTestsPerEvictionRun - * @see #setTimeBetweenEvictionRunsMillis - */ - public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) { - this.numTestsPerEvictionRun = numTestsPerEvictionRun; - } - - /** * 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). @@ -1608,29 +1547,6 @@ public class GenericKeyedObjectPool<K,T> } /** - * Start the eviction thread or service, or when - * <code>delay</code> is non-positive, stop it - * if it is already running. - * - * @param delay milliseconds between evictor runs. - */ - // Needs to be final; see POOL-195. Make protected method final as it is called from constructor. - protected final void startEvictor(long delay) { - synchronized (evictionLock) { - if (null != evictor) { - EvictionTimer.cancel(evictor); - evictor = null; - evictionIterator = null; - evictionKeyIterator = null; - } - if (delay > 0) { - evictor = new Evictor(); - EvictionTimer.schedule(evictor, delay, delay); - } - } - } - - /** * Returns pool info including {@link #getNumActive()}, {@link #getNumIdle()} * and currently defined keys. * @@ -1965,34 +1881,6 @@ public class GenericKeyedObjectPool<K,T> GenericKeyedObjectPoolConfig.DEFAULT_TEST_WHILE_IDLE; /** - * 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. - * - * @see #setTimeBetweenEvictionRunsMillis - * @see #getTimeBetweenEvictionRunsMillis - */ - private long timeBetweenEvictionRunsMillis = - GenericKeyedObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS; - - /** - * The number of objects to examine during each run of the - * idle object evictor thread (if any). - * <p> - * When a negative value is supplied, <code>ceil({@link #getNumIdle})/abs({@link #getNumTestsPerEvictionRun})</code> - * tests will be run. I.e., when the value is <code>-n</code>, roughly one <code>n</code>th of the - * idle objects will be tested per run. - * - * @see #setNumTestsPerEvictionRun - * @see #getNumTestsPerEvictionRun - * @see #getTimeBetweenEvictionRunsMillis - * @see #setTimeBetweenEvictionRunsMillis - */ - private int numTestsPerEvictionRun = - GenericKeyedObjectPoolConfig.DEFAULT_NUM_TESTS_PER_EVICTION_RUN; - - /** * 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). @@ -2054,17 +1942,6 @@ public class GenericKeyedObjectPool<K,T> private final AtomicInteger numTotal = new AtomicInteger(0); /** - * My idle object eviction {@link TimerTask}, if any. - */ - private Evictor evictor = null; // @GuardedBy("evictionLock") - - /** - * An iterator for {@link ObjectDeque#getIdleObjects()} that is used by the - * evictor. - */ - private Iterator<PooledObject<T>> evictionIterator = null; // @GuardedBy("evictionLock") - - /** * An iterator for {@link #poolMap} entries. */ private Iterator<K> evictionKeyIterator = null; // @GuardedBy("evictionLock") @@ -2080,9 +1957,6 @@ public class GenericKeyedObjectPool<K,T> */ private EvictionPolicy<T> evictionPolicy; - /** Object used to ensure thread safety of eviction process */ - private final Object evictionLock = new Object(); - /** Object used to ensure closed() is only called once. */ private final Object closeLock = new Object(); Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java?rev=1332147&r1=1332146&r2=1332147&view=diff ============================================================================== --- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java (original) +++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java Mon Apr 30 10:34:37 2012 @@ -23,7 +23,6 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; -import java.util.TimerTask; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; @@ -162,7 +161,7 @@ import org.apache.commons.pool2.Poolable * 2011) $ * @since Pool 1.0 */ -public class GenericObjectPool<T> extends BaseGenericObjectPool +public class GenericObjectPool<T> extends BaseGenericObjectPool<T> implements ObjectPool<T>, GenericObjectPoolMBean { /** @@ -302,66 +301,6 @@ public class GenericObjectPool<T> extend } } - /** - * 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. - * - * @return number of milliseconds to sleep between evictor runs. - * @see #setTimeBetweenEvictionRunsMillis - */ - @Override - public long getTimeBetweenEvictionRunsMillis() { - return 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. - * - * @param timeBetweenEvictionRunsMillis - * number of milliseconds to sleep between evictor runs. - * @see #getTimeBetweenEvictionRunsMillis - */ - public void setTimeBetweenEvictionRunsMillis( - long timeBetweenEvictionRunsMillis) { - this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; - startEvictor(timeBetweenEvictionRunsMillis); - } - - /** - * Returns the max number of objects to examine during each run of the idle - * object evictor thread (if any). - * - * @return max number of objects to examine during each evictor run. - * @see #setNumTestsPerEvictionRun - * @see #setTimeBetweenEvictionRunsMillis - */ - @Override - public int getNumTestsPerEvictionRun() { - return numTestsPerEvictionRun; - } - - /** - * Sets the max number of objects to examine during each run of the idle - * object evictor thread (if any). - * <p> - * When a negative value is supplied, - * <tt>ceil({@link #getNumIdle})/abs({@link #getNumTestsPerEvictionRun})</tt> - * tests will be run. That is, when the value is <i>-n</i>, roughly one - * <i>n</i>th 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. - * - * @param numTestsPerEvictionRun - * max number of objects to examine during each evictor run. - * @see #getNumTestsPerEvictionRun - * @see #setTimeBetweenEvictionRunsMillis - */ - public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) { - this.numTestsPerEvictionRun = numTestsPerEvictionRun; - } /** * Returns the minimum amount of time an object may sit idle in the pool @@ -1111,28 +1050,6 @@ public class GenericObjectPool<T> extend } /** - * Start the eviction thread or service, or when <i>delay</i> is - * non-positive, stop it if it is already running. - * - * @param delay - * milliseconds between evictor runs. - */ - // Needs to be final; see POOL-195. Make protected method final as it is called from constructor. - protected final void startEvictor(long delay) { - synchronized (evictionLock) { - if (null != evictor) { - EvictionTimer.cancel(evictor); - evictor = null; - evictionIterator = null; - } - if (delay > 0) { - evictor = new Evictor(); - EvictionTimer.schedule(evictor, delay, delay); - } - } - } - - /** * Returns pool info including {@link #getNumActive()}, * {@link #getNumIdle()} and a list of objects idle in the pool with their * idle times. @@ -1302,34 +1219,6 @@ public class GenericObjectPool<T> extend GenericObjectPoolConfig.DEFAULT_TEST_WHILE_IDLE; /** - * 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. - * - * @see #setTimeBetweenEvictionRunsMillis - * @see #getTimeBetweenEvictionRunsMillis - */ - private volatile long timeBetweenEvictionRunsMillis = - GenericObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS; - - /** - * The max number of objects to examine during each run of the idle object - * evictor thread (if any). - * <p> - * When a negative value is supplied, - * <tt>ceil({@link #getNumIdle})/abs({@link #getNumTestsPerEvictionRun})</tt> - * tests will be run. I.e., when the value is <i>-n</i>, roughly one - * <i>n</i>th of the idle objects will be tested per run. - * - * @see #setNumTestsPerEvictionRun - * @see #getNumTestsPerEvictionRun - * @see #getTimeBetweenEvictionRunsMillis - * @see #setTimeBetweenEvictionRunsMillis - */ - private volatile int numTestsPerEvictionRun = - GenericObjectPoolConfig.DEFAULT_NUM_TESTS_PER_EVICTION_RUN; - - /** * 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 @@ -1385,21 +1274,10 @@ public class GenericObjectPool<T> extend new LinkedBlockingDeque<PooledObject<T>>(); /** - * My idle object eviction {@link TimerTask}, if any. - */ - private Evictor evictor = null; // @GuardedBy("evictionLock") - - /** An iterator for {@link #idleObjects} that is used by the evictor. */ - private Iterator<PooledObject<T>> evictionIterator = null; // @GuardedBy("evictionLock") - - /** * Policy that determines if an object is eligible for eviction or not. */ private EvictionPolicy<T> evictionPolicy; - /** Object used to ensure thread safety of eviction process */ - private final Object evictionLock = new Object(); - /** Object used to ensure closed() is only called once. */ private final Object closeLock = new Object();