diff --git a/src/main/java/org/jboss/threads/EnhancedQueueExecutor.java b/src/main/java/org/jboss/threads/EnhancedQueueExecutor.java index cdf932f2..45f62134 100644 --- a/src/main/java/org/jboss/threads/EnhancedQueueExecutor.java +++ b/src/main/java/org/jboss/threads/EnhancedQueueExecutor.java @@ -28,6 +28,7 @@ import java.lang.management.ManagementFactory; import java.security.AccessControlContext; import java.security.PrivilegedAction; +import java.time.Duration; import java.util.ArrayList; import java.util.Collections; import java.util.Hashtable; @@ -554,6 +555,22 @@ public Builder setKeepAliveTime(final long keepAliveTime, final TimeUnit keepAli return this; } + /** + * Set the thread keep-alive time using a {@link Duration}. + *
+ * This method was added to provide API compatibility with Infinispan 15.0.19+, which uses + * the Duration-based API. It delegates to {@link #setKeepAliveTime(long, TimeUnit)}. + * + * @param keepAliveTime the thread keep-alive time (must not be {@code null}) + * @return this builder + * @see EnhancedQueueExecutor#setKeepAliveTime(long, TimeUnit) + * @see EnhancedQueueExecutor#setKeepAliveTime(Duration) + */ + public Builder setKeepAliveTime(final Duration keepAliveTime) { + Assert.checkNotNullParam("keepAliveTime", keepAliveTime); + return setKeepAliveTime(keepAliveTime.toNanos(), TimeUnit.NANOSECONDS); + } + /** * Get the thread pool growth resistance. This is the average fraction of submitted tasks that will be enqueued (instead * of causing a new thread to start) when there are no idle threads and the pool size is equal to or greater than @@ -1173,6 +1190,23 @@ public void setKeepAliveTime(final long keepAliveTime, final TimeUnit keepAliveU timeoutNanos = max(1L, keepAliveUnits.toNanos(keepAliveTime)); } + /** + * Set the thread keep-alive time using a {@link Duration}. This is the minimum length of time that idle threads + * should remain until they exit. Unless core threads are allowed to time out, threads will only exit if the + * current thread count exceeds the core limit. + *
+ * This method was added to provide API compatibility with Infinispan 15.0.19+, which uses + * the Duration-based API. It delegates to {@link #setKeepAliveTime(long, TimeUnit)}. + * + * @param keepAliveTime the thread keep-alive time (must not be {@code null}) + * @see Builder#setKeepAliveTime(Duration) Builder.setKeepAliveTime() + * @see #setKeepAliveTime(long, TimeUnit) + */ + public void setKeepAliveTime(final Duration keepAliveTime) { + Assert.checkNotNullParam("keepAliveTime", keepAliveTime); + setKeepAliveTime(keepAliveTime.toNanos(), TimeUnit.NANOSECONDS); + } + /** * Get the maximum queue size. If the queue is full and a task cannot be immediately accepted, rejection will result. * diff --git a/src/test/java/org/jboss/threads/EnhancedThreadQueueExecutorTestCase.java b/src/test/java/org/jboss/threads/EnhancedThreadQueueExecutorTestCase.java index cdd1f554..27b070c6 100644 --- a/src/test/java/org/jboss/threads/EnhancedThreadQueueExecutorTestCase.java +++ b/src/test/java/org/jboss/threads/EnhancedThreadQueueExecutorTestCase.java @@ -18,6 +18,7 @@ package org.jboss.threads; +import java.time.Duration; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -396,4 +397,32 @@ public void run() { executor.shutdown(); Assert.assertTrue(terminateLatch.await(10, TimeUnit.SECONDS)); } + + /** + * Test that the Duration-based setKeepAliveTime method works correctly. + * This verifies compatibility with Infinispan 15.0.19+ which uses Duration API. + */ + @Test + public void testSetKeepAliveTimeWithDuration() throws Exception { + // Test Builder.setKeepAliveTime(Duration) + EnhancedQueueExecutor executor = new EnhancedQueueExecutor.Builder() + .setCorePoolSize(coreSize) + .setMaximumPoolSize(maxSize) + .setKeepAliveTime(Duration.ofMillis(keepaliveTimeMillis)) + .build(); + + // Verify the keepalive time was set correctly + long actualKeepAliveMillis = executor.getKeepAliveTime(TimeUnit.MILLISECONDS); + Assert.assertEquals("KeepAlive time should match the value set via Duration API", + keepaliveTimeMillis, actualKeepAliveMillis); + + // Test runtime setKeepAliveTime(Duration) on the executor instance + long newKeepAliveMillis = 5000; + executor.setKeepAliveTime(Duration.ofMillis(newKeepAliveMillis)); + actualKeepAliveMillis = executor.getKeepAliveTime(TimeUnit.MILLISECONDS); + Assert.assertEquals("KeepAlive time should match the value set via Duration API at runtime", + newKeepAliveMillis, actualKeepAliveMillis); + + executor.shutdown(); + } }