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 e405182 Add BaseGenericObjectPool.{get|set}MaxWaitDuration(Duration) and deprecate {get|set}MaxWaitMillis(long). e405182 is described below commit e405182e6eaabc7422dfd6f65511b7883443b1e9 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Sun Jun 6 19:41:18 2021 -0400 Add BaseGenericObjectPool.{get|set}MaxWaitDuration(Duration) and deprecate {get|set}MaxWaitMillis(long). Add BaseObjectPoolConfig.{get|set}MaxWaitDuration(Duration) and deprecate {get|set}MaxWaitMillis(long). --- src/changes/changes.xml | 8 ++- .../commons/pool2/impl/BaseGenericObjectPool.java | 64 ++++++++++++++++++---- .../commons/pool2/impl/BaseObjectPoolConfig.java | 50 +++++++++++++---- .../commons/pool2/impl/GenericKeyedObjectPool.java | 4 +- .../commons/pool2/impl/TestGenericObjectPool.java | 3 +- 5 files changed, 103 insertions(+), 26 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 0caa9b1..073cd4c 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -57,7 +57,13 @@ The <action> type attribute can be add,update,fix,remove. - TrackedUse#getLastUsedInstant() </action> <action dev="ggregory" type="add" due-to="Gary Gregory"> - Add BaseObjectPoolConfig.setEvictorShutdownTimeout(Duration), setEvictorShutdownTimeoutMillis(Duration). + Add BaseObjectPoolConfig.{get|set}EvictorShutdownTimeout(Duration), {get|set}EvictorShutdownTimeoutMillis(Duration). + </action> + <action dev="ggregory" type="add" due-to="Gary Gregory"> + Add BaseGenericObjectPool.{get|set}MaxWaitDuration(Duration) and deprecate {get|set}MaxWaitMillis(long). + </action> + <action dev="ggregory" type="add" due-to="Gary Gregory"> + Add BaseObjectPoolConfig.{get|set}MaxWaitDuration(Duration) and deprecate {get|set}MaxWaitMillis(long). </action> <!-- FIXES --> <action dev="ggregory" type="fix" due-to="Gary Gregory"> 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 3eb5f6f..025c041 100644 --- a/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java @@ -316,7 +316,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { // Configuration attributes 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 Duration maxWaitDuration = BaseObjectPoolConfig.DEFAULT_MAX_WAIT; private volatile boolean lifo = BaseObjectPoolConfig.DEFAULT_LIFO; private final boolean fairness; private volatile boolean testOnCreate = BaseObjectPoolConfig.DEFAULT_TEST_ON_CREATE; @@ -592,7 +592,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { } /** - * The maximum time a thread has waited to borrow objects from the pool. + * Gets the maximum time a thread has waited to borrow objects from the pool. * @return maximum wait time in milliseconds since the pool was created */ public final long getMaxBorrowWaitTimeMillis() { @@ -615,6 +615,24 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { } /** + * Gets the maximum duration the + * {@code borrowObject()} method should block before throwing an + * exception when the pool is exhausted and + * {@link #getBlockWhenExhausted} is true. When less than 0, the + * {@code borrowObject()} method may block indefinitely. + * + * @return the maximum number of milliseconds {@code borrowObject()} + * will block. + * + * @see #setMaxWaitDuration + * @see #setBlockWhenExhausted + * @since 2.11.0 + */ + public final Duration getMaxWaitDuration() { + return maxWaitDuration; + } + + /** * Gets the maximum amount of time (in milliseconds) the * {@code borrowObject()} method should block before throwing an * exception when the pool is exhausted and @@ -624,11 +642,12 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * @return the maximum number of milliseconds {@code borrowObject()} * will block. * - * @see #setMaxWaitMillis + * @see #setMaxWaitDuration * @see #setBlockWhenExhausted + * @deprecated Use {@link #getMaxWaitDuration()}. */ public final long getMaxWaitMillis() { - return maxWait.toMillis(); + return maxWaitDuration.toMillis(); } /** @@ -1009,7 +1028,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { */ protected void setConfig(final BaseObjectPoolConfig<T> config) { setLifo(config.getLifo()); - setMaxWaitMillis(config.getMaxWaitMillis()); + setMaxWaitDuration(config.getMaxWaitDuration()); setBlockWhenExhausted(config.getBlockWhenExhausted()); setTestOnCreate(config.getTestOnCreate()); setTestOnBorrow(config.getTestOnBorrow()); @@ -1166,6 +1185,25 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { } /** + * Sets the maximum duration the + * {@code borrowObject()} method should block before throwing an + * exception when the pool is exhausted and + * {@link #getBlockWhenExhausted} is true. When less than 0, the + * {@code borrowObject()} method may block indefinitely. + * + * @param maxWaitDuration the maximum duration + * {@code borrowObject()} will block or negative + * for indefinitely. + * + * @see #getMaxWaitDuration + * @see #setBlockWhenExhausted + * @since 2.11.0 + */ + public final void setMaxWaitDuration(final Duration maxWaitDuration) { + this.maxWaitDuration = PoolImplUtils.nonNull(maxWaitDuration, BaseObjectPoolConfig.DEFAULT_MAX_WAIT); + } + + /** * Sets the maximum amount of time (in milliseconds) the * {@code borrowObject()} method should block before throwing an * exception when the pool is exhausted and @@ -1176,11 +1214,13 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * {@code borrowObject()} will block or negative * for indefinitely. * - * @see #getMaxWaitMillis + * @see #getMaxWaitDuration * @see #setBlockWhenExhausted + * @deprecated Use {@link #setMaxWaitDuration}. */ + @Deprecated public final void setMaxWaitMillis(final long maxWaitMillis) { - this.maxWait = Duration.ofMillis(maxWaitMillis); + setMaxWaitDuration(Duration.ofMillis(maxWaitMillis)); } /** @@ -1481,7 +1521,7 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { builder.append(", blockWhenExhausted="); builder.append(blockWhenExhausted); builder.append(", maxWaitMillis="); - builder.append(maxWait); + builder.append(maxWaitDuration); builder.append(", lifo="); builder.append(lifo); builder.append(", fairness="); @@ -1548,13 +1588,13 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject { * Updates statistics after an object is borrowed from the pool. * * @param p object borrowed from the pool - * @param waitTime time (in milliseconds) that the borrowing thread had to wait + * @param waitDuration time (in milliseconds) that the borrowing thread had to wait */ - final void updateStatsBorrow(final PooledObject<T> p, final Duration waitTime) { + final void updateStatsBorrow(final PooledObject<T> p, final Duration waitDuration) { borrowedCount.incrementAndGet(); idleTimes.add(p.getIdleTime()); - waitTimes.add(waitTime); - final long waitTimeMillis = waitTime.toMillis(); + waitTimes.add(waitDuration); + final long waitTimeMillis = waitDuration.toMillis(); // lock-free optimistic-locking maximum long currentMaxMillis; 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 ab7f0e3..7d3c6b6 100644 --- a/src/main/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java +++ b/src/main/java/org/apache/commons/pool2/impl/BaseObjectPoolConfig.java @@ -49,15 +49,15 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon /** * The default value for the {@code maxWait} configuration attribute. - * @see GenericObjectPool#getMaxWaitMillis() - * @see GenericKeyedObjectPool#getMaxWaitMillis() + * @see GenericObjectPool#getMaxWaitDuration() + * @see GenericKeyedObjectPool#getMaxWaitDuration() */ public static final long DEFAULT_MAX_WAIT_MILLIS = -1L; /** * The default value for the {@code maxWait} configuration attribute. - * @see GenericObjectPool#getMaxWaitMillis() - * @see GenericKeyedObjectPool#getMaxWaitMillis() + * @see GenericObjectPool#getMaxWaitDuration() + * @see GenericKeyedObjectPool#getMaxWaitDuration() * @since 2.10.0 */ public static final Duration DEFAULT_MAX_WAIT = Duration.ofMillis(DEFAULT_MAX_WAIT_MILLIS); @@ -222,7 +222,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon private boolean fairness = DEFAULT_FAIRNESS; - private Duration maxWaitMillis = DEFAULT_MAX_WAIT; + private Duration maxWaitDuration = DEFAULT_MAX_WAIT; private Duration minEvictableIdleTime = DEFAULT_MIN_EVICTABLE_IDLE_TIME; @@ -401,11 +401,27 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * @return The current setting of {@code maxWait} for this * configuration instance * + * @see GenericObjectPool#getMaxWaitDuration() + * @see GenericKeyedObjectPool#getMaxWaitDuration() + * @since 2.11.0 + */ + public Duration getMaxWaitDuration() { + return maxWaitDuration; + } + + /** + * 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 + * configuration instance + * * @see GenericObjectPool#getMaxWaitMillis() * @see GenericKeyedObjectPool#getMaxWaitMillis() + * @deprecated Use {@link #getMaxWaitDuration()}. */ public long getMaxWaitMillis() { - return maxWaitMillis.toMillis(); + return maxWaitDuration.toMillis(); } /** @@ -740,11 +756,25 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon * @param maxWaitMillis The new setting of {@code maxWaitMillis} * for this configuration instance * - * @see GenericObjectPool#getMaxWaitMillis() - * @see GenericKeyedObjectPool#getMaxWaitMillis() + * @see GenericObjectPool#getMaxWaitDuration() + * @see GenericKeyedObjectPool#getMaxWaitDuration() */ public void setMaxWaitMillis(final long maxWaitMillis) { - this.maxWaitMillis = Duration.ofMillis(maxWaitMillis); + setMaxWaitDuration(Duration.ofMillis(maxWaitMillis)); + } + + /** + * Sets the value for the {@code maxWait} configuration attribute for pools + * created with this configuration instance. + * + * @param maxWaitDuration The new setting of {@code maxWaitDuration} + * for this configuration instance + * + * @see GenericObjectPool#getMaxWaitDuration() + * @see GenericKeyedObjectPool#getMaxWaitDuration() + */ + public void setMaxWaitDuration(final Duration maxWaitDuration) { + this.maxWaitDuration = PoolImplUtils.nonNull(maxWaitDuration, DEFAULT_MAX_WAIT);; } /** @@ -926,7 +956,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon builder.append(", fairness="); builder.append(fairness); builder.append(", maxWaitMillis="); - builder.append(maxWaitMillis); + builder.append(maxWaitDuration); builder.append(", minEvictableIdleTime="); builder.append(minEvictableIdleTime); builder.append(", softMinEvictableIdleTime="); 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 ed5f206..a5ae8aa 100644 --- a/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java @@ -344,13 +344,13 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T> /** * Equivalent to <code>{@link #borrowObject(Object, long) borrowObject}(key, - * {@link #getMaxWaitMillis()})</code>. + * {@link #getMaxWaitDuration()})</code>. * <p> * {@inheritDoc} */ @Override public T borrowObject(final K key) throws Exception { - return borrowObject(key, getMaxWaitMillis()); + return borrowObject(key, getMaxWaitDuration().toMillis()); } 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 e9ce2ad..b35cbb2 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java @@ -607,7 +607,8 @@ public class TestGenericObjectPool extends TestBaseObjectPool { "whenExhaustedAction"); assertEquals(expected.getMaxTotal(), actual.getMaxTotal(), "maxTotal"); assertEquals(expected.getMaxIdle(), actual.getMaxIdle(), "maxIdle"); - assertEquals(expected.getMaxWaitMillis(), actual.getMaxWaitMillis(), "maxWait"); + assertEquals(expected.getMaxWaitMillis(), actual.getMaxWaitMillis(), "maxWaitDuration"); + assertEquals(expected.getMaxWaitDuration(), actual.getMaxWaitDuration(), "maxWaitDuration"); assertEquals(expected.getMinEvictableIdleTimeMillis(), actual.getMinEvictableIdleTimeMillis(), "minEvictableIdleTimeMillis"); assertEquals(expected.getMinEvictableIdleTime(), actual.getMinEvictableIdleTime(),