This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-pool.git
The following commit(s) were added to refs/heads/master by this push: new 3e31c71 Use Durations instead of ints or longs. 3e31c71 is described below commit 3e31c71274d576f868a13fc857ac3fdc559553bd Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Sat Jul 24 18:05:15 2021 -0400 Use Durations instead of ints or longs. - Add and use Duration APIs in BaseGenericObjectPool: getDurationBetweenEvictionRuns(), getEvictorShutdownTimeoutDuration(), getMinEvictableIdleDuration(), getSoftMinEvictableIdleDuration(), setMaxWait(Duration), setMinEvictableIdle(Duration), setSoftMinEvictableIdle(Duration). - Add and use Duration APIs in BaseObjectPoolConfig: getDurationBetweenEvictionRuns(), getEvictorShutdownTimeoutDuration(),getMinEvictableIdleDuration(), getSoftMinEvictableIdleDuration(). - Add and use Duration APIs in EvictionConfig: getIdleEvictDuration(), getIdleSoftEvictDuration(). - Add and use Duration APIs in PooledObject: getIdleDuration(). - No need to initialize instance variables to their default values. - Update Javadocs. - Update toString() implementations with duration labels. --- src/changes/changes.xml | 10 + .../org/apache/commons/pool2/PooledObject.java | 14 ++ .../apache/commons/pool2/impl/AbandonedConfig.java | 35 +-- .../commons/pool2/impl/BaseGenericObjectPool.java | 222 ++++++++++++++----- .../commons/pool2/impl/BaseObjectPoolConfig.java | 240 ++++++++++++++------- .../commons/pool2/impl/DefaultEvictionPolicy.java | 29 +-- .../pool2/impl/DefaultPooledObjectInfo.java | 22 +- .../apache/commons/pool2/impl/EvictionConfig.java | 94 +++++--- .../commons/pool2/impl/GenericKeyedObjectPool.java | 24 +-- .../pool2/impl/GenericKeyedObjectPoolMXBean.java | 12 +- .../commons/pool2/impl/GenericObjectPool.java | 15 +- .../pool2/impl/GenericObjectPoolMXBean.java | 16 +- .../pool2/impl/SecurityManagerCallStack.java | 1 - .../java/org/apache/commons/pool2/PoolTest.java | 1 + .../pool2/impl/TestAbandonedKeyedObjectPool.java | 19 +- .../pool2/impl/TestAbandonedObjectPool.java | 20 +- .../apache/commons/pool2/impl/TestConstants.java | 20 +- .../pool2/impl/TestDefaultPooledObjectInfo.java | 2 +- .../commons/pool2/impl/TestEvictionConfig.java | 6 + .../commons/pool2/impl/TestEvictionTimer.java | 4 +- .../pool2/impl/TestGenericKeyedObjectPool.java | 14 ++ .../commons/pool2/impl/TestGenericObjectPool.java | 29 ++- 22 files changed, 583 insertions(+), 266 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 2c9575b..7bd876b 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -66,6 +66,16 @@ The <action> type attribute can be add,update,fix,remove. <action dev="ggregory" type="add" due-to="Gary Gregory"> Add BaseObjectPoolConfig.{get|set}MaxWaitDuration(Duration) and deprecate {get|set}MaxWaitMillis(long). </action> + <action dev="ggregory" type="add" due-to="Gary Gregory"> + Add and use Duration APIs instead of ints or longs. + - Add and use Duration APIs in BaseGenericObjectPool: getDurationBetweenEvictionRuns(), getEvictorShutdownTimeoutDuration(), getMinEvictableIdleDuration(), getSoftMinEvictableIdleDuration(), setMaxWait(Duration), setMinEvictableIdle(Duration), setSoftMinEvictableIdle(Duration). + - Add and use Duration APIs in BaseObjectPoolConfig: getDurationBetweenEvictionRuns(), getEvictorShutdownTimeoutDuration(),getMinEvictableIdleDuration(), getSoftMinEvictableIdleDuration(). + - Add and use Duration APIs in EvictionConfig: getIdleEvictDuration(), getIdleSoftEvictDuration(). + - Add and use Duration APIs in PooledObject: getIdleDuration(). + - No need to initialize instance variables to their default values. + - Update Javadocs. + - Update toString() implementations with duration labels. + </action> <action issue="POOL-396" dev="ggregory" type="add" due-to="Jeremy Kong, Phil Steitz"> Handle validation exceptions during eviction. #85. </action> diff --git a/src/main/java/org/apache/commons/pool2/PooledObject.java b/src/main/java/org/apache/commons/pool2/PooledObject.java index 83ed7c0..1e8ce4f 100644 --- a/src/main/java/org/apache/commons/pool2/PooledObject.java +++ b/src/main/java/org/apache/commons/pool2/PooledObject.java @@ -140,8 +140,22 @@ public interface PooledObject<T> extends Comparable<PooledObject<T>> { * return an increased value). * * @return The amount of time in last spent in the idle state. + * @since 2.11.0 + */ + default Duration getIdleDuration() { + return Duration.ofMillis(getIdleTimeMillis()); + } + + /** + * Gets the amount of time that this object last spend in the + * idle state (it may still be idle in which case subsequent calls will + * return an increased value). + * + * @return The amount of time in last spent in the idle state. * @since 2.10.0 + * @deprecated Use {@link #getIdleDuration()}. */ + @Deprecated default Duration getIdleTime() { return Duration.ofMillis(getIdleTimeMillis()); } diff --git a/src/main/java/org/apache/commons/pool2/impl/AbandonedConfig.java b/src/main/java/org/apache/commons/pool2/impl/AbandonedConfig.java index be3f1bb..ef728db 100644 --- a/src/main/java/org/apache/commons/pool2/impl/AbandonedConfig.java +++ b/src/main/java/org/apache/commons/pool2/impl/AbandonedConfig.java @@ -32,7 +32,10 @@ import org.apache.commons.pool2.UsageTracking; */ public class AbandonedConfig { - private static final Duration DEFAULT_REMOVE_ABANDONED_TIMEOUT = Duration.ofSeconds(300); + /** + * The 5 minutes Duration. + */ + private static final Duration DEFAULT_REMOVE_ABANDONED_TIMEOUT_DURATION = Duration.ofMinutes(5); /** * Creates a new instance with values from the given instance. @@ -59,13 +62,13 @@ public class AbandonedConfig { /** * Timeout before an abandoned object can be removed. */ - private Duration removeAbandonedTimeout = DEFAULT_REMOVE_ABANDONED_TIMEOUT; + private Duration removeAbandonedTimeoutDuration = DEFAULT_REMOVE_ABANDONED_TIMEOUT_DURATION; /** * Determines whether or not to log stack traces for application code * which abandoned an object. */ - private boolean logAbandoned = false; + private boolean logAbandoned; /** * Determines whether or not to log full stack traces when logAbandoned is true. @@ -87,7 +90,7 @@ public class AbandonedConfig { * stack trace every time a method is called on a pooled object and retain * the most recent stack trace to aid debugging of abandoned objects? */ - private boolean useUsageTracking = false; + private boolean useUsageTracking; /** * Creates a new instance. @@ -165,7 +168,7 @@ public class AbandonedConfig { * <p>If set to true, abandoned objects are removed by the pool * maintenance thread when it runs. This setting has no effect * unless maintenance is enabled by setting - *{@link GenericObjectPool#getTimeBetweenEvictionRuns() timeBetweenEvictionRuns} + *{@link GenericObjectPool#getDurationBetweenEvictionRuns() durationBetweenEvictionRuns} * to a positive number.</p> * * @return true if abandoned objects are to be removed by the evictor @@ -183,12 +186,12 @@ public class AbandonedConfig { * * <p>The default value is 300 seconds.</p> * - * @return the abandoned object timeout in seconds + * @return the abandoned object timeout in seconds. * @deprecated Use {@link #getRemoveAbandonedTimeoutDuration()}. */ @Deprecated public int getRemoveAbandonedTimeout() { - return (int) this.removeAbandonedTimeout.getSeconds(); + return (int) this.removeAbandonedTimeoutDuration.getSeconds(); } /** @@ -204,7 +207,7 @@ public class AbandonedConfig { * @since 2.10.0 */ public Duration getRemoveAbandonedTimeoutDuration() { - return this.removeAbandonedTimeout; + return this.removeAbandonedTimeoutDuration; } /** @@ -289,11 +292,11 @@ public class AbandonedConfig { * are both false.</p> * * @param removeAbandonedTimeout new abandoned timeout - * @see #getRemoveAbandonedTimeout() + * @see #getRemoveAbandonedTimeoutDuration() * @since 2.10.0 */ public void setRemoveAbandonedTimeout(final Duration removeAbandonedTimeout) { - this.removeAbandonedTimeout = PoolImplUtils.nonNull(removeAbandonedTimeout, DEFAULT_REMOVE_ABANDONED_TIMEOUT); + this.removeAbandonedTimeoutDuration = PoolImplUtils.nonNull(removeAbandonedTimeout, DEFAULT_REMOVE_ABANDONED_TIMEOUT_DURATION); } /** @@ -305,13 +308,13 @@ public class AbandonedConfig { * {@link #getRemoveAbandonedOnMaintenance() removeAbandonedOnMaintenance} * are both false.</p> * - * @param removeAbandonedTimeout new abandoned timeout in seconds - * @see #getRemoveAbandonedTimeout() + * @param removeAbandonedTimeoutSeconds new abandoned timeout in seconds + * @see #getRemoveAbandonedTimeoutDuration() * @deprecated Use {@link #setRemoveAbandonedTimeout(Duration)}. */ @Deprecated - public void setRemoveAbandonedTimeout(final int removeAbandonedTimeout) { - setRemoveAbandonedTimeout(Duration.ofSeconds(removeAbandonedTimeout)); + public void setRemoveAbandonedTimeout(final int removeAbandonedTimeoutSeconds) { + setRemoveAbandonedTimeout(Duration.ofSeconds(removeAbandonedTimeoutSeconds)); } /** @@ -351,8 +354,8 @@ public class AbandonedConfig { builder.append(removeAbandonedOnBorrow); builder.append(", removeAbandonedOnMaintenance="); builder.append(removeAbandonedOnMaintenance); - builder.append(", removeAbandonedTimeout="); - builder.append(removeAbandonedTimeout); + builder.append(", removeAbandonedTimeoutDuration="); + builder.append(removeAbandonedTimeoutDuration); builder.append(", logAbandoned="); builder.append(logAbandoned); builder.append(", logWriter="); diff --git a/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java b/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java index f5efecb..3502d50 100644 --- a/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java @@ -341,13 +341,13 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { private volatile boolean testOnBorrow = BaseObjectPoolConfig.DEFAULT_TEST_ON_BORROW; private volatile boolean testOnReturn = BaseObjectPoolConfig.DEFAULT_TEST_ON_RETURN; private volatile boolean testWhileIdle = BaseObjectPoolConfig.DEFAULT_TEST_WHILE_IDLE; - private volatile Duration timeBetweenEvictionRuns = BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS; + private volatile Duration durationBetweenEvictionRuns = BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS; private volatile int numTestsPerEvictionRun = BaseObjectPoolConfig.DEFAULT_NUM_TESTS_PER_EVICTION_RUN; - private volatile Duration minEvictableIdleTime = BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME; - private volatile Duration softMinEvictableIdleTime = BaseObjectPoolConfig.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME; + private volatile Duration minEvictableIdleDuration = BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_DURATION; + private volatile Duration softMinEvictableIdleDuration = BaseObjectPoolConfig.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_DURATION; private volatile EvictionPolicy<T> evictionPolicy; - private volatile Duration evictorShutdownTimeout = BaseObjectPoolConfig.DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT; + private volatile Duration evictorShutdownTimeoutDuration = BaseObjectPoolConfig.DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT; // Internal (primarily state) attributes final Object closeLock = new Object(); volatile boolean closed; @@ -589,9 +589,24 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * @return The timeout that will be used while waiting for * the Evictor to shut down. * @since 2.10.0 + * @deprecated Use {@link #getEvictorShutdownTimeoutDuration()}. */ + @Deprecated public final Duration getEvictorShutdownTimeout() { - return evictorShutdownTimeout; + return evictorShutdownTimeoutDuration; + } + + /** + * Gets the timeout that will be used when waiting for the Evictor to + * shutdown if this pool is closed and it is the only pool still using the + * the value for the Evictor. + * + * @return The timeout that will be used while waiting for + * the Evictor to shut down. + * @since 2.11.0 + */ + public final Duration getEvictorShutdownTimeoutDuration() { + return evictorShutdownTimeoutDuration; } /** @@ -601,11 +616,11 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * * @return The timeout in milliseconds that will be used while waiting for * the Evictor to shut down. - * @deprecated Use {@link #getEvictorShutdownTimeout()}. + * @deprecated Use {@link #getEvictorShutdownTimeoutDuration()}. */ @Deprecated public final long getEvictorShutdownTimeoutMillis() { - return evictorShutdownTimeout.toMillis(); + return evictorShutdownTimeoutDuration.toMillis(); } /** @@ -692,7 +707,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * @return the maximum number of milliseconds {@code borrowObject()} * will block. * - * @see #setMaxWaitDuration + * @see #setMaxWait * @see #setBlockWhenExhausted * @since 2.11.0 */ @@ -710,7 +725,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * @return the maximum number of milliseconds {@code borrowObject()} * will block. * - * @see #setMaxWaitDuration + * @see #setMaxWait * @see #setBlockWhenExhausted * @deprecated Use {@link #getMaxWaitDuration()}. */ @@ -774,10 +789,29 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * * @see #setMinEvictableIdleTimeMillis * @see #setTimeBetweenEvictionRunsMillis + * @since 2.11.0 + */ + public final Duration getMinEvictableIdleDuration() { + return minEvictableIdleDuration; + } + + /** + * Gets 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 {@link #setTimeBetweenEvictionRuns(Duration)}). When non-positive, + * no objects will be evicted from the pool due to idle time alone. + * + * @return minimum amount of time an object may sit idle in the pool before + * it is eligible for eviction + * + * @see #setMinEvictableIdleTimeMillis + * @see #setTimeBetweenEvictionRunsMillis * @since 2.10.0 + * @deprecated Use {@link #getMinEvictableIdleDuration()}. */ + @Deprecated public final Duration getMinEvictableIdleTime() { - return minEvictableIdleTime; + return minEvictableIdleDuration; } /** @@ -791,11 +825,11 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * * @see #setMinEvictableIdleTimeMillis * @see #setTimeBetweenEvictionRunsMillis - * @deprecated Use {@link #getMinEvictableIdleTime()}. + * @deprecated Use {@link #getMinEvictableIdleDuration()}. */ @Deprecated public final long getMinEvictableIdleTimeMillis() { - return minEvictableIdleTime.toMillis(); + return minEvictableIdleDuration.toMillis(); } /** @@ -859,15 +893,14 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * @return The abandoned object timeout in seconds if abandoned object * removal is configured for this pool; Integer.MAX_VALUE otherwise. * - * @see AbandonedConfig#getRemoveAbandonedTimeout() + * @see AbandonedConfig#getRemoveAbandonedTimeoutDuration() * @see AbandonedConfig#getRemoveAbandonedTimeoutDuration() * @deprecated Use {@link #getRemoveAbandonedTimeoutDuration()}. * @since 2.11.0 */ @Deprecated public int getRemoveAbandonedTimeout() { - final AbandonedConfig ac = this.abandonedConfig; - return ac != null ? ac.getRemoveAbandonedTimeout() : Integer.MAX_VALUE; + return (int) getRemoveAbandonedTimeoutDuration().getSeconds(); } /** @@ -908,11 +941,33 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * @return minimum amount of time an object may sit idle in the pool before * it is eligible for eviction if minIdle instances are available * - * @see #setSoftMinEvictableIdleTime(Duration) + * @see #setSoftMinEvictableIdle(Duration) + * @since 2.11.0 + */ + public final Duration getSoftMinEvictableIdleDuration() { + return softMinEvictableIdleDuration; + } + + /** + * Gets 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 {@link #setTimeBetweenEvictionRuns(Duration)}), + * with the extra condition that at least {@code minIdle} object + * instances remain in the pool. This setting is overridden by + * {@link #getMinEvictableIdleTime} (that is, if + * {@link #getMinEvictableIdleTime} is positive, then + * {@link #getSoftMinEvictableIdleTime} is ignored). + * + * @return minimum amount of time an object may sit idle in the pool before + * it is eligible for eviction if minIdle instances are available + * + * @see #setSoftMinEvictableIdle(Duration) * @since 2.10.0 + * @deprecated Use {@link #getSoftMinEvictableIdleDuration}. */ + @Deprecated public final Duration getSoftMinEvictableIdleTime() { - return softMinEvictableIdleTime; + return softMinEvictableIdleDuration; } /** @@ -933,7 +988,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { */ @Deprecated public final long getSoftMinEvictableIdleTimeMillis() { - return softMinEvictableIdleTime.toMillis(); + return softMinEvictableIdleDuration.toMillis(); } /** @@ -960,15 +1015,14 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { // Simply listed in AB order. return String.format( "activeTimes=%s, blockWhenExhausted=%s, borrowedCount=%,d, closed=%s, createdCount=%,d, destroyedByBorrowValidationCount=%,d, " + - "destroyedByEvictorCount=%,d, evictorShutdownTimeout=%s, fairness=%s, idleTimes=%s, lifo=%s, maxBorrowWaitTimeMillis=%,d, " + - "maxTotal=%s, maxWaitDuration=%s, minEvictableIdleTime=%s, numTestsPerEvictionRun=%s, returnedCount=%s, " + - "softMinEvictableIdleTime=%s, testOnBorrow=%s, testOnCreate=%s, testOnReturn=%s, testWhileIdle=%s, timeBetweenEvictionRuns=%s, " + - "waitTimes=%s", - activeTimes.getCurrentValues(), blockWhenExhausted, borrowedCount.get(), closed, createdCount.get(), - destroyedByBorrowValidationCount.get(), destroyedByEvictorCount.get(), evictorShutdownTimeout, fairness, - idleTimes.getCurrentValues(), lifo, maxBorrowWaitTimeMillis.get(), maxTotal, maxWaitDuration, - minEvictableIdleTime, numTestsPerEvictionRun, returnedCount, softMinEvictableIdleTime, testOnBorrow, - testOnCreate, testOnReturn, testWhileIdle, timeBetweenEvictionRuns, waitTimes.getCurrentValues()); + "destroyedByEvictorCount=%,d, evictorShutdownTimeoutDuration=%s, fairness=%s, idleTimes=%s, lifo=%s, maxBorrowWaitTimeMillis=%,d, " + + "maxTotal=%s, maxWaitDuration=%s, minEvictableIdleDuration=%s, numTestsPerEvictionRun=%s, returnedCount=%s, " + + "softMinEvictableIdleDuration=%s, testOnBorrow=%s, testOnCreate=%s, testOnReturn=%s, testWhileIdle=%s, " + + "durationBetweenEvictionRuns=%s, waitTimes=%s", + activeTimes.getCurrentValues(), blockWhenExhausted, borrowedCount.get(), closed, createdCount.get(), destroyedByBorrowValidationCount.get(), + destroyedByEvictorCount.get(), evictorShutdownTimeoutDuration, fairness, idleTimes.getCurrentValues(), lifo, maxBorrowWaitTimeMillis.get(), + maxTotal, maxWaitDuration, minEvictableIdleDuration, numTestsPerEvictionRun, returnedCount, softMinEvictableIdleDuration, testOnBorrow, + testOnCreate, testOnReturn, testWhileIdle, durationBetweenEvictionRuns, waitTimes.getCurrentValues()); } /** @@ -1057,10 +1111,26 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * @return number of milliseconds to sleep between evictor runs * * @see #setTimeBetweenEvictionRuns + * @since 2.11.0 + */ + public final Duration getDurationBetweenEvictionRuns() { + return durationBetweenEvictionRuns; + } + + /** + * Gets the duration 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 #setTimeBetweenEvictionRuns * @since 2.10.0 + * @deprecated {@link #getDurationBetweenEvictionRuns()}. */ + @Deprecated public final Duration getTimeBetweenEvictionRuns() { - return timeBetweenEvictionRuns; + return durationBetweenEvictionRuns; } /** @@ -1071,11 +1141,11 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * @return number of milliseconds to sleep between evictor runs * * @see #setTimeBetweenEvictionRunsMillis - * @deprecated Use {@link #getTimeBetweenEvictionRuns()}. + * @deprecated Use {@link #getDurationBetweenEvictionRuns()}. */ @Deprecated public final long getTimeBetweenEvictionRunsMillis() { - return timeBetweenEvictionRuns.toMillis(); + return durationBetweenEvictionRuns.toMillis(); } // Monitoring (primarily JMX) related methods @@ -1217,16 +1287,16 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { */ protected void setConfig(final BaseObjectPoolConfig<T> config) { setLifo(config.getLifo()); - setMaxWaitDuration(config.getMaxWaitDuration()); + setMaxWait(config.getMaxWaitDuration()); setBlockWhenExhausted(config.getBlockWhenExhausted()); setTestOnCreate(config.getTestOnCreate()); setTestOnBorrow(config.getTestOnBorrow()); setTestOnReturn(config.getTestOnReturn()); setTestWhileIdle(config.getTestWhileIdle()); setNumTestsPerEvictionRun(config.getNumTestsPerEvictionRun()); - setMinEvictableIdleTime(config.getMinEvictableIdleTime()); - setTimeBetweenEvictionRuns(config.getTimeBetweenEvictionRuns()); - setSoftMinEvictableIdleTime(config.getSoftMinEvictableIdleTime()); + setMinEvictableIdle(config.getMinEvictableIdleDuration()); + setTimeBetweenEvictionRuns(config.getDurationBetweenEvictionRuns()); + setSoftMinEvictableIdle(config.getSoftMinEvictableIdleDuration()); final EvictionPolicy<T> policy = config.getEvictionPolicy(); if (policy == null) { // Use the class name (pre-2.6.0 compatible) @@ -1235,7 +1305,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { // Otherwise, use the class (2.6.0 feature) setEvictionPolicy(policy); } - setEvictorShutdownTimeout(config.getEvictorShutdownTimeout()); + setEvictorShutdownTimeout(config.getEvictorShutdownTimeoutDuration()); } /** @@ -1323,7 +1393,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * @since 2.10.0 */ public final void setEvictorShutdownTimeout(final Duration evictorShutdownTimeout) { - this.evictorShutdownTimeout = PoolImplUtils.nonNull(evictorShutdownTimeout, BaseObjectPoolConfig.DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT); + this.evictorShutdownTimeoutDuration = PoolImplUtils.nonNull(evictorShutdownTimeout, BaseObjectPoolConfig.DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT); } /** @@ -1385,7 +1455,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * @see #setBlockWhenExhausted * @since 2.11.0 */ - public final void setMaxWaitDuration(final Duration maxWaitDuration) { + public final void setMaxWait(final Duration maxWaitDuration) { this.maxWaitDuration = PoolImplUtils.nonNull(maxWaitDuration, BaseObjectPoolConfig.DEFAULT_MAX_WAIT); } @@ -1402,11 +1472,11 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * * @see #getMaxWaitDuration * @see #setBlockWhenExhausted - * @deprecated Use {@link #setMaxWaitDuration}. + * @deprecated Use {@link #setMaxWait}. */ @Deprecated public final void setMaxWaitMillis(final long maxWaitMillis) { - setMaxWaitDuration(Duration.ofMillis(maxWaitMillis)); + setMaxWait(Duration.ofMillis(maxWaitMillis)); } /** @@ -1435,10 +1505,30 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * * @see #getMinEvictableIdleTime * @see #setTimeBetweenEvictionRuns + * @since 2.11.0 + */ + public final void setMinEvictableIdle(final Duration minEvictableIdleTime) { + this.minEvictableIdleDuration = PoolImplUtils.nonNull(minEvictableIdleTime, BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_DURATION); + } + + /** + * 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 {@link #setTimeBetweenEvictionRuns(Duration)}). When non-positive, + * no objects will be evicted from the pool due to idle time alone. + * + * @param minEvictableIdleTime + * minimum amount of time an object may sit idle in the pool + * before it is eligible for eviction + * + * @see #getMinEvictableIdleTime + * @see #setTimeBetweenEvictionRuns * @since 2.10.0 + * @deprecated Use {@link #setMinEvictableIdle(Duration)}. */ + @Deprecated public final void setMinEvictableIdleTime(final Duration minEvictableIdleTime) { - this.minEvictableIdleTime = PoolImplUtils.nonNull(minEvictableIdleTime, BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME); + this.minEvictableIdleDuration = PoolImplUtils.nonNull(minEvictableIdleTime, BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_DURATION); } /** @@ -1496,14 +1586,36 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * available * * @see #getSoftMinEvictableIdleTimeMillis + * @since 2.11.0 + */ + public final void setSoftMinEvictableIdle(final Duration softMinEvictableIdleTime) { + this.softMinEvictableIdleDuration = PoolImplUtils.nonNull(softMinEvictableIdleTime, BaseObjectPoolConfig.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_DURATION); + } + + /** + * 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 {@link #setTimeBetweenEvictionRuns(Duration)}), + * with the extra condition that at least {@code minIdle} object + * instances remain in the pool. This setting is overridden by + * {@link #getMinEvictableIdleTime} (that is, if + * {@link #getMinEvictableIdleTime} is positive, then + * {@link #getSoftMinEvictableIdleTime} is ignored). + * + * @param softMinEvictableIdleTime + * minimum amount of time an object may sit idle in the pool + * before it is eligible for eviction if minIdle instances are + * available + * + * @see #getSoftMinEvictableIdleTimeMillis * @since 2.10.0 + * @deprecated Use {@link #setSoftMinEvictableIdle(Duration)}. */ + @Deprecated public final void setSoftMinEvictableIdleTime(final Duration softMinEvictableIdleTime) { - this.softMinEvictableIdleTime = PoolImplUtils.nonNull(softMinEvictableIdleTime, BaseObjectPoolConfig.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME); + this.softMinEvictableIdleDuration = PoolImplUtils.nonNull(softMinEvictableIdleTime, BaseObjectPoolConfig.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_DURATION); } - // Inner classes - /** * 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 - @@ -1520,7 +1632,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * available * * @see #getSoftMinEvictableIdleTimeMillis - * @deprecated Use {@link #setSoftMinEvictableIdleTime(Duration)}. + * @deprecated Use {@link #setSoftMinEvictableIdle(Duration)}. */ @Deprecated public final void setSoftMinEvictableIdleTimeMillis(final long softMinEvictableIdleTimeMillis) { @@ -1627,8 +1739,8 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * @since 2.10.0 */ public final void setTimeBetweenEvictionRuns(final Duration timeBetweenEvictionRuns) { - this.timeBetweenEvictionRuns = PoolImplUtils.nonNull(timeBetweenEvictionRuns, BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS); - startEvictor(this.timeBetweenEvictionRuns); + this.durationBetweenEvictionRuns = PoolImplUtils.nonNull(timeBetweenEvictionRuns, BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS); + startEvictor(this.durationBetweenEvictionRuns); } /** @@ -1669,14 +1781,14 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { } } else if (isPositiverDelay) { // Stop or restart of existing evictor: Restart synchronized (EvictionTimer.class) { // Ensure no cancel can happen between cancel / schedule calls - EvictionTimer.cancel(evictor, evictorShutdownTimeout, true); + EvictionTimer.cancel(evictor, evictorShutdownTimeoutDuration, true); evictor = null; evictionIterator = null; evictor = new Evictor(); EvictionTimer.schedule(evictor, delay, delay); } } else { // Stopping evictor - EvictionTimer.cancel(evictor, evictorShutdownTimeout, false); + EvictionTimer.cancel(evictor, evictorShutdownTimeoutDuration, false); } } } @@ -1716,7 +1828,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { builder.append(maxTotal); builder.append(", blockWhenExhausted="); builder.append(blockWhenExhausted); - builder.append(", maxWaitMillis="); + builder.append(", maxWaitDuration="); builder.append(maxWaitDuration); builder.append(", lifo="); builder.append(lifo); @@ -1731,13 +1843,13 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { builder.append(", testWhileIdle="); builder.append(testWhileIdle); builder.append(", timeBetweenEvictionRunsMillis="); - builder.append(timeBetweenEvictionRuns); + builder.append(durationBetweenEvictionRuns); builder.append(", numTestsPerEvictionRun="); builder.append(numTestsPerEvictionRun); - builder.append(", minEvictableIdleTimeMillis="); - builder.append(minEvictableIdleTime); - builder.append(", softMinEvictableIdleTimeMillis="); - builder.append(softMinEvictableIdleTime); + builder.append(", minEvictableIdleTimeDuration="); + builder.append(minEvictableIdleDuration); + builder.append(", softMinEvictableIdleTimeDuration="); + builder.append(softMinEvictableIdleDuration); builder.append(", evictionPolicy="); builder.append(evictionPolicy); builder.append(", closeLock="); @@ -1788,7 +1900,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { */ final void updateStatsBorrow(final PooledObject<T> p, final Duration waitDuration) { borrowedCount.incrementAndGet(); - idleTimes.add(p.getIdleTime()); + idleTimes.add(p.getIdleDuration()); waitTimes.add(waitDuration); final long waitTimeMillis = waitDuration.toMillis(); diff --git a/src/main/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java b/src/main/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java index 4f9791b..195c916 100644 --- a/src/main/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java +++ b/src/main/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java @@ -65,30 +65,42 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon public static final Duration DEFAULT_MAX_WAIT = Duration.ofMillis(DEFAULT_MAX_WAIT_MILLIS); /** - * The default value for the {@code minEvictableIdleTime} + * The default value for the {@code minEvictableIdleDuration} * configuration attribute. - * @see GenericObjectPool#getMinEvictableIdleTimeMillis() - * @see GenericKeyedObjectPool#getMinEvictableIdleTimeMillis() + * @see GenericObjectPool#getMinEvictableIdleDuration() + * @see GenericKeyedObjectPool#getMinEvictableIdleDuration() * @deprecated Use {@link #DEFAULT_MIN_EVICTABLE_IDLE_TIME}. */ @Deprecated public static final long DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS = 1000L * 60L * 30L; /** - * The default value for the {@code minEvictableIdleTime} + * The default value for the {@code minEvictableIdleDuration} * configuration attribute. - * @see GenericObjectPool#getMinEvictableIdleTime() - * @see GenericKeyedObjectPool#getMinEvictableIdleTime() + * @see GenericObjectPool#getMinEvictableIdleDuration() + * @see GenericKeyedObjectPool#getMinEvictableIdleDuration() + * @since 2.11.0 + */ + public static final Duration DEFAULT_MIN_EVICTABLE_IDLE_DURATION = + Duration.ofMillis(DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS); + + /** + * The default value for the {@code minEvictableIdleDuration} + * configuration attribute. + * @see GenericObjectPool#getMinEvictableIdleDuration() + * @see GenericKeyedObjectPool#getMinEvictableIdleDuration() * @since 2.10.0 + * @deprecated Use {@link #DEFAULT_MIN_EVICTABLE_IDLE_DURATION}. */ + @Deprecated public static final Duration DEFAULT_MIN_EVICTABLE_IDLE_TIME = Duration.ofMillis(DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS); /** * The default value for the {@code softMinEvictableIdleTime} * configuration attribute. - * @see GenericObjectPool#getSoftMinEvictableIdleTimeMillis() - * @see GenericKeyedObjectPool#getSoftMinEvictableIdleTimeMillis() + * @see GenericObjectPool#getSoftMinEvictableIdleDuration() + * @see GenericKeyedObjectPool#getSoftMinEvictableIdleDuration() * @deprecated Use {@link #DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME}. */ @Deprecated @@ -97,18 +109,30 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon /** * The default value for the {@code softMinEvictableIdleTime} * configuration attribute. - * @see GenericObjectPool#getSoftMinEvictableIdleTime() - * @see GenericKeyedObjectPool#getSoftMinEvictableIdleTime() + * @see GenericObjectPool#getSoftMinEvictableIdleDuration() + * @see GenericKeyedObjectPool#getSoftMinEvictableIdleDuration() * @since 2.10.0 + * @deprecated Use {@link #DEFAULT_SOFT_MIN_EVICTABLE_IDLE_DURATION}. */ + @Deprecated public static final Duration DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME = Duration.ofMillis(DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS); /** + * The default value for the {@code softMinEvictableIdleTime} + * configuration attribute. + * @see GenericObjectPool#getSoftMinEvictableIdleDuration() + * @see GenericKeyedObjectPool#getSoftMinEvictableIdleDuration() + * @since 2.11.0 + */ + public static final Duration DEFAULT_SOFT_MIN_EVICTABLE_IDLE_DURATION = + Duration.ofMillis(DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS); + + /** * The default value for {@code evictorShutdownTimeout} configuration * attribute. - * @see GenericObjectPool#getEvictorShutdownTimeoutMillis() - * @see GenericKeyedObjectPool#getEvictorShutdownTimeoutMillis() + * @see GenericObjectPool#getEvictorShutdownTimeoutDuration() + * @see GenericKeyedObjectPool#getEvictorShutdownTimeoutDuration() * @deprecated Use {@link #DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT}. */ @Deprecated @@ -117,8 +141,8 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon /** * The default value for {@code evictorShutdownTimeout} configuration * attribute. - * @see GenericObjectPool#getEvictorShutdownTimeout() - * @see GenericKeyedObjectPool#getEvictorShutdownTimeout() + * @see GenericObjectPool#getEvictorShutdownTimeoutDuration() + * @see GenericKeyedObjectPool#getEvictorShutdownTimeoutDuration() * @since 2.10.0 */ public static final Duration DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT = @@ -165,8 +189,8 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon /** * The default value for the {@code timeBetweenEvictionRuns} * configuration attribute. - * @see GenericObjectPool#getTimeBetweenEvictionRunsMillis() - * @see GenericKeyedObjectPool#getTimeBetweenEvictionRunsMillis() + * @see GenericObjectPool#getDurationBetweenEvictionRuns() + * @see GenericKeyedObjectPool#getDurationBetweenEvictionRuns() * @deprecated Use {@link #DEFAULT_TIME_BETWEEN_EVICTION_RUNS}. */ @Deprecated @@ -175,8 +199,8 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon /** * The default value for the {@code timeBetweenEvictionRuns} * configuration attribute. - * @see GenericObjectPool#getTimeBetweenEvictionRuns() - * @see GenericKeyedObjectPool#getTimeBetweenEvictionRuns() + * @see GenericObjectPool#getDurationBetweenEvictionRuns() + * @see GenericKeyedObjectPool#getDurationBetweenEvictionRuns() */ public static final Duration DEFAULT_TIME_BETWEEN_EVICTION_RUNS = Duration .ofMillis(DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS); @@ -226,11 +250,11 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon private Duration maxWaitDuration = DEFAULT_MAX_WAIT; - private Duration minEvictableIdleTime = DEFAULT_MIN_EVICTABLE_IDLE_TIME; + private Duration minEvictableIdleDuration = DEFAULT_MIN_EVICTABLE_IDLE_TIME; private Duration evictorShutdownTimeoutDuration = DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT; - private Duration softMinEvictableIdleTime = DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME; + private Duration softMinEvictableIdleDuration = DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME; private int numTestsPerEvictionRun = DEFAULT_NUM_TESTS_PER_EVICTION_RUN; @@ -246,7 +270,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon private boolean testWhileIdle = DEFAULT_TEST_WHILE_IDLE; - private Duration timeBetweenEvictionRuns = DEFAULT_TIME_BETWEEN_EVICTION_RUNS; + private Duration durationBetweenEvictionRuns = DEFAULT_TIME_BETWEEN_EVICTION_RUNS; private boolean blockWhenExhausted = DEFAULT_BLOCK_WHEN_EXHAUSTED; @@ -308,10 +332,12 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * @return The current setting of {@code evictorShutdownTimeout} for * this configuration instance * - * @see GenericObjectPool#getEvictorShutdownTimeout() - * @see GenericKeyedObjectPool#getEvictorShutdownTimeout() + * @see GenericObjectPool#getEvictorShutdownTimeoutDuration() + * @see GenericKeyedObjectPool#getEvictorShutdownTimeoutDuration() * @since 2.10.0 + * @deprecated Use {@link #getEvictorShutdownTimeoutDuration()}. */ + @Deprecated public Duration getEvictorShutdownTimeout() { return evictorShutdownTimeoutDuration; } @@ -323,8 +349,23 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * @return The current setting of {@code evictorShutdownTimeout} for * this configuration instance * - * @see GenericObjectPool#getEvictorShutdownTimeoutMillis() - * @see GenericKeyedObjectPool#getEvictorShutdownTimeoutMillis() + * @see GenericObjectPool#getEvictorShutdownTimeoutDuration() + * @see GenericKeyedObjectPool#getEvictorShutdownTimeoutDuration() + * @since 2.11.0 + */ + public Duration getEvictorShutdownTimeoutDuration() { + return evictorShutdownTimeoutDuration; + } + + /** + * Gets the value for the {@code evictorShutdownTimeout} configuration + * attribute for pools created with this configuration instance. + * + * @return The current setting of {@code evictorShutdownTimeout} for + * this configuration instance + * + * @see GenericObjectPool#getEvictorShutdownTimeoutDuration() + * @see GenericKeyedObjectPool#getEvictorShutdownTimeoutDuration() * @deprecated Use {@link #getEvictorShutdownTimeout()}. */ @Deprecated @@ -418,8 +459,8 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * @return The current setting of {@code maxWait} for this * configuration instance * - * @see GenericObjectPool#getMaxWaitMillis() - * @see GenericKeyedObjectPool#getMaxWaitMillis() + * @see GenericObjectPool#getMaxWaitDuration() + * @see GenericKeyedObjectPool#getMaxWaitDuration() * @deprecated Use {@link #getMaxWaitDuration()}. */ @Deprecated @@ -434,12 +475,29 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * @return The current setting of {@code minEvictableIdleTime} for * this configuration instance * - * @see GenericObjectPool#getMinEvictableIdleTime() - * @see GenericKeyedObjectPool#getMinEvictableIdleTime() + * @see GenericObjectPool#getMinEvictableIdleDuration() + * @see GenericKeyedObjectPool#getMinEvictableIdleDuration() + * @since 2.11.0 + */ + public Duration getMinEvictableIdleDuration() { + return minEvictableIdleDuration; + } + + /** + * Gets the value for the {@code minEvictableIdleTime} configuration + * attribute for pools created with this configuration instance. + * + * @return The current setting of {@code minEvictableIdleTime} for + * this configuration instance + * + * @see GenericObjectPool#getMinEvictableIdleDuration() + * @see GenericKeyedObjectPool#getMinEvictableIdleDuration() * @since 2.10.0 + * @deprecated Use {@link #getMinEvictableIdleDuration()}. */ + @Deprecated public Duration getMinEvictableIdleTime() { - return minEvictableIdleTime; + return minEvictableIdleDuration; } /** @@ -449,13 +507,13 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * @return The current setting of {@code minEvictableIdleTime} for * this configuration instance * - * @see GenericObjectPool#getMinEvictableIdleTimeMillis() - * @see GenericKeyedObjectPool#getMinEvictableIdleTimeMillis() + * @see GenericObjectPool#getMinEvictableIdleDuration() + * @see GenericKeyedObjectPool#getMinEvictableIdleDuration() * @deprecated Use {@link #getMinEvictableIdleTime()}. */ @Deprecated public long getMinEvictableIdleTimeMillis() { - return minEvictableIdleTime.toMillis(); + return minEvictableIdleDuration.toMillis(); } /** @@ -480,12 +538,30 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * @return The current setting of {@code softMinEvictableIdleTime} * for this configuration instance * - * @see GenericObjectPool#getSoftMinEvictableIdleTime() - * @see GenericKeyedObjectPool#getSoftMinEvictableIdleTime() + * @see GenericObjectPool#getSoftMinEvictableIdleDuration() + * @see GenericKeyedObjectPool#getSoftMinEvictableIdleDuration() + * @since 2.11.0 + */ + public Duration getSoftMinEvictableIdleDuration() { + return softMinEvictableIdleDuration; + } + + /** + * Gets the value for the {@code softMinEvictableIdleTime} + * configuration attribute for pools created with this configuration + * instance. + * + * @return The current setting of {@code softMinEvictableIdleTime} + * for this configuration instance + * + * @see GenericObjectPool#getSoftMinEvictableIdleDuration() + * @see GenericKeyedObjectPool#getSoftMinEvictableIdleDuration() * @since 2.10.0 + * @deprecated Use {@link #getSoftMinEvictableIdleDuration()}. */ + @Deprecated public Duration getSoftMinEvictableIdleTime() { - return softMinEvictableIdleTime; + return softMinEvictableIdleDuration; } /** @@ -496,13 +572,13 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * @return The current setting of {@code softMinEvictableIdleTime} * for this configuration instance * - * @see GenericObjectPool#getSoftMinEvictableIdleTime() - * @see GenericKeyedObjectPool#getSoftMinEvictableIdleTime() - * @deprecated Use {@link #getSoftMinEvictableIdleTime()}. + * @see GenericObjectPool#getSoftMinEvictableIdleDuration() + * @see GenericKeyedObjectPool#getSoftMinEvictableIdleDuration() + * @deprecated Use {@link #getSoftMinEvictableIdleDuration()}. */ @Deprecated public long getSoftMinEvictableIdleTimeMillis() { - return softMinEvictableIdleTime.toMillis(); + return softMinEvictableIdleDuration.toMillis(); } /** @@ -570,12 +646,29 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * @return The current setting of {@code timeBetweenEvictionRuns} for * this configuration instance * - * @see GenericObjectPool#getTimeBetweenEvictionRuns() - * @see GenericKeyedObjectPool#getTimeBetweenEvictionRuns() + * @see GenericObjectPool#getDurationBetweenEvictionRuns() + * @see GenericKeyedObjectPool#getDurationBetweenEvictionRuns() + * @since 2.11.0 + */ + public Duration getDurationBetweenEvictionRuns() { + return durationBetweenEvictionRuns; + } + + /** + * Gets the value for the {@code timeBetweenEvictionRuns} configuration + * attribute for pools created with this configuration instance. + * + * @return The current setting of {@code timeBetweenEvictionRuns} for + * this configuration instance + * + * @see GenericObjectPool#getDurationBetweenEvictionRuns() + * @see GenericKeyedObjectPool#getDurationBetweenEvictionRuns() * @since 2.10.0 + * @deprecated Use {@link #getDurationBetweenEvictionRuns()}. */ + @Deprecated public Duration getTimeBetweenEvictionRuns() { - return timeBetweenEvictionRuns; + return durationBetweenEvictionRuns; } /** @@ -585,13 +678,13 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * @return The current setting of {@code timeBetweenEvictionRuns} for * this configuration instance * - * @see GenericObjectPool#getTimeBetweenEvictionRunsMillis() - * @see GenericKeyedObjectPool#getTimeBetweenEvictionRunsMillis() - * @deprecated Use {@link #getTimeBetweenEvictionRuns()}. + * @see GenericObjectPool#getDurationBetweenEvictionRuns() + * @see GenericKeyedObjectPool#getDurationBetweenEvictionRuns() + * @deprecated Use {@link #getDurationBetweenEvictionRuns()}. */ @Deprecated public long getTimeBetweenEvictionRunsMillis() { - return timeBetweenEvictionRuns.toMillis(); + return durationBetweenEvictionRuns.toMillis(); } /** @@ -645,8 +738,8 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * {@code evictorShutdownTimeout} for this configuration * instance * - * @see GenericObjectPool#getEvictorShutdownTimeout() - * @see GenericKeyedObjectPool#getEvictorShutdownTimeout() + * @see GenericObjectPool#getEvictorShutdownTimeoutDuration() + * @see GenericKeyedObjectPool#getEvictorShutdownTimeoutDuration() * @since 2.10.0 * @deprecated Use {@link #setEvictorShutdownTimeoutDuration(Duration)}. */ @@ -663,13 +756,12 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * {@code evictorShutdownTimeout} for this configuration * instance * - * @see GenericObjectPool#getEvictorShutdownTimeout() - * @see GenericKeyedObjectPool#getEvictorShutdownTimeout() + * @see GenericObjectPool#getEvictorShutdownTimeoutDuration() + * @see GenericKeyedObjectPool#getEvictorShutdownTimeoutDuration() * @since 2.11.0 */ public void setEvictorShutdownTimeoutDuration(final Duration evictorShutdownTimeoutDuration) { - this.evictorShutdownTimeoutDuration = PoolImplUtils.nonNull(evictorShutdownTimeoutDuration, - DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT); + this.evictorShutdownTimeoutDuration = PoolImplUtils.nonNull(evictorShutdownTimeoutDuration, DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT); } /** @@ -680,8 +772,8 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * {@code evictorShutdownTimeout} for this configuration * instance * - * @see GenericObjectPool#getEvictorShutdownTimeoutMillis() - * @see GenericKeyedObjectPool#getEvictorShutdownTimeoutMillis() + * @see GenericObjectPool#getEvictorShutdownTimeoutDuration() + * @see GenericKeyedObjectPool#getEvictorShutdownTimeoutDuration() * @deprecated Use {@link #setEvictorShutdownTimeoutDuration(Duration)}. */ @Deprecated @@ -790,12 +882,12 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * @param minEvictableIdleTime The new setting of * {@code minEvictableIdleTime} for this configuration instance * - * @see GenericObjectPool#getMinEvictableIdleTime() - * @see GenericKeyedObjectPool#getMinEvictableIdleTime() + * @see GenericObjectPool#getMinEvictableIdleDuration() + * @see GenericKeyedObjectPool#getMinEvictableIdleDuration() * @since 2.10.0 */ public void setMinEvictableIdleTime(final Duration minEvictableIdleTime) { - this.minEvictableIdleTime = PoolImplUtils.nonNull(minEvictableIdleTime, DEFAULT_MIN_EVICTABLE_IDLE_TIME); + this.minEvictableIdleDuration = PoolImplUtils.nonNull(minEvictableIdleTime, DEFAULT_MIN_EVICTABLE_IDLE_TIME); } /** @@ -805,13 +897,13 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * @param minEvictableIdleTimeMillis The new setting of * {@code minEvictableIdleTime} for this configuration instance * - * @see GenericObjectPool#getMinEvictableIdleTimeMillis() - * @see GenericKeyedObjectPool#getMinEvictableIdleTimeMillis() + * @see GenericObjectPool#getMinEvictableIdleDuration() + * @see GenericKeyedObjectPool#getMinEvictableIdleDuration() * @deprecated Use {@link #setMinEvictableIdleTime(Duration)}. */ @Deprecated public void setMinEvictableIdleTimeMillis(final long minEvictableIdleTimeMillis) { - this.minEvictableIdleTime = Duration.ofMillis(minEvictableIdleTimeMillis); + this.minEvictableIdleDuration = Duration.ofMillis(minEvictableIdleTimeMillis); } /** @@ -837,12 +929,12 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * {@code softMinEvictableIdleTime} for this configuration * instance * - * @see GenericObjectPool#getSoftMinEvictableIdleTime() - * @see GenericKeyedObjectPool#getSoftMinEvictableIdleTime() + * @see GenericObjectPool#getSoftMinEvictableIdleDuration() + * @see GenericKeyedObjectPool#getSoftMinEvictableIdleDuration() * @since 2.10.0 */ public void setSoftMinEvictableIdleTime(final Duration softMinEvictableIdleTime) { - this.softMinEvictableIdleTime = PoolImplUtils.nonNull(softMinEvictableIdleTime, DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME); + this.softMinEvictableIdleDuration = PoolImplUtils.nonNull(softMinEvictableIdleTime, DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME); } /** @@ -854,8 +946,8 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * {@code softMinEvictableIdleTime} for this configuration * instance * - * @see GenericObjectPool#getSoftMinEvictableIdleTimeMillis() - * @see GenericKeyedObjectPool#getSoftMinEvictableIdleTimeMillis() + * @see GenericObjectPool#getSoftMinEvictableIdleDuration() + * @see GenericKeyedObjectPool#getSoftMinEvictableIdleDuration() * @deprecated Use {@link #setSoftMinEvictableIdleTime(Duration)}. */ @Deprecated @@ -930,12 +1022,12 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * {@code timeBetweenEvictionRuns} for this configuration * instance * - * @see GenericObjectPool#getTimeBetweenEvictionRuns() - * @see GenericKeyedObjectPool#getTimeBetweenEvictionRuns() + * @see GenericObjectPool#getDurationBetweenEvictionRuns() + * @see GenericKeyedObjectPool#getDurationBetweenEvictionRuns() * @since 2.10.0 */ public void setTimeBetweenEvictionRuns(final Duration timeBetweenEvictionRuns) { - this.timeBetweenEvictionRuns = PoolImplUtils.nonNull(timeBetweenEvictionRuns, DEFAULT_TIME_BETWEEN_EVICTION_RUNS); + this.durationBetweenEvictionRuns = PoolImplUtils.nonNull(timeBetweenEvictionRuns, DEFAULT_TIME_BETWEEN_EVICTION_RUNS); } /** @@ -946,8 +1038,8 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * {@code timeBetweenEvictionRuns} for this configuration * instance * - * @see GenericObjectPool#getTimeBetweenEvictionRunsMillis() - * @see GenericKeyedObjectPool#getTimeBetweenEvictionRunsMillis() + * @see GenericObjectPool#getDurationBetweenEvictionRuns() + * @see GenericKeyedObjectPool#getDurationBetweenEvictionRuns() * @deprecated Use {@link #setTimeBetweenEvictionRuns(Duration)}. */ @Deprecated @@ -964,9 +1056,9 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon builder.append(", maxWaitMillis="); builder.append(maxWaitDuration); builder.append(", minEvictableIdleTime="); - builder.append(minEvictableIdleTime); + builder.append(minEvictableIdleDuration); builder.append(", softMinEvictableIdleTime="); - builder.append(softMinEvictableIdleTime); + builder.append(softMinEvictableIdleDuration); builder.append(", numTestsPerEvictionRun="); builder.append(numTestsPerEvictionRun); builder.append(", evictionPolicyClassName="); @@ -980,7 +1072,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon builder.append(", testWhileIdle="); builder.append(testWhileIdle); builder.append(", timeBetweenEvictionRuns="); - builder.append(timeBetweenEvictionRuns); + builder.append(durationBetweenEvictionRuns); builder.append(", blockWhenExhausted="); builder.append(blockWhenExhausted); builder.append(", jmxEnabled="); diff --git a/src/main/java/org/apache/commons/pool2/impl/DefaultEvictionPolicy.java b/src/main/java/org/apache/commons/pool2/impl/DefaultEvictionPolicy.java index 30ca95d..caf5dfe 100644 --- a/src/main/java/org/apache/commons/pool2/impl/DefaultEvictionPolicy.java +++ b/src/main/java/org/apache/commons/pool2/impl/DefaultEvictionPolicy.java @@ -19,33 +19,36 @@ package org.apache.commons.pool2.impl; import org.apache.commons.pool2.PooledObject; /** - * Provides the default implementation of {@link EvictionPolicy} used by the - * pools. Objects will be evicted if the following conditions are met: + * Provides the default implementation of {@link EvictionPolicy} used by the pools. + * <p> + * Objects will be evicted if the following conditions are met: + * </p> * <ul> - * <li>the object has been idle longer than - * {@link GenericObjectPool#getMinEvictableIdleTime()} / - * {@link GenericKeyedObjectPool#getMinEvictableIdleTime()}</li> - * <li>there are more than {@link GenericObjectPool#getMinIdle()} / + * <li>The object has been idle longer than + * {@link GenericObjectPool#getMinEvictableIdleDuration()} / + * {@link GenericKeyedObjectPool#getMinEvictableIdleDuration()}</li> + * <li>There are more than {@link GenericObjectPool#getMinIdle()} / * {@link GenericKeyedObjectPoolConfig#getMinIdlePerKey()} idle objects in * the pool and the object has been idle for longer than - * {@link GenericObjectPool#getSoftMinEvictableIdleTime()} / - * {@link GenericKeyedObjectPool#getSoftMinEvictableIdleTime()} + * {@link GenericObjectPool#getSoftMinEvictableIdleDuration()} / + * {@link GenericKeyedObjectPool#getSoftMinEvictableIdleDuration()} * </ul> * <p> * This class is immutable and thread-safe. * </p> * - * @param <T> the type of objects in the pool + * @param <T> the type of objects in the pool. * * @since 2.0 */ public class DefaultEvictionPolicy<T> implements EvictionPolicy<T> { @Override - public boolean evict(final EvictionConfig config, final PooledObject<T> underTest, - final int idleCount) { - return (config.getIdleSoftEvictTimeDuration().compareTo(underTest.getIdleTime()) < 0 && + public boolean evict(final EvictionConfig config, final PooledObject<T> underTest, final int idleCount) { + // @formatter:off + return (config.getIdleSoftEvictDuration().compareTo(underTest.getIdleDuration()) < 0 && config.getMinIdle() < idleCount) || - config.getIdleEvictTimeDuration().compareTo(underTest.getIdleTime()) < 0; + config.getIdleEvictDuration().compareTo(underTest.getIdleDuration()) < 0; + // @formatter:on } } diff --git a/src/main/java/org/apache/commons/pool2/impl/DefaultPooledObjectInfo.java b/src/main/java/org/apache/commons/pool2/impl/DefaultPooledObjectInfo.java index 0087676..6294484 100644 --- a/src/main/java/org/apache/commons/pool2/impl/DefaultPooledObjectInfo.java +++ b/src/main/java/org/apache/commons/pool2/impl/DefaultPooledObjectInfo.java @@ -30,6 +30,8 @@ import org.apache.commons.pool2.PooledObject; */ public class DefaultPooledObjectInfo implements DefaultPooledObjectInfoMBean { + private static final String PATTERN = "yyyy-MM-dd HH:mm:ss Z"; + private final PooledObject<?> pooledObject; /** @@ -48,24 +50,23 @@ public class DefaultPooledObjectInfo implements DefaultPooledObjectInfoMBean { @Override public long getCreateTime() { - return pooledObject.getCreateTime(); + return pooledObject.getCreateInstant().toEpochMilli(); } @Override public String getCreateTimeFormatted() { - final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z"); - return sdf.format(Long.valueOf(pooledObject.getCreateTime())); + return getTimeFormatted(getCreateTime()); } @Override public long getLastBorrowTime() { - return pooledObject.getLastBorrowTime(); + return pooledObject.getLastBorrowInstant().toEpochMilli(); } + @Override public String getLastBorrowTimeFormatted() { - final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z"); - return sdf.format(Long.valueOf(pooledObject.getLastBorrowTime())); + return getTimeFormatted(getLastBorrowTime()); } @Override @@ -77,13 +78,12 @@ public class DefaultPooledObjectInfo implements DefaultPooledObjectInfoMBean { @Override public long getLastReturnTime() { - return pooledObject.getLastReturnTime(); + return pooledObject.getLastReturnInstant().toEpochMilli(); } @Override public String getLastReturnTimeFormatted() { - final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z"); - return sdf.format(Long.valueOf(pooledObject.getLastReturnTime())); + return getTimeFormatted(getLastReturnTime()); } @Override @@ -96,6 +96,10 @@ public class DefaultPooledObjectInfo implements DefaultPooledObjectInfoMBean { return pooledObject.getObject().getClass().getName(); } + private String getTimeFormatted(final long millis) { + return new SimpleDateFormat(PATTERN).format(Long.valueOf(millis)); + } + /** * @since 2.4.3 */ diff --git a/src/main/java/org/apache/commons/pool2/impl/EvictionConfig.java b/src/main/java/org/apache/commons/pool2/impl/EvictionConfig.java index f8788b8..a54868e 100644 --- a/src/main/java/org/apache/commons/pool2/impl/EvictionConfig.java +++ b/src/main/java/org/apache/commons/pool2/impl/EvictionConfig.java @@ -30,35 +30,27 @@ import java.time.Duration; */ public class EvictionConfig { - private static final Duration MAX_MILLIS_DURATION = Duration.ofMillis(Long.MAX_VALUE); - private final Duration idleEvictTime; - private final Duration idleSoftEvictTime; + private static final Duration MAX_DURATION = Duration.ofMillis(Long.MAX_VALUE); + private final Duration idleEvictDuration; + private final Duration idleSoftEvictDuration; private final int minIdle; /** * Creates a new eviction configuration with the specified parameters. * Instances are immutable. * - * @param poolIdleEvictTime Expected to be provided by - * {@link BaseGenericObjectPool#getMinEvictableIdleTime()} - * @param poolIdleSoftEvictTime Expected to be provided by - * {@link BaseGenericObjectPool#getSoftMinEvictableIdleTime()} + * @param idleEvictDuration Expected to be provided by + * {@link BaseGenericObjectPool#getMinEvictableIdleDuration()} + * @param idleSoftEvictDuration Expected to be provided by + * {@link BaseGenericObjectPool#getSoftMinEvictableIdleDuration()} * @param minIdle Expected to be provided by * {@link GenericObjectPool#getMinIdle()} or * {@link GenericKeyedObjectPool#getMinIdlePerKey()} * @since 2.10.0 */ - public EvictionConfig(final Duration poolIdleEvictTime, final Duration poolIdleSoftEvictTime, final int minIdle) { - if (PoolImplUtils.isPositive(poolIdleEvictTime)) { - idleEvictTime = poolIdleEvictTime; - } else { - idleEvictTime = MAX_MILLIS_DURATION; - } - if (PoolImplUtils.isPositive(poolIdleSoftEvictTime)) { - idleSoftEvictTime = poolIdleSoftEvictTime; - } else { - idleSoftEvictTime = MAX_MILLIS_DURATION; - } + public EvictionConfig(final Duration idleEvictDuration, final Duration idleSoftEvictDuration, final int minIdle) { + this.idleEvictDuration = PoolImplUtils.isPositive(idleEvictDuration) ? idleEvictDuration : MAX_DURATION; + this.idleSoftEvictDuration = PoolImplUtils.isPositive(idleSoftEvictDuration) ? idleSoftEvictDuration : MAX_DURATION; this.minIdle = minIdle; } @@ -66,18 +58,33 @@ public class EvictionConfig { * Creates a new eviction configuration with the specified parameters. * Instances are immutable. * - * @param poolIdleEvictTime Expected to be provided by - * {@link BaseGenericObjectPool#getMinEvictableIdleTimeMillis()} - * @param poolIdleSoftEvictTime Expected to be provided by - * {@link BaseGenericObjectPool#getSoftMinEvictableIdleTimeMillis()} + * @param poolIdleEvictMillis Expected to be provided by + * {@link BaseGenericObjectPool#getMinEvictableIdleDuration()} + * @param poolIdleSoftEvictMillis Expected to be provided by + * {@link BaseGenericObjectPool#getSoftMinEvictableIdleDuration()} * @param minIdle Expected to be provided by * {@link GenericObjectPool#getMinIdle()} or * {@link GenericKeyedObjectPool#getMinIdlePerKey()} * @deprecated Use {@link #EvictionConfig(Duration, Duration, int)}. */ @Deprecated - public EvictionConfig(final long poolIdleEvictTime, final long poolIdleSoftEvictTime, final int minIdle) { - this(Duration.ofMillis(poolIdleEvictTime), Duration.ofMillis(poolIdleSoftEvictTime), minIdle); + public EvictionConfig(final long poolIdleEvictMillis, final long poolIdleSoftEvictMillis, final int minIdle) { + this(Duration.ofMillis(poolIdleEvictMillis), Duration.ofMillis(poolIdleSoftEvictMillis), minIdle); + } + + /** + * Gets the {@code idleEvictTime} for this eviction configuration + * instance. + * <p> + * How the evictor behaves based on this value will be determined by the + * configured {@link EvictionPolicy}. + * </p> + * + * @return The {@code idleEvictTime}. + * @since 2.11.0 + */ + public Duration getIdleEvictDuration() { + return idleEvictDuration; } /** @@ -89,9 +96,11 @@ public class EvictionConfig { * </p> * * @return The {@code idleEvictTime} in milliseconds + * @deprecated Use {@link #getIdleEvictDuration()}. */ + @Deprecated public long getIdleEvictTime() { - return idleEvictTime.toMillis(); + return idleEvictDuration.toMillis(); } /** @@ -104,9 +113,11 @@ public class EvictionConfig { * * @return The {@code idleEvictTime}. * @since 2.10.0 + * @deprecated Use {@link #getIdleEvictDuration()}. */ + @Deprecated public Duration getIdleEvictTimeDuration() { - return idleEvictTime; + return idleEvictDuration; } /** @@ -118,9 +129,26 @@ public class EvictionConfig { * </p> * * @return The (@code idleSoftEvictTime} in milliseconds + * @since 2.11.0 */ + public Duration getIdleSoftEvictDuration() { + return idleSoftEvictDuration; + } + + /** + * Gets the {@code idleSoftEvictTime} for this eviction configuration + * instance. + * <p> + * How the evictor behaves based on this value will be determined by the + * configured {@link EvictionPolicy}. + * </p> + * + * @return The (@code idleSoftEvictTime} in milliseconds + * @deprecated Use {@link #getIdleSoftEvictDuration()}. + */ + @Deprecated public long getIdleSoftEvictTime() { - return idleSoftEvictTime.toMillis(); + return idleSoftEvictDuration.toMillis(); } /** @@ -132,9 +160,11 @@ public class EvictionConfig { * </p> * * @return The (@code idleSoftEvictTime} in milliseconds + * @deprecated Use {@link #getIdleSoftEvictDuration()}. */ + @Deprecated public Duration getIdleSoftEvictTimeDuration() { - return idleSoftEvictTime; + return idleSoftEvictDuration; } /** @@ -156,10 +186,10 @@ public class EvictionConfig { @Override public String toString() { final StringBuilder builder = new StringBuilder(); - builder.append("EvictionConfig [idleEvictTime="); - builder.append(idleEvictTime); - builder.append(", idleSoftEvictTime="); - builder.append(idleSoftEvictTime); + builder.append("EvictionConfig [idleEvictDuration="); + builder.append(idleEvictDuration); + builder.append(", idleSoftEvictDuration="); + builder.append(idleSoftEvictDuration); builder.append(", minIdle="); builder.append(minIdle); builder.append("]"); diff --git a/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java b/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java index f362a55..8ba70f9 100644 --- a/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java @@ -952,14 +952,14 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> synchronized (evictionLock) { final EvictionConfig evictionConfig = new EvictionConfig( - getMinEvictableIdleTime(), - getSoftMinEvictableIdleTime(), + getMinEvictableIdleDuration(), + getSoftMinEvictableIdleDuration(), getMinIdlePerKey()); final boolean testWhileIdle = getTestWhileIdle(); for (int i = 0, m = getNumTests(); i < m; i++) { - if(evictionIterator == null || !evictionIterator.hasNext()) { + if (evictionIterator == null || !evictionIterator.hasNext()) { if (evictionKeyIterator == null || !evictionKeyIterator.hasNext()) { final List<K> keyCopy = new ArrayList<>(); @@ -1128,7 +1128,7 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> /** * Gets the target for the minimum number of idle objects to maintain in * each of the keyed sub-pools. This setting only has an effect if it is - * positive and {@link #getTimeBetweenEvictionRuns()} is greater than + * positive and {@link #getDurationBetweenEvictionRuns()} is greater than * zero. If this is the case, an attempt is made to ensure that each * sub-pool has the required minimum number of instances during idle object * eviction runs. @@ -1145,10 +1145,7 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> @Override public int getMinIdlePerKey() { final int maxIdlePerKeySave = getMaxIdlePerKey(); - if (this.minIdlePerKey > maxIdlePerKeySave) { - return maxIdlePerKeySave; - } - return minIdlePerKey; + return this.minIdlePerKey > maxIdlePerKeySave ? maxIdlePerKeySave : minIdlePerKey; } @Override @@ -1459,15 +1456,13 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> final ObjectDeque<T> objectDeque = poolMap.get(key); if (objectDeque == null) { - throw new IllegalStateException( - "No keyed pool found under the given key."); + throw new IllegalStateException("No keyed pool found under the given key."); } final PooledObject<T> p = objectDeque.getAllObjects().get(new IdentityWrapper<>(obj)); if (p == null) { - throw new IllegalStateException( - "Returned object not currently part of this pool"); + throw new IllegalStateException("Returned object not currently part of this pool"); } markReturningState(p); @@ -1499,8 +1494,7 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> } if (!p.deallocate()) { - throw new IllegalStateException( - "Object has already been returned to this pool"); + throw new IllegalStateException("Object has already been returned to this pool"); } final int maxIdle = getMaxIdlePerKey(); @@ -1635,7 +1629,7 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> /** * Sets the target for the minimum number of idle objects to maintain in * each of the keyed sub-pools. This setting only has an effect if it is - * positive and {@link #getTimeBetweenEvictionRuns()} is greater than + * positive and {@link #getDurationBetweenEvictionRuns()} is greater than * zero. If this is the case, an attempt is made to ensure that each * sub-pool has the required minimum number of instances during idle object * eviction runs. diff --git a/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMXBean.java b/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMXBean.java index 1750a0f..884a42b 100644 --- a/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMXBean.java +++ b/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPoolMXBean.java @@ -125,8 +125,8 @@ public interface GenericKeyedObjectPoolMXBean<K> { int getMaxTotalPerKey(); /** - * See {@link GenericKeyedObjectPool#getMaxWaitMillis()} - * @return See {@link GenericKeyedObjectPool#getMaxWaitMillis()} + * See {@link GenericKeyedObjectPool#getMaxWaitDuration()} + * @return See {@link GenericKeyedObjectPool#getMaxWaitDuration()} */ long getMaxWaitMillis(); @@ -149,8 +149,8 @@ public interface GenericKeyedObjectPoolMXBean<K> { long getMeanIdleTimeMillis(); /** - * See {@link GenericKeyedObjectPool#getMinEvictableIdleTime()} - * @return See {@link GenericKeyedObjectPool#getMinEvictableIdleTime()} + * See {@link GenericKeyedObjectPool#getMinEvictableIdleDuration()} + * @return See {@link GenericKeyedObjectPool#getMinEvictableIdleDuration()} */ long getMinEvictableIdleTimeMillis(); @@ -257,8 +257,8 @@ public interface GenericKeyedObjectPoolMXBean<K> { boolean getTestWhileIdle(); /** - * See {@link GenericKeyedObjectPool#getTimeBetweenEvictionRuns()} - * @return See {@link GenericKeyedObjectPool#getTimeBetweenEvictionRuns()} + * See {@link GenericKeyedObjectPool#getDurationBetweenEvictionRuns} + * @return See {@link GenericKeyedObjectPool#getDurationBetweenEvictionRuns()} */ long getTimeBetweenEvictionRunsMillis(); diff --git a/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java b/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java index a53b2d3..9b7c59b 100644 --- a/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java @@ -516,7 +516,7 @@ public class GenericObjectPool<T> extends BaseGenericObjectPool<T> } final long localStartTimeMillis = System.currentTimeMillis(); - final long localMaxWaitTimeMillis = Math.max(getMaxWaitMillis(), 0); + final long localMaxWaitTimeMillis = Math.max(getMaxWaitDuration().toMillis(), 0); // Flag that indicates if create should: // - TRUE: call the factory to create an object @@ -673,8 +673,8 @@ public class GenericObjectPool<T> extends BaseGenericObjectPool<T> synchronized (evictionLock) { final EvictionConfig evictionConfig = new EvictionConfig( - getMinEvictableIdleTime(), - getSoftMinEvictableIdleTime(), + getMinEvictableIdleDuration(), + getSoftMinEvictableIdleDuration(), getMinIdle()); final boolean testWhileIdle = getTestWhileIdle(); @@ -830,7 +830,7 @@ public class GenericObjectPool<T> extends BaseGenericObjectPool<T> /** * Gets the target for the minimum number of idle objects to maintain in * the pool. This setting only has an effect if it is positive and - * {@link #getTimeBetweenEvictionRuns()} is greater than zero. If this + * {@link #getDurationBetweenEvictionRuns()} is greater than zero. If this * is the case, an attempt is made to ensure that the pool has the required * minimum number of instances during idle object eviction runs. * <p> @@ -1129,7 +1129,7 @@ public class GenericObjectPool<T> extends BaseGenericObjectPool<T> /** * Sets the target for the minimum number of idle objects to maintain in * the pool. This setting only has an effect if it is positive and - * {@link #getTimeBetweenEvictionRuns()} is greater than zero. If this + * {@link #getDurationBetweenEvictionRuns()} is greater than zero. If this * is the case, an attempt is made to ensure that the pool has the required * minimum number of instances during idle object eviction runs. * <p> @@ -1142,7 +1142,7 @@ public class GenericObjectPool<T> extends BaseGenericObjectPool<T> * * @see #getMinIdle() * @see #getMaxIdle() - * @see #getTimeBetweenEvictionRuns() + * @see #getDurationBetweenEvictionRuns() */ public void setMinIdle(final int minIdle) { this.minIdle = minIdle; @@ -1173,8 +1173,7 @@ public class GenericObjectPool<T> extends BaseGenericObjectPool<T> public void use(final T pooledObject) { final AbandonedConfig abandonedCfg = this.abandonedConfig; if (abandonedCfg != null && abandonedCfg.getUseUsageTracking()) { - final PooledObject<T> wrapper = allObjects.get(new IdentityWrapper<>(pooledObject)); - wrapper.use(); + allObjects.get(new IdentityWrapper<>(pooledObject)).use(); } } diff --git a/src/main/java/org/apache/commons/pool2/impl/GenericObjectPoolMXBean.java b/src/main/java/org/apache/commons/pool2/impl/GenericObjectPoolMXBean.java index 1c3129e..5f6d73a 100644 --- a/src/main/java/org/apache/commons/pool2/impl/GenericObjectPoolMXBean.java +++ b/src/main/java/org/apache/commons/pool2/impl/GenericObjectPoolMXBean.java @@ -119,8 +119,8 @@ public interface GenericObjectPoolMXBean { int getMaxTotal(); /** - * See {@link GenericObjectPool#getMaxWaitMillis()} - * @return See {@link GenericObjectPool#getMaxWaitMillis()} + * See {@link GenericObjectPool#getMaxWaitDuration()} + * @return See {@link GenericObjectPool#getMaxWaitDuration()} */ long getMaxWaitMillis(); @@ -145,8 +145,8 @@ public interface GenericObjectPoolMXBean { long getMeanIdleTimeMillis(); /** - * See {@link GenericObjectPool#getMinEvictableIdleTimeMillis()} - * @return See {@link GenericObjectPool#getMinEvictableIdleTimeMillis()} + * See {@link GenericObjectPool#getMinEvictableIdleDuration()} + * @return See {@link GenericObjectPool#getMinEvictableIdleDuration()} */ long getMinEvictableIdleTimeMillis(); @@ -193,8 +193,8 @@ public interface GenericObjectPoolMXBean { boolean getRemoveAbandonedOnMaintenance(); /** - * See {@link GenericObjectPool#getRemoveAbandonedTimeout()} - * @return See {@link GenericObjectPool#getRemoveAbandonedTimeout()} + * See {@link GenericObjectPool#getRemoveAbandonedTimeoutDuration()} + * @return See {@link GenericObjectPool#getRemoveAbandonedTimeoutDuration()} */ int getRemoveAbandonedTimeout(); @@ -232,8 +232,8 @@ public interface GenericObjectPoolMXBean { boolean getTestWhileIdle(); /** - * See {@link GenericObjectPool#getTimeBetweenEvictionRunsMillis()} - * @return See {@link GenericObjectPool#getTimeBetweenEvictionRunsMillis()} + * See {@link GenericObjectPool#getDurationBetweenEvictionRuns()} + * @return See {@link GenericObjectPool#getDurationBetweenEvictionRuns()} */ long getTimeBetweenEvictionRunsMillis(); diff --git a/src/main/java/org/apache/commons/pool2/impl/SecurityManagerCallStack.java b/src/main/java/org/apache/commons/pool2/impl/SecurityManagerCallStack.java index a43b076..1276e1d 100644 --- a/src/main/java/org/apache/commons/pool2/impl/SecurityManagerCallStack.java +++ b/src/main/java/org/apache/commons/pool2/impl/SecurityManagerCallStack.java @@ -22,7 +22,6 @@ import java.security.AccessController; import java.security.PrivilegedAction; import java.text.DateFormat; import java.text.SimpleDateFormat; -import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; diff --git a/src/test/java/org/apache/commons/pool2/PoolTest.java b/src/test/java/org/apache/commons/pool2/PoolTest.java index c3a884c..0290a7d 100644 --- a/src/test/java/org/apache/commons/pool2/PoolTest.java +++ b/src/test/java/org/apache/commons/pool2/PoolTest.java @@ -75,6 +75,7 @@ public class PoolTest { final PooledFooFactory pooledFooFactory = new PooledFooFactory(); try (GenericObjectPool<Foo> pool = new GenericObjectPool<>(pooledFooFactory, poolConfig)) { pool.setTimeBetweenEvictionRunsMillis(EVICTION_PERIOD_IN_MILLIS); + assertEquals(EVICTION_PERIOD_IN_MILLIS, pool.getDurationBetweenEvictionRuns().toMillis()); assertEquals(EVICTION_PERIOD_IN_MILLIS, pool.getTimeBetweenEvictionRuns().toMillis()); pool.setTimeBetweenEvictionRuns(Duration.ofMillis(EVICTION_PERIOD_IN_MILLIS)); assertEquals(EVICTION_PERIOD_IN_MILLIS, pool.getTimeBetweenEvictionRuns().toMillis()); diff --git a/src/test/java/org/apache/commons/pool2/impl/TestAbandonedKeyedObjectPool.java b/src/test/java/org/apache/commons/pool2/impl/TestAbandonedKeyedObjectPool.java index 0d850c9..2a58571 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestAbandonedKeyedObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestAbandonedKeyedObjectPool.java @@ -147,10 +147,13 @@ public class TestAbandonedKeyedObjectPool { // Uncomment the following line to enable logging: // abandonedConfig.setLogAbandoned(true); - abandonedConfig.setRemoveAbandonedOnBorrow(true); + // One second Duration. + abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND_DURATION); + assertEquals(TestConstants.ONE_SECOND_DURATION, abandonedConfig.getRemoveAbandonedTimeoutDuration()); + assertEquals(1, abandonedConfig.getRemoveAbandonedTimeout()); + // One second int (not millis). abandonedConfig.setRemoveAbandonedTimeout(1); - assertEquals(TestConstants.ONE_SECOND, abandonedConfig.getRemoveAbandonedTimeoutDuration()); - abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND); + assertEquals(TestConstants.ONE_SECOND_DURATION, abandonedConfig.getRemoveAbandonedTimeoutDuration()); assertEquals(1, abandonedConfig.getRemoveAbandonedTimeout()); pool = new GenericKeyedObjectPool<>( @@ -196,7 +199,7 @@ public class TestAbandonedKeyedObjectPool { public void testAbandonedInvalidate() throws Exception { abandonedConfig = new AbandonedConfig(); abandonedConfig.setRemoveAbandonedOnMaintenance(true); - abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND); + abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND_DURATION); pool.close(); // Unregister pool created by setup pool = new GenericKeyedObjectPool<>( // destroys take 200 ms @@ -228,7 +231,7 @@ public class TestAbandonedKeyedObjectPool { public void testAbandonedReturn() throws Exception { abandonedConfig = new AbandonedConfig(); abandonedConfig.setRemoveAbandonedOnBorrow(true); - abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND); + abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND_DURATION); pool.close(); // Unregister pool created by setup pool = new GenericKeyedObjectPool<>( new SimpleFactory(200, 0), @@ -304,7 +307,7 @@ public class TestAbandonedKeyedObjectPool { public void testDestroyModeAbandoned() throws Exception { abandonedConfig = new AbandonedConfig(); abandonedConfig.setRemoveAbandonedOnMaintenance(true); - abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND); + abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND_DURATION); pool.close(); // Unregister pool created by setup pool = new GenericKeyedObjectPool<>( // validate takes 1 second @@ -338,7 +341,7 @@ public class TestAbandonedKeyedObjectPool { public void testRemoveAbandonedWhileReturning() throws Exception { abandonedConfig = new AbandonedConfig(); abandonedConfig.setRemoveAbandonedOnMaintenance(true); - abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND); + abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND_DURATION); pool.close(); // Unregister pool created by setup pool = new GenericKeyedObjectPool<>( // validate takes 1 second @@ -367,7 +370,7 @@ public class TestAbandonedKeyedObjectPool { public void testStackTrace() throws Exception { abandonedConfig.setRemoveAbandonedOnMaintenance(true); abandonedConfig.setLogAbandoned(true); - abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND); + abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND_DURATION); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final BufferedOutputStream bos = new BufferedOutputStream(baos); final PrintWriter pw = new PrintWriter(bos); diff --git a/src/test/java/org/apache/commons/pool2/impl/TestAbandonedObjectPool.java b/src/test/java/org/apache/commons/pool2/impl/TestAbandonedObjectPool.java index 9b64560..d1cba90 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestAbandonedObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestAbandonedObjectPool.java @@ -228,10 +228,14 @@ public class TestAbandonedObjectPool { // abandonedConfig.setLogAbandoned(true); abandonedConfig.setRemoveAbandonedOnBorrow(true); + // One second Duration. + abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND_DURATION); + assertEquals(TestConstants.ONE_SECOND_DURATION, abandonedConfig.getRemoveAbandonedTimeoutDuration()); + assertEquals(1, abandonedConfig.getRemoveAbandonedTimeout()); // in seconds. + // One second int (not millis). abandonedConfig.setRemoveAbandonedTimeout(1); - assertEquals(TestConstants.ONE_SECOND, abandonedConfig.getRemoveAbandonedTimeoutDuration()); - abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND); - assertEquals(1, abandonedConfig.getRemoveAbandonedTimeout()); + assertEquals(TestConstants.ONE_SECOND_DURATION, abandonedConfig.getRemoveAbandonedTimeoutDuration()); + assertEquals(1, abandonedConfig.getRemoveAbandonedTimeout()); // in seconds. pool = new GenericObjectPool<>( new SimpleFactory(), @@ -276,7 +280,7 @@ public class TestAbandonedObjectPool { public void testAbandonedInvalidate() throws Exception { abandonedConfig = new AbandonedConfig(); abandonedConfig.setRemoveAbandonedOnMaintenance(true); - abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND); + abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND_DURATION); pool.close(); // Unregister pool created by setup pool = new GenericObjectPool<>( // destroys take 200 ms @@ -308,7 +312,7 @@ public class TestAbandonedObjectPool { public void testAbandonedReturn() throws Exception { abandonedConfig = new AbandonedConfig(); abandonedConfig.setRemoveAbandonedOnBorrow(true); - abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND); + abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND_DURATION); pool.close(); // Unregister pool created by setup pool = new GenericObjectPool<>( new SimpleFactory(200, 0), @@ -384,7 +388,7 @@ public class TestAbandonedObjectPool { public void testDestroyModeAbandoned() throws Exception { abandonedConfig = new AbandonedConfig(); abandonedConfig.setRemoveAbandonedOnMaintenance(true); - abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND); + abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND_DURATION); pool.close(); // Unregister pool created by setup pool = new GenericObjectPool<>( // validate takes 1 second @@ -418,7 +422,7 @@ public class TestAbandonedObjectPool { public void testRemoveAbandonedWhileReturning() throws Exception { abandonedConfig = new AbandonedConfig(); abandonedConfig.setRemoveAbandonedOnMaintenance(true); - abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND); + abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND_DURATION); pool.close(); // Unregister pool created by setup pool = new GenericObjectPool<>( // validate takes 1 second @@ -447,7 +451,7 @@ public class TestAbandonedObjectPool { public void testStackTrace() throws Exception { abandonedConfig.setRemoveAbandonedOnMaintenance(true); abandonedConfig.setLogAbandoned(true); - abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND); + abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND_DURATION); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final BufferedOutputStream bos = new BufferedOutputStream(baos); final PrintWriter pw = new PrintWriter(bos); diff --git a/src/test/java/org/apache/commons/pool2/impl/TestConstants.java b/src/test/java/org/apache/commons/pool2/impl/TestConstants.java index 59479ad..9d1a625 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestConstants.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestConstants.java @@ -19,10 +19,24 @@ package org.apache.commons.pool2.impl; import java.time.Duration; +/** + * Constants used in tests. + */ public class TestConstants { - public static final Duration ONE_SECOND = Duration.ofSeconds(1); - public static final Duration ONE_MINUTE = Duration.ofMinutes(1); - public static final Duration ONE_MILLISECOND = Duration.ofMillis(1); + /** + * A duration of one second. + */ + public static final Duration ONE_SECOND_DURATION = Duration.ofSeconds(1); + + /** + * A duration of one minute. + */ + public static final Duration ONE_MINUTE_DURATION = Duration.ofMinutes(1); + + /** + * A duration of one millisecond. + */ + public static final Duration ONE_MILLISECOND_DURATION = Duration.ofMillis(1); } diff --git a/src/test/java/org/apache/commons/pool2/impl/TestDefaultPooledObjectInfo.java b/src/test/java/org/apache/commons/pool2/impl/TestDefaultPooledObjectInfo.java index 2da2316..fd05ad4 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestDefaultPooledObjectInfo.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestDefaultPooledObjectInfo.java @@ -32,7 +32,7 @@ public class TestDefaultPooledObjectInfo { final AbandonedConfig abandonedConfig = new AbandonedConfig(); abandonedConfig.setRemoveAbandonedOnBorrow(true); - abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND); + abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND_DURATION); abandonedConfig.setLogAbandoned(true); try (final GenericObjectPool<String> pool = new GenericObjectPool<>(new SimpleFactory(), new GenericObjectPoolConfig<>(), abandonedConfig)) { diff --git a/src/test/java/org/apache/commons/pool2/impl/TestEvictionConfig.java b/src/test/java/org/apache/commons/pool2/impl/TestEvictionConfig.java index 2cfa0bc..95db65b 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestEvictionConfig.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestEvictionConfig.java @@ -31,8 +31,10 @@ public class TestEvictionConfig { public void testConstructor1s() { final EvictionConfig config = new EvictionConfig(Duration.ofMillis(1), Duration.ofMillis(1), 1); + assertEquals(1, config.getIdleEvictDuration().toMillis()); assertEquals(1, config.getIdleEvictTime()); assertEquals(1, config.getIdleEvictTimeDuration().toMillis()); + assertEquals(1, config.getIdleSoftEvictDuration().toMillis()); assertEquals(1, config.getIdleSoftEvictTime()); assertEquals(1, config.getIdleSoftEvictTimeDuration().toMillis()); assertEquals(1, config.getMinIdle()); @@ -42,8 +44,10 @@ public class TestEvictionConfig { public void testConstructorZerosDurations() { final EvictionConfig config = new EvictionConfig(Duration.ZERO, Duration.ZERO, 0); + assertEquals(Long.MAX_VALUE, config.getIdleEvictDuration().toMillis()); assertEquals(Long.MAX_VALUE, config.getIdleEvictTime()); assertEquals(Long.MAX_VALUE, config.getIdleEvictTimeDuration().toMillis()); + assertEquals(Long.MAX_VALUE, config.getIdleSoftEvictDuration().toMillis()); assertEquals(Long.MAX_VALUE, config.getIdleSoftEvictTime()); assertEquals(Long.MAX_VALUE, config.getIdleSoftEvictTimeDuration().toMillis()); assertEquals(0, config.getMinIdle()); @@ -54,8 +58,10 @@ public class TestEvictionConfig { @SuppressWarnings("deprecation") final EvictionConfig config = new EvictionConfig(0, 0, 0); + assertEquals(Long.MAX_VALUE, config.getIdleEvictDuration().toMillis()); assertEquals(Long.MAX_VALUE, config.getIdleEvictTime()); assertEquals(Long.MAX_VALUE, config.getIdleEvictTimeDuration().toMillis()); + assertEquals(Long.MAX_VALUE, config.getIdleSoftEvictDuration().toMillis()); assertEquals(Long.MAX_VALUE, config.getIdleSoftEvictTime()); assertEquals(Long.MAX_VALUE, config.getIdleSoftEvictTimeDuration().toMillis()); assertEquals(0, config.getMinIdle()); diff --git a/src/test/java/org/apache/commons/pool2/impl/TestEvictionTimer.java b/src/test/java/org/apache/commons/pool2/impl/TestEvictionTimer.java index b4664a5..a45e189 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestEvictionTimer.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestEvictionTimer.java @@ -53,7 +53,7 @@ public class TestEvictionTimer { // Start evictor #1 final BaseGenericObjectPool<String>.Evictor evictor1 = pool.new Evictor(); - EvictionTimer.schedule(evictor1, TestConstants.ONE_MINUTE, TestConstants.ONE_MINUTE); + EvictionTimer.schedule(evictor1, TestConstants.ONE_MINUTE_DURATION, TestConstants.ONE_MINUTE_DURATION); // Assert that eviction objects are correctly allocated // 1 - the evictor timer task is created @@ -71,7 +71,7 @@ public class TestEvictionTimer { // Start evictor #2 final BaseGenericObjectPool<String>.Evictor evictor2 = pool.new Evictor(); - EvictionTimer.schedule(evictor2, TestConstants.ONE_MINUTE, TestConstants.ONE_MINUTE); + EvictionTimer.schedule(evictor2, TestConstants.ONE_MINUTE_DURATION, TestConstants.ONE_MINUTE_DURATION); // Assert that eviction objects are correctly allocated // 1 - the evictor timer task is created diff --git a/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java b/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java index cd36876..c031d48 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java @@ -1193,6 +1193,8 @@ public class TestGenericKeyedObjectPool extends TestKeyedObjectPool { objPool.getMinEvictableIdleTimeMillis()); assertEquals(BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME, objPool.getMinEvictableIdleTime()); + assertEquals(BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME, + objPool.getMinEvictableIdleDuration()); // assertEquals(BaseObjectPoolConfig.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, objPool.getNumTestsPerEvictionRun()); assertEquals(Boolean.valueOf(BaseObjectPoolConfig.DEFAULT_TEST_ON_BORROW), @@ -1202,6 +1204,8 @@ public class TestGenericKeyedObjectPool extends TestKeyedObjectPool { assertEquals(Boolean.valueOf(BaseObjectPoolConfig.DEFAULT_TEST_WHILE_IDLE), Boolean.valueOf(objPool.getTestWhileIdle())); // + assertEquals(BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS, + objPool.getDurationBetweenEvictionRuns()); assertEquals(BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS, objPool.getTimeBetweenEvictionRunsMillis()); assertEquals(BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS, @@ -1233,12 +1237,14 @@ public class TestGenericKeyedObjectPool extends TestKeyedObjectPool { assertEquals(maxWaitMillis, objPool.getMaxWaitMillis()); assertEquals(minIdle, objPool.getMinIdlePerKey()); assertEquals(maxTotal, objPool.getMaxTotal()); + assertEquals(minEvictableIdleTimeMillis, objPool.getMinEvictableIdleDuration().toMillis()); assertEquals(minEvictableIdleTimeMillis, objPool.getMinEvictableIdleTimeMillis()); assertEquals(minEvictableIdleTimeMillis, objPool.getMinEvictableIdleTime().toMillis()); assertEquals(numTestsPerEvictionRun, objPool.getNumTestsPerEvictionRun()); assertEquals(Boolean.valueOf(testOnBorrow), Boolean.valueOf(objPool.getTestOnBorrow())); assertEquals(Boolean.valueOf(testOnReturn), Boolean.valueOf(objPool.getTestOnReturn())); assertEquals(Boolean.valueOf(testWhileIdle), Boolean.valueOf(objPool.getTestWhileIdle())); + assertEquals(timeBetweenEvictionRunsMillis, objPool.getDurationBetweenEvictionRuns().toMillis()); assertEquals(timeBetweenEvictionRunsMillis, objPool.getTimeBetweenEvictionRunsMillis()); assertEquals(timeBetweenEvictionRunsMillis, objPool.getTimeBetweenEvictionRuns().toMillis()); assertEquals(Boolean.valueOf(blockWhenExhausted), Boolean.valueOf(objPool.getBlockWhenExhausted())); @@ -2407,6 +2413,10 @@ public class TestGenericKeyedObjectPool extends TestKeyedObjectPool { assertEquals(12345L, gkoPool.getMinEvictableIdleTime().toMillis()); } { + gkoPool.setMinEvictableIdleTime(Duration.ofMillis(12345L)); + assertEquals(12345L, gkoPool.getMinEvictableIdleDuration().toMillis()); + } + { gkoPool.setNumTestsPerEvictionRun(11); assertEquals(11, gkoPool.getNumTestsPerEvictionRun()); } @@ -2430,11 +2440,15 @@ public class TestGenericKeyedObjectPool extends TestKeyedObjectPool { } { gkoPool.setTimeBetweenEvictionRunsMillis(11235L); + assertEquals(11235L, gkoPool.getDurationBetweenEvictionRuns().toMillis()); + assertEquals(11235L, gkoPool.getTimeBetweenEvictionRuns().toMillis()); assertEquals(11235L, gkoPool.getTimeBetweenEvictionRunsMillis()); } { gkoPool.setTimeBetweenEvictionRuns(Duration.ofMillis(11235L)); + assertEquals(11235L, gkoPool.getDurationBetweenEvictionRuns().toMillis()); assertEquals(11235L, gkoPool.getTimeBetweenEvictionRuns().toMillis()); + assertEquals(11235L, gkoPool.getTimeBetweenEvictionRunsMillis()); } { gkoPool.setBlockWhenExhausted(true); diff --git a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java index fe646a3..5c66d47 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java @@ -628,20 +628,27 @@ public class TestGenericObjectPool extends TestBaseObjectPool { "minEvictableIdleTimeMillis"); assertEquals(expected.getMinEvictableIdleTime(), actual.getMinEvictableIdleTime(), "minEvictableIdleTime"); + assertEquals(expected.getMinEvictableIdleDuration(), actual.getMinEvictableIdleDuration(), + "minEvictableIdleDuration"); assertEquals(expected.getNumTestsPerEvictionRun(), actual.getNumTestsPerEvictionRun(), "numTestsPerEvictionRun"); + assertEquals(expected.getEvictorShutdownTimeoutDuration(), actual.getEvictorShutdownTimeoutDuration(), + "evictorShutdownTimeoutDuration"); assertEquals(expected.getEvictorShutdownTimeoutMillis(), actual.getEvictorShutdownTimeoutMillis(), "evictorShutdownTimeoutMillis"); assertEquals(expected.getEvictorShutdownTimeout(), actual.getEvictorShutdownTimeout(), "evictorShutdownTimeout"); assertEquals(expected.getTimeBetweenEvictionRunsMillis(), actual.getTimeBetweenEvictionRunsMillis(), "timeBetweenEvictionRunsMillis"); + assertEquals(expected.getDurationBetweenEvictionRuns(), actual.getTimeBetweenEvictionRuns(), + "timeBetweenEvictionRuns"); assertEquals(expected.getTimeBetweenEvictionRuns(), actual.getTimeBetweenEvictionRuns(), "timeBetweenEvictionRuns"); } private void checkEvict(final boolean lifo) throws Exception { // yea this is hairy but it tests all the code paths in GOP.evict() + genericObjectPool.setSoftMinEvictableIdle(Duration.ofMillis(10)); genericObjectPool.setSoftMinEvictableIdleTime(Duration.ofMillis(10)); genericObjectPool.setMinIdle(2); genericObjectPool.setTestWhileIdle(true); @@ -1149,8 +1156,8 @@ public class TestGenericObjectPool extends TestBaseObjectPool { @Timeout(value = 60000, unit = TimeUnit.MILLISECONDS) public void testCloseMultiplePools1() { try (final GenericObjectPool<String> genericObjectPool2 = new GenericObjectPool<>(simpleFactory)) { - genericObjectPool.setTimeBetweenEvictionRuns(TestConstants.ONE_MILLISECOND); - genericObjectPool2.setTimeBetweenEvictionRuns(TestConstants.ONE_MILLISECOND); + genericObjectPool.setTimeBetweenEvictionRuns(TestConstants.ONE_MILLISECOND_DURATION); + genericObjectPool2.setTimeBetweenEvictionRuns(TestConstants.ONE_MILLISECOND_DURATION); } genericObjectPool.close(); } @@ -1162,10 +1169,10 @@ public class TestGenericObjectPool extends TestBaseObjectPool { // Ensure eviction takes a long time, during which time EvictionTimer.executor's queue is empty simpleFactory.setDestroyLatency(1000L); // Ensure there is an object to evict, so that above latency takes effect - genericObjectPool.setTimeBetweenEvictionRuns(TestConstants.ONE_MILLISECOND); - genericObjectPool2.setTimeBetweenEvictionRuns(TestConstants.ONE_MILLISECOND); - genericObjectPool.setMinEvictableIdleTime(TestConstants.ONE_MILLISECOND); - genericObjectPool2.setMinEvictableIdleTime(TestConstants.ONE_MILLISECOND); + genericObjectPool.setTimeBetweenEvictionRuns(TestConstants.ONE_MILLISECOND_DURATION); + genericObjectPool2.setTimeBetweenEvictionRuns(TestConstants.ONE_MILLISECOND_DURATION); + genericObjectPool.setMinEvictableIdleTime(TestConstants.ONE_MILLISECOND_DURATION); + genericObjectPool2.setMinEvictableIdleTime(TestConstants.ONE_MILLISECOND_DURATION); genericObjectPool.addObject(); genericObjectPool2.addObject(); // Close both pools @@ -1291,6 +1298,8 @@ public class TestGenericObjectPool extends TestBaseObjectPool { dummyPool.getMinEvictableIdleTimeMillis()); assertEquals(BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME, dummyPool.getMinEvictableIdleTime()); + assertEquals(BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME, + dummyPool.getMinEvictableIdleDuration()); assertEquals(BaseObjectPoolConfig.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, dummyPool.getNumTestsPerEvictionRun()); assertEquals(Boolean.valueOf(BaseObjectPoolConfig.DEFAULT_TEST_ON_BORROW), @@ -1299,6 +1308,8 @@ public class TestGenericObjectPool extends TestBaseObjectPool { Boolean.valueOf(dummyPool.getTestOnReturn())); assertEquals(Boolean.valueOf(BaseObjectPoolConfig.DEFAULT_TEST_WHILE_IDLE), Boolean.valueOf(dummyPool.getTestWhileIdle())); + assertEquals(BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS, + dummyPool.getDurationBetweenEvictionRuns()); assertEquals(BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS, dummyPool.getTimeBetweenEvictionRunsMillis()); assertEquals(BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS, @@ -1649,8 +1660,9 @@ public class TestGenericObjectPool extends TestBaseObjectPool { timePool.setMaxIdle(5); timePool.setMaxTotal(5); timePool.setNumTestsPerEvictionRun(5); + timePool.setMinEvictableIdle(Duration.ofSeconds(3)); timePool.setMinEvictableIdleTime(Duration.ofSeconds(3)); - timePool.setSoftMinEvictableIdleTime(TestConstants.ONE_SECOND); + timePool.setSoftMinEvictableIdleTime(TestConstants.ONE_SECOND_DURATION); timePool.setMinIdle(2); final TimeTest[] active = new TimeTest[5]; @@ -2708,6 +2720,7 @@ public class TestGenericObjectPool extends TestBaseObjectPool { } { genericObjectPool.setMinEvictableIdleTimeMillis(12345L); + assertEquals(12345L,genericObjectPool.getMinEvictableIdleDuration().toMillis()); assertEquals(12345L,genericObjectPool.getMinEvictableIdleTimeMillis()); assertEquals(12345L,genericObjectPool.getMinEvictableIdleTime().toMillis()); } @@ -2735,11 +2748,13 @@ public class TestGenericObjectPool extends TestBaseObjectPool { } { genericObjectPool.setTimeBetweenEvictionRunsMillis(11235L); + assertEquals(11235L,genericObjectPool.getDurationBetweenEvictionRuns().toMillis()); assertEquals(11235L,genericObjectPool.getTimeBetweenEvictionRunsMillis()); assertEquals(11235L,genericObjectPool.getTimeBetweenEvictionRuns().toMillis()); } { genericObjectPool.setSoftMinEvictableIdleTimeMillis(12135L); + assertEquals(12135L,genericObjectPool.getSoftMinEvictableIdleDuration().toMillis()); assertEquals(12135L,genericObjectPool.getSoftMinEvictableIdleTimeMillis()); assertEquals(12135L,genericObjectPool.getSoftMinEvictableIdleTime().toMillis()); }