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
commit 91a81e5dc5d7bcfafd5eef52fc7caed8453b3d81 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Mon Feb 15 13:44:08 2021 -0500 Switch internals to java.time.Duration. Add Duration constants based on existing milliseconds constants. --- .../commons/pool2/impl/BaseGenericObjectPool.java | 81 ++++----- .../commons/pool2/impl/BaseObjectPoolConfig.java | 184 +++++++++++++-------- .../apache/commons/pool2/impl/EvictionTimer.java | 15 +- .../apache/commons/pool2/impl/TestConstants.java | 1 + .../commons/pool2/impl/TestEvictionTimer.java | 10 +- .../commons/pool2/impl/TestGenericObjectPool.java | 3 +- 6 files changed, 163 insertions(+), 131 deletions(-) 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 a907014..b8e8cc3 100644 --- a/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java @@ -22,6 +22,7 @@ import java.io.Writer; import java.lang.management.ManagementFactory; import java.lang.ref.WeakReference; import java.lang.reflect.InvocationTargetException; +import java.time.Duration; import java.util.Arrays; import java.util.Deque; import java.util.Iterator; @@ -66,34 +67,21 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { private static final String EVICTION_POLICY_TYPE_NAME = EvictionPolicy.class.getName(); // Configuration attributes - private volatile int maxTotal = - GenericKeyedObjectPoolConfig.DEFAULT_MAX_TOTAL; - private volatile boolean blockWhenExhausted = - BaseObjectPoolConfig.DEFAULT_BLOCK_WHEN_EXHAUSTED; - private volatile long maxWaitMillis = - BaseObjectPoolConfig.DEFAULT_MAX_WAIT_MILLIS; + private volatile int maxTotal = GenericKeyedObjectPoolConfig.DEFAULT_MAX_TOTAL; + private volatile boolean blockWhenExhausted = BaseObjectPoolConfig.DEFAULT_BLOCK_WHEN_EXHAUSTED; + private volatile Duration maxWait = BaseObjectPoolConfig.DEFAULT_MAX_WAIT; private volatile boolean lifo = BaseObjectPoolConfig.DEFAULT_LIFO; private final boolean fairness; - private volatile boolean testOnCreate = - BaseObjectPoolConfig.DEFAULT_TEST_ON_CREATE; - 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 long timeBetweenEvictionRunsMillis = - BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS; - private volatile int numTestsPerEvictionRun = - BaseObjectPoolConfig.DEFAULT_NUM_TESTS_PER_EVICTION_RUN; - private volatile long minEvictableIdleTimeMillis = - BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS; - private volatile long softMinEvictableIdleTimeMillis = - BaseObjectPoolConfig.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS; + private volatile boolean testOnCreate = BaseObjectPoolConfig.DEFAULT_TEST_ON_CREATE; + 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 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 EvictionPolicy<T> evictionPolicy; - private volatile long evictorShutdownTimeoutMillis = - BaseObjectPoolConfig.DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT_MILLIS; - + private volatile Duration evictorShutdownTimeout = BaseObjectPoolConfig.DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT; // Internal (primarily state) attributes final Object closeLock = new Object(); @@ -259,7 +247,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * @see #setBlockWhenExhausted */ public final long getMaxWaitMillis() { - return maxWaitMillis; + return maxWait.toMillis(); } /** @@ -277,7 +265,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * @see #setBlockWhenExhausted */ public final void setMaxWaitMillis(final long maxWaitMillis) { - this.maxWaitMillis = maxWaitMillis; + this.maxWait = Duration.ofMillis(maxWaitMillis); } /** @@ -475,7 +463,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * @see #setTimeBetweenEvictionRunsMillis */ public final long getTimeBetweenEvictionRunsMillis() { - return timeBetweenEvictionRunsMillis; + return timeBetweenEvictionRuns.toMillis(); } /** @@ -492,8 +480,8 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { */ public final void setTimeBetweenEvictionRunsMillis( final long timeBetweenEvictionRunsMillis) { - this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; - startEvictor(timeBetweenEvictionRunsMillis); + this.timeBetweenEvictionRuns = Duration.ofMillis(timeBetweenEvictionRunsMillis); + startEvictor(timeBetweenEvictionRuns); } /** @@ -548,7 +536,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * @see #setTimeBetweenEvictionRunsMillis */ public final long getMinEvictableIdleTimeMillis() { - return minEvictableIdleTimeMillis; + return minEvictableIdleTime.toMillis(); } /** @@ -566,7 +554,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { */ public final void setMinEvictableIdleTimeMillis( final long minEvictableIdleTimeMillis) { - this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; + this.minEvictableIdleTime = Duration.ofMillis(minEvictableIdleTimeMillis); } /** @@ -585,7 +573,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * @see #setSoftMinEvictableIdleTimeMillis */ public final long getSoftMinEvictableIdleTimeMillis() { - return softMinEvictableIdleTimeMillis; + return softMinEvictableIdleTime.toMillis(); } /** @@ -607,7 +595,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { */ public final void setSoftMinEvictableIdleTimeMillis( final long softMinEvictableIdleTimeMillis) { - this.softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis; + this.softMinEvictableIdleTime = Duration.ofMillis(softMinEvictableIdleTimeMillis); } /** @@ -707,7 +695,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * the Evictor to shut down. */ public final long getEvictorShutdownTimeoutMillis() { - return evictorShutdownTimeoutMillis; + return evictorShutdownTimeout.toMillis(); } /** @@ -721,7 +709,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { */ public final void setEvictorShutdownTimeoutMillis( final long evictorShutdownTimeoutMillis) { - this.evictorShutdownTimeoutMillis = evictorShutdownTimeoutMillis; + this.evictorShutdownTimeout = Duration.ofMillis(evictorShutdownTimeoutMillis); } /** @@ -781,24 +769,25 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * * @param delay time in milliseconds before start and between eviction runs */ - final void startEvictor(final long delay) { + final void startEvictor(final Duration delay) { synchronized (evictionLock) { + final boolean isPositiverDelay = !delay.isNegative() && !delay.isZero(); if (evictor == null) { // Starting evictor for the first time or after a cancel - if (delay > 0) { // Starting new evictor + if (isPositiverDelay) { // Starting new evictor evictor = new Evictor(); EvictionTimer.schedule(evictor, delay, delay); } } else { // Stop or restart of existing evictor - if (delay > 0) { // Restart + if (isPositiverDelay) { // Restart synchronized (EvictionTimer.class) { // Ensure no cancel can happen between cancel / schedule calls - EvictionTimer.cancel(evictor, evictorShutdownTimeoutMillis, TimeUnit.MILLISECONDS, true); + EvictionTimer.cancel(evictor, evictorShutdownTimeout, true); evictor = null; evictionIterator = null; evictor = new Evictor(); EvictionTimer.schedule(evictor, delay, delay); } } else { // Stopping evictor - EvictionTimer.cancel(evictor, evictorShutdownTimeoutMillis, TimeUnit.MILLISECONDS, false); + EvictionTimer.cancel(evictor, evictorShutdownTimeout, false); } } } @@ -808,7 +797,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * Stops the evictor. */ void stopEvictor() { - startEvictor(-1L); + startEvictor(Duration.ofMillis(-1L)); } /** * Tries to ensure that the configured minimum number of idle instances are @@ -1375,7 +1364,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { builder.append(", blockWhenExhausted="); builder.append(blockWhenExhausted); builder.append(", maxWaitMillis="); - builder.append(maxWaitMillis); + builder.append(maxWait); builder.append(", lifo="); builder.append(lifo); builder.append(", fairness="); @@ -1389,13 +1378,13 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { builder.append(", testWhileIdle="); builder.append(testWhileIdle); builder.append(", timeBetweenEvictionRunsMillis="); - builder.append(timeBetweenEvictionRunsMillis); + builder.append(timeBetweenEvictionRuns); builder.append(", numTestsPerEvictionRun="); builder.append(numTestsPerEvictionRun); builder.append(", minEvictableIdleTimeMillis="); - builder.append(minEvictableIdleTimeMillis); + builder.append(minEvictableIdleTime); builder.append(", softMinEvictableIdleTimeMillis="); - builder.append(softMinEvictableIdleTimeMillis); + builder.append(softMinEvictableIdleTime); builder.append(", evictionPolicy="); builder.append(evictionPolicy); builder.append(", closeLock="); 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 548fe57..525aede 100644 --- a/src/main/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java +++ b/src/main/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java @@ -16,6 +16,8 @@ */ package org.apache.commons.pool2.impl; +import java.time.Duration; + import org.apache.commons.pool2.BaseObject; /** @@ -53,7 +55,15 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon public static final long DEFAULT_MAX_WAIT_MILLIS = -1L; /** - * The default value for the {@code minEvictableIdleTimeMillis} + * The default value for the {@code maxWait} configuration attribute. + * @see GenericObjectPool#getMaxWaitMillis() + * @see GenericKeyedObjectPool#getMaxWaitMillis() + * @since 2.10.0 + */ + public static final Duration DEFAULT_MAX_WAIT = Duration.ofMillis(DEFAULT_MAX_WAIT_MILLIS); + + /** + * The default value for the {@code minEvictableIdleTime} * configuration attribute. * @see GenericObjectPool#getMinEvictableIdleTimeMillis() * @see GenericKeyedObjectPool#getMinEvictableIdleTimeMillis() @@ -62,7 +72,17 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon 1000L * 60L * 30L; /** - * The default value for the {@code softMinEvictableIdleTimeMillis} + * The default value for the {@code minEvictableIdleTime} + * configuration attribute. + * @see GenericObjectPool#getMinEvictableIdleTimeMillis() + * @see GenericKeyedObjectPool#getMinEvictableIdleTimeMillis() + * @since 2.10.0 + */ + 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() @@ -70,7 +90,17 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon public static final long DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS = -1; /** - * The default value for {@code evictorShutdownTimeoutMillis} configuration + * The default value for the {@code softMinEvictableIdleTime} + * configuration attribute. + * @see GenericObjectPool#getSoftMinEvictableIdleTimeMillis() + * @see GenericKeyedObjectPool#getSoftMinEvictableIdleTimeMillis() + * @since 2.10.0 + */ + public static final Duration DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME = + Duration.ofMillis(DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS); + + /** + * The default value for {@code evictorShutdownTimeout} configuration * attribute. * @see GenericObjectPool#getEvictorShutdownTimeoutMillis() * @see GenericKeyedObjectPool#getEvictorShutdownTimeoutMillis() @@ -79,6 +109,16 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon 10L * 1000L; /** + * The default value for {@code evictorShutdownTimeout} configuration + * attribute. + * @see GenericObjectPool#getEvictorShutdownTimeoutMillis() + * @see GenericKeyedObjectPool#getEvictorShutdownTimeoutMillis() + * @since 2.10.0 + */ + public static final Duration DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT = + Duration.ofMillis(DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT_MILLIS); + + /** * The default value for the {@code numTestsPerEvictionRun} configuration * attribute. * @see GenericObjectPool#getNumTestsPerEvictionRun() @@ -117,7 +157,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon public static final boolean DEFAULT_TEST_WHILE_IDLE = false; /** - * The default value for the {@code timeBetweenEvictionRunsMillis} + * The default value for the {@code timeBetweenEvictionRuns} * configuration attribute. * @see GenericObjectPool#getTimeBetweenEvictionRunsMillis() * @see GenericKeyedObjectPool#getTimeBetweenEvictionRunsMillis() @@ -125,6 +165,15 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon public static final long DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS = -1L; /** + * The default value for the {@code timeBetweenEvictionRuns} + * configuration attribute. + * @see GenericObjectPool#getTimeBetweenEvictionRunsMillis() + * @see GenericKeyedObjectPool#getTimeBetweenEvictionRunsMillis() + */ + public static final Duration DEFAULT_TIME_BETWEEN_EVICTION_RUNS = + Duration.ofMillis(DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS); + + /** * The default value for the {@code blockWhenExhausted} configuration * attribute. * @see GenericObjectPool#getBlockWhenExhausted() @@ -167,21 +216,17 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon private boolean fairness = DEFAULT_FAIRNESS; - private long maxWaitMillis = DEFAULT_MAX_WAIT_MILLIS; + private Duration maxWaitMillis = DEFAULT_MAX_WAIT; - private long minEvictableIdleTimeMillis = - DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS; + private Duration minEvictableIdleTime = DEFAULT_MIN_EVICTABLE_IDLE_TIME; - private long evictorShutdownTimeoutMillis = - DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT_MILLIS; + private Duration evictorShutdownTimeout = DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT; - private long softMinEvictableIdleTimeMillis = - DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS; + private Duration softMinEvictableIdleTime = DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME; - private int numTestsPerEvictionRun = - DEFAULT_NUM_TESTS_PER_EVICTION_RUN; + private int numTestsPerEvictionRun = DEFAULT_NUM_TESTS_PER_EVICTION_RUN; - private EvictionPolicy<T> evictionPolicy = null; // Only 2.6.0 applications set this + private EvictionPolicy<T> evictionPolicy; // Only 2.6.0 applications set this private String evictionPolicyClassName = DEFAULT_EVICTION_POLICY_CLASS_NAME; @@ -193,8 +238,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon private boolean testWhileIdle = DEFAULT_TEST_WHILE_IDLE; - private long timeBetweenEvictionRunsMillis = - DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS; + private Duration timeBetweenEvictionRuns = DEFAULT_TIME_BETWEEN_EVICTION_RUNS; private boolean blockWhenExhausted = DEFAULT_BLOCK_WHEN_EXHAUSTED; @@ -207,7 +251,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon /** - * Get the value for the {@code lifo} configuration attribute for pools + * Gets the value for the {@code lifo} configuration attribute for pools * created with this configuration instance. * * @return The current setting of {@code lifo} for this configuration @@ -221,7 +265,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Get the value for the {@code fairness} configuration attribute for pools + * Gets the value for the {@code fairness} configuration attribute for pools * created with this configuration instance. * * @return The current setting of {@code fairness} for this configuration @@ -235,7 +279,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Set the value for the {@code lifo} configuration attribute for pools + * Sets the value for the {@code lifo} configuration attribute for pools * created with this configuration instance. * * @param lifo The new setting of {@code lifo} @@ -249,7 +293,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Set the value for the {@code fairness} configuration attribute for pools + * Sets the value for the {@code fairness} configuration attribute for pools * created with this configuration instance. * * @param fairness The new setting of {@code fairness} @@ -263,7 +307,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Get the value for the {@code maxWait} configuration attribute for pools + * Gets the value for the {@code maxWait} configuration attribute for pools * created with this configuration instance. * * @return The current setting of {@code maxWait} for this @@ -273,11 +317,11 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * @see GenericKeyedObjectPool#getMaxWaitMillis() */ public long getMaxWaitMillis() { - return maxWaitMillis; + return maxWaitMillis.toMillis(); } /** - * Set the value for the {@code maxWait} configuration attribute for pools + * Sets the value for the {@code maxWait} configuration attribute for pools * created with this configuration instance. * * @param maxWaitMillis The new setting of {@code maxWaitMillis} @@ -287,59 +331,59 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * @see GenericKeyedObjectPool#getMaxWaitMillis() */ public void setMaxWaitMillis(final long maxWaitMillis) { - this.maxWaitMillis = maxWaitMillis; + this.maxWaitMillis = Duration.ofMillis(maxWaitMillis); } /** - * Get the value for the {@code minEvictableIdleTimeMillis} configuration + * Gets the value for the {@code minEvictableIdleTime} configuration * attribute for pools created with this configuration instance. * - * @return The current setting of {@code minEvictableIdleTimeMillis} for + * @return The current setting of {@code minEvictableIdleTime} for * this configuration instance * * @see GenericObjectPool#getMinEvictableIdleTimeMillis() * @see GenericKeyedObjectPool#getMinEvictableIdleTimeMillis() */ public long getMinEvictableIdleTimeMillis() { - return minEvictableIdleTimeMillis; + return minEvictableIdleTime.toMillis(); } /** - * Set the value for the {@code minEvictableIdleTimeMillis} configuration + * Sets the value for the {@code minEvictableIdleTime} configuration * attribute for pools created with this configuration instance. * * @param minEvictableIdleTimeMillis The new setting of - * {@code minEvictableIdleTimeMillis} for this configuration instance + * {@code minEvictableIdleTime} for this configuration instance * * @see GenericObjectPool#getMinEvictableIdleTimeMillis() * @see GenericKeyedObjectPool#getMinEvictableIdleTimeMillis() */ public void setMinEvictableIdleTimeMillis(final long minEvictableIdleTimeMillis) { - this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; + this.minEvictableIdleTime = Duration.ofMillis(minEvictableIdleTimeMillis); } /** - * Get the value for the {@code softMinEvictableIdleTimeMillis} + * Gets the value for the {@code softMinEvictableIdleTime} * configuration attribute for pools created with this configuration * instance. * - * @return The current setting of {@code softMinEvictableIdleTimeMillis} + * @return The current setting of {@code softMinEvictableIdleTime} * for this configuration instance * * @see GenericObjectPool#getSoftMinEvictableIdleTimeMillis() * @see GenericKeyedObjectPool#getSoftMinEvictableIdleTimeMillis() */ public long getSoftMinEvictableIdleTimeMillis() { - return softMinEvictableIdleTimeMillis; + return softMinEvictableIdleTime.toMillis(); } /** - * Set the value for the {@code softMinEvictableIdleTimeMillis} + * Sets the value for the {@code softMinEvictableIdleTime} * configuration attribute for pools created with this configuration * instance. * * @param softMinEvictableIdleTimeMillis The new setting of - * {@code softMinEvictableIdleTimeMillis} for this configuration + * {@code softMinEvictableIdleTime} for this configuration * instance * * @see GenericObjectPool#getSoftMinEvictableIdleTimeMillis() @@ -347,11 +391,11 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon */ public void setSoftMinEvictableIdleTimeMillis( final long softMinEvictableIdleTimeMillis) { - this.softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis; + this.softMinEvictableIdleTime = Duration.ofMillis(softMinEvictableIdleTimeMillis); } /** - * Get the value for the {@code numTestsPerEvictionRun} configuration + * Gets the value for the {@code numTestsPerEvictionRun} configuration * attribute for pools created with this configuration instance. * * @return The current setting of {@code numTestsPerEvictionRun} for this @@ -365,7 +409,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Set the value for the {@code numTestsPerEvictionRun} configuration + * Sets the value for the {@code numTestsPerEvictionRun} configuration * attribute for pools created with this configuration instance. * * @param numTestsPerEvictionRun The new setting of @@ -379,25 +423,25 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Get the value for the {@code evictorShutdownTimeoutMillis} configuration + * Gets the value for the {@code evictorShutdownTimeout} configuration * attribute for pools created with this configuration instance. * - * @return The current setting of {@code evictorShutdownTimeoutMillis} for + * @return The current setting of {@code evictorShutdownTimeout} for * this configuration instance * * @see GenericObjectPool#getEvictorShutdownTimeoutMillis() * @see GenericKeyedObjectPool#getEvictorShutdownTimeoutMillis() */ public long getEvictorShutdownTimeoutMillis() { - return evictorShutdownTimeoutMillis; + return evictorShutdownTimeout.toMillis(); } /** - * Set the value for the {@code evictorShutdownTimeoutMillis} configuration + * Sets the value for the {@code evictorShutdownTimeout} configuration * attribute for pools created with this configuration instance. * * @param evictorShutdownTimeoutMillis The new setting of - * {@code evictorShutdownTimeoutMillis} for this configuration + * {@code evictorShutdownTimeout} for this configuration * instance * * @see GenericObjectPool#getEvictorShutdownTimeoutMillis() @@ -405,11 +449,11 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon */ public void setEvictorShutdownTimeoutMillis( final long evictorShutdownTimeoutMillis) { - this.evictorShutdownTimeoutMillis = evictorShutdownTimeoutMillis; + this.evictorShutdownTimeout = Duration.ofMillis(evictorShutdownTimeoutMillis); } /** - * Get the value for the {@code testOnCreate} configuration attribute for + * Gets the value for the {@code testOnCreate} configuration attribute for * pools created with this configuration instance. * * @return The current setting of {@code testOnCreate} for this @@ -425,7 +469,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Set the value for the {@code testOnCreate} configuration attribute for + * Sets the value for the {@code testOnCreate} configuration attribute for * pools created with this configuration instance. * * @param testOnCreate The new setting of {@code testOnCreate} @@ -441,7 +485,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Get the value for the {@code testOnBorrow} configuration attribute for + * Gets the value for the {@code testOnBorrow} configuration attribute for * pools created with this configuration instance. * * @return The current setting of {@code testOnBorrow} for this @@ -455,7 +499,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Set the value for the {@code testOnBorrow} configuration attribute for + * Sets the value for the {@code testOnBorrow} configuration attribute for * pools created with this configuration instance. * * @param testOnBorrow The new setting of {@code testOnBorrow} @@ -469,7 +513,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Get the value for the {@code testOnReturn} configuration attribute for + * Gets the value for the {@code testOnReturn} configuration attribute for * pools created with this configuration instance. * * @return The current setting of {@code testOnReturn} for this @@ -483,7 +527,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Set the value for the {@code testOnReturn} configuration attribute for + * Sets the value for the {@code testOnReturn} configuration attribute for * pools created with this configuration instance. * * @param testOnReturn The new setting of {@code testOnReturn} @@ -497,7 +541,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Get the value for the {@code testWhileIdle} configuration attribute for + * Gets the value for the {@code testWhileIdle} configuration attribute for * pools created with this configuration instance. * * @return The current setting of {@code testWhileIdle} for this @@ -511,7 +555,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Set the value for the {@code testWhileIdle} configuration attribute for + * Sets the value for the {@code testWhileIdle} configuration attribute for * pools created with this configuration instance. * * @param testWhileIdle The new setting of {@code testWhileIdle} @@ -525,25 +569,25 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Get the value for the {@code timeBetweenEvictionRunsMillis} configuration + * Gets the value for the {@code timeBetweenEvictionRuns} configuration * attribute for pools created with this configuration instance. * - * @return The current setting of {@code timeBetweenEvictionRunsMillis} for + * @return The current setting of {@code timeBetweenEvictionRuns} for * this configuration instance * * @see GenericObjectPool#getTimeBetweenEvictionRunsMillis() * @see GenericKeyedObjectPool#getTimeBetweenEvictionRunsMillis() */ public long getTimeBetweenEvictionRunsMillis() { - return timeBetweenEvictionRunsMillis; + return timeBetweenEvictionRuns.toMillis(); } /** - * Set the value for the {@code timeBetweenEvictionRunsMillis} configuration + * Sets the value for the {@code timeBetweenEvictionRuns} configuration * attribute for pools created with this configuration instance. * * @param timeBetweenEvictionRunsMillis The new setting of - * {@code timeBetweenEvictionRunsMillis} for this configuration + * {@code timeBetweenEvictionRuns} for this configuration * instance * * @see GenericObjectPool#getTimeBetweenEvictionRunsMillis() @@ -551,11 +595,11 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon */ public void setTimeBetweenEvictionRunsMillis( final long timeBetweenEvictionRunsMillis) { - this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; + this.timeBetweenEvictionRuns = Duration.ofMillis(timeBetweenEvictionRunsMillis); } /** - * Get the value for the {@code evictionPolicyClass} configuration + * Gets the value for the {@code evictionPolicyClass} configuration * attribute for pools created with this configuration instance. * * @return The current setting of {@code evictionPolicyClass} for this @@ -570,7 +614,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Get the value for the {@code evictionPolicyClassName} configuration + * Gets the value for the {@code evictionPolicyClassName} configuration * attribute for pools created with this configuration instance. * * @return The current setting of {@code evictionPolicyClassName} for this @@ -584,7 +628,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Set the value for the {@code evictionPolicyClass} configuration + * Sets the value for the {@code evictionPolicyClass} configuration * attribute for pools created with this configuration instance. * * @param evictionPolicy The new setting of @@ -599,7 +643,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Set the value for the {@code evictionPolicyClassName} configuration + * Sets the value for the {@code evictionPolicyClassName} configuration * attribute for pools created with this configuration instance. * * @param evictionPolicyClassName The new setting of @@ -613,7 +657,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Get the value for the {@code blockWhenExhausted} configuration attribute + * Gets the value for the {@code blockWhenExhausted} configuration attribute * for pools created with this configuration instance. * * @return The current setting of {@code blockWhenExhausted} for this @@ -627,7 +671,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon } /** - * Set the value for the {@code blockWhenExhausted} configuration attribute + * Sets the value for the {@code blockWhenExhausted} configuration attribute * for pools created with this configuration instance. * * @param blockWhenExhausted The new setting of {@code blockWhenExhausted} @@ -720,10 +764,10 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon builder.append(fairness); builder.append(", maxWaitMillis="); builder.append(maxWaitMillis); - builder.append(", minEvictableIdleTimeMillis="); - builder.append(minEvictableIdleTimeMillis); - builder.append(", softMinEvictableIdleTimeMillis="); - builder.append(softMinEvictableIdleTimeMillis); + builder.append(", minEvictableIdleTime="); + builder.append(minEvictableIdleTime); + builder.append(", softMinEvictableIdleTime="); + builder.append(softMinEvictableIdleTime); builder.append(", numTestsPerEvictionRun="); builder.append(numTestsPerEvictionRun); builder.append(", evictionPolicyClassName="); @@ -736,8 +780,8 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon builder.append(testOnReturn); builder.append(", testWhileIdle="); builder.append(testWhileIdle); - builder.append(", timeBetweenEvictionRunsMillis="); - builder.append(timeBetweenEvictionRunsMillis); + builder.append(", timeBetweenEvictionRuns="); + builder.append(timeBetweenEvictionRuns); builder.append(", blockWhenExhausted="); builder.append(blockWhenExhausted); builder.append(", jmxEnabled="); diff --git a/src/main/java/org/apache/commons/pool2/impl/EvictionTimer.java b/src/main/java/org/apache/commons/pool2/impl/EvictionTimer.java index 517e8bb..a12da3b 100644 --- a/src/main/java/org/apache/commons/pool2/impl/EvictionTimer.java +++ b/src/main/java/org/apache/commons/pool2/impl/EvictionTimer.java @@ -19,6 +19,7 @@ package org.apache.commons.pool2.impl; import java.lang.ref.WeakReference; import java.security.AccessController; import java.security.PrivilegedAction; +import java.time.Duration; import java.util.HashMap; import java.util.Map.Entry; import java.util.concurrent.ScheduledFuture; @@ -81,16 +82,16 @@ class EvictionTimer { * @param period Time in milliseconds between executions. */ static synchronized void schedule( - final BaseGenericObjectPool<?>.Evictor task, final long delay, final long period) { + final BaseGenericObjectPool<?>.Evictor task, final Duration delay, final Duration period) { if (null == executor) { executor = new ScheduledThreadPoolExecutor(1, new EvictorThreadFactory()); executor.setRemoveOnCancelPolicy(true); - executor.scheduleAtFixedRate(new Reaper(), delay, period, TimeUnit.MILLISECONDS); + executor.scheduleAtFixedRate(new Reaper(), delay.toMillis(), period.toMillis(), TimeUnit.MILLISECONDS); } final WeakReference<Runnable> ref = new WeakReference<>(task); final WeakRunner runner = new WeakRunner(ref); - final ScheduledFuture<?> scheduledFuture = - executor.scheduleWithFixedDelay(runner, delay, period, TimeUnit.MILLISECONDS); + final ScheduledFuture<?> scheduledFuture = executor.scheduleWithFixedDelay(runner, delay.toMillis(), + period.toMillis(), TimeUnit.MILLISECONDS); task.setScheduledFuture(scheduledFuture); taskMap.put(ref, runner); } @@ -102,11 +103,9 @@ class EvictionTimer { * @param timeout If the associated executor is no longer required, how * long should this thread wait for the executor to * terminate? - * @param unit The units for the specified timeout. * @param restarting The state of the evictor. */ - static synchronized void cancel( - final BaseGenericObjectPool<?>.Evictor evictor, final long timeout, final TimeUnit unit, + static synchronized void cancel(final BaseGenericObjectPool<?>.Evictor evictor, final Duration timeout, final boolean restarting) { if (evictor != null) { evictor.cancel(); @@ -115,7 +114,7 @@ class EvictionTimer { if (!restarting && executor != null && taskMap.isEmpty()) { executor.shutdown(); try { - executor.awaitTermination(timeout, unit); + executor.awaitTermination(timeout.toMillis(), TimeUnit.MILLISECONDS); } catch (final InterruptedException e) { // Swallow // Significant API changes would be required to propagate this 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 14d182f..1eeb967 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestConstants.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestConstants.java @@ -22,5 +22,6 @@ import java.time.Duration; public class TestConstants { public static final Duration ONE_SECOND = Duration.ofSeconds(1); + public static final Duration ONE_MINUTE = Duration.ofMinutes(1); } 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 ffb00a9..09657ce 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestEvictionTimer.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestEvictionTimer.java @@ -54,7 +54,7 @@ public class TestEvictionTimer { // Start evictor #1 final BaseGenericObjectPool<String>.Evictor evictor1 = pool.new Evictor(); - EvictionTimer.schedule(evictor1, 60000, 60000); + EvictionTimer.schedule(evictor1, TestConstants.ONE_MINUTE, TestConstants.ONE_MINUTE); // Assert that eviction objects are correctly allocated // 1 - the evictor timer task is created @@ -72,7 +72,7 @@ public class TestEvictionTimer { // Start evictor #2 final BaseGenericObjectPool<String>.Evictor evictor2 = pool.new Evictor(); - EvictionTimer.schedule(evictor2, 60000, 60000); + EvictionTimer.schedule(evictor2, TestConstants.ONE_MINUTE, TestConstants.ONE_MINUTE); // Assert that eviction objects are correctly allocated // 1 - the evictor timer task is created @@ -83,8 +83,7 @@ public class TestEvictionTimer { assertEquals(2, EvictionTimer.getNumTasks()); // Stop evictor #1 - EvictionTimer.cancel(evictor1, BaseObjectPoolConfig.DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT_MILLIS, - TimeUnit.MILLISECONDS, false); + EvictionTimer.cancel(evictor1, BaseObjectPoolConfig.DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT, false); // Assert that eviction objects are correctly cleaned // 1 - the evictor timer task is cancelled @@ -96,8 +95,7 @@ public class TestEvictionTimer { assertEquals(1, EvictionTimer.getNumTasks()); // Stop evictor #2 - EvictionTimer.cancel(evictor2, BaseObjectPoolConfig.DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT_MILLIS, - TimeUnit.MILLISECONDS, false); + EvictionTimer.cancel(evictor2, BaseObjectPoolConfig.DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT, false); // Assert that eviction objects are correctly cleaned // 1 - the evictor timer task is cancelled 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 3250d5e..61f14ba 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java @@ -22,6 +22,7 @@ import java.lang.management.ManagementFactory; import java.lang.ref.WeakReference; import java.lang.reflect.Field; import java.nio.charset.UnsupportedCharsetException; +import java.time.Duration; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -2692,7 +2693,7 @@ public class TestGenericObjectPool extends TestBaseObjectPool { assertEquals(0,genericObjectPool.getNumIdle(),"Should have 0 idle"); // stop the evictor - genericObjectPool.startEvictor(0L); + genericObjectPool.startEvictor(Duration.ZERO); } }