You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/02/15 18:44:13 UTC
[commons-pool] 01/05: Add and use java.time.Duration APIs for
AbandonedConfig timeouts instead of using ints as seconds. - Add
AbandonedConfig.getRemoveAbandonedTimeoutDuration() - Add
AbandonedConfig.setRemoveAbandonedTimeout(Duration)
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 eb00d5b9e9b7573c35f98ea7d31390b778b8011b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Feb 15 10:00:17 2021 -0500
Add and use java.time.Duration APIs for AbandonedConfig timeouts instead
of using ints as seconds.
- Add AbandonedConfig.getRemoveAbandonedTimeoutDuration()
- Add AbandonedConfig.setRemoveAbandonedTimeout(Duration)
---
pom.xml | 6 +--
src/changes/changes.xml | 10 ++++-
.../apache/commons/pool2/impl/AbandonedConfig.java | 44 ++++++++++++++++++++--
.../commons/pool2/impl/GenericObjectPool.java | 12 +++---
.../pool2/impl/TestAbandonedObjectPool.java | 16 +++++---
.../apache/commons/pool2/impl/TestConstants.java | 26 +++++++++++++
.../pool2/impl/TestDefaultPooledObjectInfo.java | 2 +-
.../pool2/proxy/BaseTestProxiedObjectPool.java | 5 ++-
8 files changed, 100 insertions(+), 21 deletions(-)
diff --git a/pom.xml b/pom.xml
index 560f106..fbce2e7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
</parent>
<artifactId>commons-pool2</artifactId>
- <version>2.9.1-SNAPSHOT</version>
+ <version>2.10.0-SNAPSHOT</version>
<name>Apache Commons Pool</name>
<inceptionYear>2001</inceptionYear>
@@ -158,7 +158,7 @@
<commons.module.name>org.apache.commons.pool2</commons.module.name>
<commons.rc.version>RC1</commons.rc.version>
<!-- Java 8 -->
- <commons.release.version>2.9.0</commons.release.version>
+ <commons.release.version>2.10.0</commons.release.version>
<commons.release.desc>(Java 8)</commons.release.desc>
<!-- Java 7 -->
<commons.release.2.version>2.6.2</commons.release.2.version>
@@ -185,7 +185,7 @@
<spotbugs.impl.version>4.2.0</spotbugs.impl.version>
<!-- Commons Release Plugin -->
- <commons.bc.version>2.8.1</commons.bc.version>
+ <commons.bc.version>2.9.0</commons.bc.version>
<commons.release.isDistModule>true</commons.release.isDistModule>
<commons.releaseManagerName>Gary Gregory</commons.releaseManagerName>
<commons.releaseManagerKey>86fdc7e2a11262cb</commons.releaseManagerKey>
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 18352ff..150db44 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -43,8 +43,14 @@ The <action> type attribute can be add,update,fix,remove.
<title>Apache Commons Pool Release Notes</title>
</properties>
<body>
- <release version="2.9.1" date="202Y-MM-DD" description="This is a MMMM release (Java 8).">
- <!-- UPDATES -->
+ <release version="2.10.0" date="202Y-MM-DD" description="This is a MMMM release (Java 8).">
+ <!-- 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)
+ </action>
+ <!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Dependabot">
Bump spotbugs-maven-plugin from 4.0.4 to 4.2.0 #48, #53, #59.
</action>
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 85c1cc4..91e2eea 100644
--- a/src/main/java/org/apache/commons/pool2/impl/AbandonedConfig.java
+++ b/src/main/java/org/apache/commons/pool2/impl/AbandonedConfig.java
@@ -20,6 +20,7 @@ package org.apache.commons.pool2.impl;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.Charset;
+import java.time.Duration;
import org.apache.commons.pool2.TrackedUse;
import org.apache.commons.pool2.UsageTracking;
@@ -102,9 +103,9 @@ public class AbandonedConfig {
}
/**
- * Timeout in seconds before an abandoned object can be removed.
+ * Timeout before an abandoned object can be removed.
*/
- private int removeAbandonedTimeout = 300;
+ private Duration removeAbandonedTimeout = Duration.ofSeconds(300);
/**
* <p>Timeout in seconds before an abandoned object can be removed.</p>
@@ -116,12 +117,47 @@ public class AbandonedConfig {
* <p>The default value is 300 seconds.</p>
*
* @return the abandoned object timeout in seconds
+ * @deprecated Use {@link #getRemoveAbandonedTimeoutDuration()}.
*/
+ @Deprecated
public int getRemoveAbandonedTimeout() {
+ return (int) this.removeAbandonedTimeout.getSeconds();
+ }
+
+ /**
+ * <p>Timeout before an abandoned object can be removed.</p>
+ *
+ * <p>The time of most recent use of an object is the maximum (latest) of
+ * {@link TrackedUse#getLastUsed()} (if this class of the object implements
+ * TrackedUse) and the time when the object was borrowed from the pool.</p>
+ *
+ * <p>The default value is 300 seconds.</p>
+ *
+ * @return the abandoned object timeout.
+ * @since 2.10.0
+ */
+ public Duration getRemoveAbandonedTimeoutDuration() {
return this.removeAbandonedTimeout;
}
/**
+ * <p>Sets the timeout before an abandoned object can be
+ * removed</p>
+ *
+ * <p>Setting this property has no effect if
+ * {@link #getRemoveAbandonedOnBorrow() removeAbandonedOnBorrow} and
+ * {@link #getRemoveAbandonedOnMaintenance() removeAbandonedOnMaintenance}
+ * are both false.</p>
+ *
+ * @param removeAbandonedTimeout new abandoned timeout
+ * @see #getRemoveAbandonedTimeout()
+ * @since 2.10.0
+ */
+ public void setRemoveAbandonedTimeout(final Duration removeAbandonedTimeout) {
+ this.removeAbandonedTimeout = removeAbandonedTimeout;
+ }
+
+ /**
* <p>Sets the timeout in seconds before an abandoned object can be
* removed</p>
*
@@ -132,9 +168,11 @@ public class AbandonedConfig {
*
* @param removeAbandonedTimeout new abandoned timeout in seconds
* @see #getRemoveAbandonedTimeout()
+ * @deprecated Use {@link #setRemoveAbandonedTimeout(Duration)}.
*/
+ @Deprecated
public void setRemoveAbandonedTimeout(final int removeAbandonedTimeout) {
- this.removeAbandonedTimeout = removeAbandonedTimeout;
+ setRemoveAbandonedTimeout(Duration.ofSeconds(removeAbandonedTimeout));
}
/**
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 6bb571b..9991926 100644
--- a/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
+++ b/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
@@ -282,14 +282,16 @@ public class GenericObjectPool<T> extends BaseGenericObjectPool<T>
}
/**
- * Obtains the timeout before which an object will be considered to be
+ * 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()
*/
+ @SuppressWarnings("deprecation")
@Override
public int getRemoveAbandonedTimeout() {
final AbandonedConfig ac = this.abandonedConfig;
@@ -328,7 +330,7 @@ public class GenericObjectPool<T> extends BaseGenericObjectPool<T>
this.abandonedConfig.setLogWriter(abandonedConfig.getLogWriter());
this.abandonedConfig.setRemoveAbandonedOnBorrow(abandonedConfig.getRemoveAbandonedOnBorrow());
this.abandonedConfig.setRemoveAbandonedOnMaintenance(abandonedConfig.getRemoveAbandonedOnMaintenance());
- this.abandonedConfig.setRemoveAbandonedTimeout(abandonedConfig.getRemoveAbandonedTimeout());
+ this.abandonedConfig.setRemoveAbandonedTimeout(abandonedConfig.getRemoveAbandonedTimeoutDuration());
this.abandonedConfig.setUseUsageTracking(abandonedConfig.getUseUsageTracking());
this.abandonedConfig.setRequireFullStackTrace(abandonedConfig.getRequireFullStackTrace());
}
@@ -1070,7 +1072,7 @@ public class GenericObjectPool<T> extends BaseGenericObjectPool<T>
// Generate a list of abandoned objects to remove
final long nowMillis = System.currentTimeMillis();
final long timeoutMillis =
- nowMillis - (abandonedConfig.getRemoveAbandonedTimeout() * 1000L);
+ nowMillis - abandonedConfig.getRemoveAbandonedTimeoutDuration().toMillis();
final ArrayList<PooledObject<T>> remove = new ArrayList<>();
final Iterator<PooledObject<T>> it = allObjects.values().iterator();
while (it.hasNext()) {
@@ -1202,7 +1204,7 @@ public class GenericObjectPool<T> extends BaseGenericObjectPool<T>
* {@link #_maxActive} objects created at any one time.
*/
private final AtomicLong createCount = new AtomicLong(0);
- private long makeObjectCount = 0;
+ private long makeObjectCount;
private final Object makeObjectCountLock = new Object();
private final LinkedBlockingDeque<PooledObject<T>> idleObjects;
@@ -1211,7 +1213,7 @@ public class GenericObjectPool<T> extends BaseGenericObjectPool<T>
"org.apache.commons.pool2:type=GenericObjectPool,name=";
// Additional configuration properties for abandoned object tracking
- private volatile AbandonedConfig abandonedConfig = null;
+ private volatile AbandonedConfig abandonedConfig;
@Override
protected void toStringAppendFields(final StringBuilder builder) {
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 e576a85..39886aa 100644
--- a/src/test/java/org/apache/commons/pool2/impl/TestAbandonedObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/impl/TestAbandonedObjectPool.java
@@ -45,9 +45,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
* TestCase for AbandonedObjectPool
*/
public class TestAbandonedObjectPool {
+
private GenericObjectPool<PooledTestObject> pool = null;
private AbandonedConfig abandonedConfig = null;
+ @SuppressWarnings("deprecation")
@BeforeEach
public void setUp() throws Exception {
abandonedConfig = new AbandonedConfig();
@@ -57,6 +59,10 @@ public class TestAbandonedObjectPool {
abandonedConfig.setRemoveAbandonedOnBorrow(true);
abandonedConfig.setRemoveAbandonedTimeout(1);
+ assertEquals(TestConstants.ONE_SECOND, abandonedConfig.getRemoveAbandonedTimeoutDuration());
+ abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND);
+ assertEquals(1, abandonedConfig.getRemoveAbandonedTimeout());
+
pool = new GenericObjectPool<>(
new SimpleFactory(),
new GenericObjectPoolConfig<PooledTestObject>(),
@@ -148,7 +154,7 @@ public class TestAbandonedObjectPool {
public void testAbandonedReturn() throws Exception {
abandonedConfig = new AbandonedConfig();
abandonedConfig.setRemoveAbandonedOnBorrow(true);
- abandonedConfig.setRemoveAbandonedTimeout(1);
+ abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND);
pool.close(); // Unregister pool created by setup
pool = new GenericObjectPool<>(
new SimpleFactory(200, 0),
@@ -183,7 +189,7 @@ public class TestAbandonedObjectPool {
public void testAbandonedInvalidate() throws Exception {
abandonedConfig = new AbandonedConfig();
abandonedConfig.setRemoveAbandonedOnMaintenance(true);
- abandonedConfig.setRemoveAbandonedTimeout(1);
+ abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND);
pool.close(); // Unregister pool created by setup
pool = new GenericObjectPool<>(
// destroys take 200 ms
@@ -207,7 +213,7 @@ public class TestAbandonedObjectPool {
public void testDestroyModeAbandoned() throws Exception {
abandonedConfig = new AbandonedConfig();
abandonedConfig.setRemoveAbandonedOnMaintenance(true);
- abandonedConfig.setRemoveAbandonedTimeout(1);
+ abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND);
pool.close(); // Unregister pool created by setup
pool = new GenericObjectPool<>(
// validate takes 1 second
@@ -241,7 +247,7 @@ public class TestAbandonedObjectPool {
public void testRemoveAbandonedWhileReturning() throws Exception {
abandonedConfig = new AbandonedConfig();
abandonedConfig.setRemoveAbandonedOnMaintenance(true);
- abandonedConfig.setRemoveAbandonedTimeout(1);
+ abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND);
pool.close(); // Unregister pool created by setup
pool = new GenericObjectPool<>(
// validate takes 1 second
@@ -299,7 +305,7 @@ public class TestAbandonedObjectPool {
public void testStackTrace() throws Exception {
abandonedConfig.setRemoveAbandonedOnMaintenance(true);
abandonedConfig.setLogAbandoned(true);
- abandonedConfig.setRemoveAbandonedTimeout(1);
+ abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND);
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
new file mode 100644
index 0000000..14d182f
--- /dev/null
+++ b/src/test/java/org/apache/commons/pool2/impl/TestConstants.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.pool2.impl;
+
+import java.time.Duration;
+
+public class TestConstants {
+
+ public static final Duration ONE_SECOND = Duration.ofSeconds(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 9a49a6b..50b3050 100644
--- a/src/test/java/org/apache/commons/pool2/impl/TestDefaultPooledObjectInfo.java
+++ b/src/test/java/org/apache/commons/pool2/impl/TestDefaultPooledObjectInfo.java
@@ -112,7 +112,7 @@ public class TestDefaultPooledObjectInfo {
final AbandonedConfig abandonedConfig = new AbandonedConfig();
abandonedConfig.setRemoveAbandonedOnBorrow(true);
- abandonedConfig.setRemoveAbandonedTimeout(1);
+ abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND);
abandonedConfig.setLogAbandoned(true);
try (final GenericObjectPool<String> pool = new GenericObjectPool<>(new SimpleFactory(),
new GenericObjectPoolConfig<String>(), abandonedConfig)) {
diff --git a/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedObjectPool.java b/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedObjectPool.java
index 3db3d1d..4e1b93c 100644
--- a/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedObjectPool.java
@@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.PrintWriter;
import java.io.StringWriter;
+import java.time.Duration;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.ObjectPool;
@@ -39,7 +40,7 @@ import org.junit.jupiter.api.Test;
public abstract class BaseTestProxiedObjectPool {
private static final String DATA1 = "data1";
- private static final int ABANDONED_TIMEOUT_SECS = 3;
+ private static final Duration ABANDONED_TIMEOUT_SECS = Duration.ofSeconds(3);
private ObjectPool<TestObject> pool = null;
private StringWriter log = null;
@@ -130,7 +131,7 @@ public abstract class BaseTestProxiedObjectPool {
obj.setData(DATA1);
// Sleep long enough for the object to be considered abandoned
- Thread.sleep((ABANDONED_TIMEOUT_SECS + 2) * 1000);
+ Thread.sleep(ABANDONED_TIMEOUT_SECS.plusSeconds(2).toMillis());
// Borrow another object to trigger the abandoned object processing
pool.borrowObject();