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 ca34e916613ee21c25ea813cab4f3c37a0cc3031 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Mon Feb 15 12:08:40 2021 -0500 Add and use java.time.Duration APIs for AbandonedConfig timeouts instead of using ints as seconds. - Add GenericObjectPool.getRemoveAbandonedTimeoutDuration() --- src/changes/changes.xml | 5 +++-- .../apache/commons/pool2/impl/GenericObjectPool.java | 20 +++++++++++++++++++- .../commons/pool2/impl/TestGenericObjectPool.java | 2 ++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 150db44..ab72916 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -47,8 +47,9 @@ The <action> type attribute can be add,update,fix,remove. <!-- ADD --> <action dev="ggregory" type="update" due-to="Dependabot"> Add and use java.time.Duration APIs for AbandonedConfig timeouts instead of using ints for seconds. - - Add AbandonedConfig.getRemoveAbandonedTimeoutDuration() - - Add AbandonedConfig.setRemoveAbandonedTimeout(Duration) + - Add AbandonedConfig.getRemoveAbandonedTimeoutDuration(). + - Add AbandonedConfig.setRemoveAbandonedTimeout(Duration). + - Add GenericObjectPool.getRemoveAbandonedTimeoutDuration(). </action> <!-- UPDATE --> <action dev="ggregory" type="update" due-to="Dependabot"> 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 9991926..f37f614 100644 --- a/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java @@ -26,6 +26,7 @@ import org.apache.commons.pool2.SwallowedExceptionListener; import org.apache.commons.pool2.TrackedUse; import org.apache.commons.pool2.UsageTracking; +import java.time.Duration; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; @@ -83,6 +84,8 @@ import java.util.concurrent.atomic.AtomicLong; public class GenericObjectPool<T> extends BaseGenericObjectPool<T> implements ObjectPool<T>, GenericObjectPoolMXBean, UsageTracking<T> { + private static final Duration DEFAULT_REMOVE_ABANDONED_TIMEOUT = Duration.ofSeconds(Integer.MAX_VALUE); + /** * Creates a new {@code GenericObjectPool} using defaults from * {@link GenericObjectPoolConfig}. @@ -290,14 +293,29 @@ public class GenericObjectPool<T> extends BaseGenericObjectPool<T> * * @see AbandonedConfig#getRemoveAbandonedTimeout() * @see AbandonedConfig#getRemoveAbandonedTimeoutDuration() + * @deprecated Use {@link #getRemoveAbandonedTimeoutDuration()}. */ - @SuppressWarnings("deprecation") @Override + @Deprecated public int getRemoveAbandonedTimeout() { final AbandonedConfig ac = this.abandonedConfig; return ac != null ? ac.getRemoveAbandonedTimeout() : Integer.MAX_VALUE; } + /** + * Gets the timeout before which an object will be considered to be + * abandoned by this pool. + * + * @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() + */ + public Duration getRemoveAbandonedTimeoutDuration() { + final AbandonedConfig ac = this.abandonedConfig; + return ac != null ? ac.getRemoveAbandonedTimeoutDuration() : DEFAULT_REMOVE_ABANDONED_TIMEOUT; + } /** * Sets the base pool configuration. 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 a1fef27..3250d5e 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java @@ -2961,12 +2961,14 @@ public class TestGenericObjectPool extends TestBaseObjectPool { * Check that a pool that starts an evictor, but is never closed does not * leave EvictionTimer executor running. Confirmation check is in teardown. */ + @SuppressWarnings("deprecation") @Test public void testAbandonedPool() throws Exception { final GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setJmxEnabled(false); GenericObjectPool<String> abandoned = new GenericObjectPool<>(simpleFactory, config); abandoned.setTimeBetweenEvictionRunsMillis(100); // Starts evictor + assertEquals(abandoned.getRemoveAbandonedTimeout(), abandoned.getRemoveAbandonedTimeoutDuration().getSeconds()); // This is ugly, but forces gc to hit the pool final WeakReference<GenericObjectPool> ref = new WeakReference<>(abandoned);