This is an automated email from the ASF dual-hosted git repository. tv pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-jcs.git
commit 97e05ff9b969f6bde165e081841bbf0ec6e17e76 Author: Thomas Vandahl <[email protected]> AuthorDate: Tue Feb 17 22:32:54 2026 +0100 Use Duration for time-related variables (part one) --- .../jcs4/auxiliary/disk/AbstractDiskCache.java | 3 ++- .../auxiliary/disk/AbstractDiskCacheAttributes.java | 13 +++++++------ .../disk/behavior/IDiskCacheAttributes.java | 7 ++++--- .../jcs4/auxiliary/disk/block/BlockDiskCache.java | 7 ++++--- .../disk/block/BlockDiskCacheAttributes.java | 14 ++++++++------ .../auxiliary/disk/jdbc/JDBCDiskCacheAttributes.java | 20 +++++++++++--------- .../auxiliary/disk/jdbc/JDBCDiskCacheFactory.java | 6 +++--- .../jcs4/auxiliary/disk/jdbc/ShrinkerThread.java | 18 ++++++++++-------- .../disk/jdbc/dsfactory/JndiDataSourceFactory.java | 4 ++-- .../socket/tcp/LateralTCPCacheAttributes.java | 18 ++++++++++-------- .../lateral/socket/tcp/LateralTCPListener.java | 5 +++-- .../lateral/socket/tcp/LateralTCPSender.java | 11 ++++++----- .../tcp/behavior/ILateralTCPCacheAttributes.java | 6 ++++-- .../remote/AbstractRemoteAuxiliaryCache.java | 12 +++++++----- .../remote/CommonRemoteCacheAttributes.java | 12 +++++++----- .../commons/jcs4/auxiliary/remote/RemoteCache.java | 3 ++- .../jcs4/auxiliary/remote/RemoteCacheAttributes.java | 14 ++++++++------ .../commons/jcs4/auxiliary/remote/RemoteUtils.java | 15 ++++++++------- .../behavior/ICommonRemoteCacheAttributes.java | 8 +++++--- .../remote/behavior/IRemoteCacheAttributes.java | 6 ++++-- .../remote/server/RemoteCacheServerAttributes.java | 16 +++++++++------- .../remote/server/RemoteCacheServerFactory.java | 4 ++-- .../server/TimeoutConfigurableRMISocketFactory.java | 17 +++++++++-------- .../behavior/IRemoteCacheServerAttributes.java | 6 ++++-- .../commons/jcs4/engine/AbstractCacheEventQueue.java | 19 ++++++++++--------- .../apache/commons/jcs4/engine/CacheEventQueue.java | 5 +++-- .../commons/jcs4/engine/PooledCacheEventQueue.java | 10 ++++++---- .../jcs4/engine/behavior/ICacheEventQueue.java | 7 ++++--- .../jcs4/engine/behavior/IElementSerializer.java | 4 ++-- .../server/RemoteCacheServerFactoryUnitTest.java | 18 ++++++++++-------- .../jcs4/engine/EventQueueConcurrentLoadTest.java | 3 ++- 31 files changed, 176 insertions(+), 135 deletions(-) diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/AbstractDiskCache.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/AbstractDiskCache.java index ed760c6f..766eec26 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/AbstractDiskCache.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/AbstractDiskCache.java @@ -20,6 +20,7 @@ package org.apache.commons.jcs4.auxiliary.disk; */ import java.io.IOException; +import java.time.Duration; import java.util.Collections; import java.util.HashSet; import java.util.Map; @@ -287,7 +288,7 @@ public abstract class AbstractDiskCache<K, V> log.info( "In dispose, destroying event queue." ); // wait for dispose and then quit if not done. - int spoolTimeLimit = getAuxiliaryCacheAttributes().getShutdownSpoolTimeLimit(); + Duration spoolTimeLimit = getAuxiliaryCacheAttributes().getShutdownSpoolTimeLimit(); cacheEventQueue.destroy(spoolTimeLimit); // Invoke any implementation specific disposal code diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/AbstractDiskCacheAttributes.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/AbstractDiskCacheAttributes.java index 201af1a7..76afb770 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/AbstractDiskCacheAttributes.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/AbstractDiskCacheAttributes.java @@ -20,6 +20,7 @@ package org.apache.commons.jcs4.auxiliary.disk; */ import java.io.File; +import java.time.Duration; import org.apache.commons.jcs4.auxiliary.AbstractAuxiliaryCacheAttributes; import org.apache.commons.jcs4.auxiliary.disk.behavior.IDiskCacheAttributes; @@ -37,7 +38,7 @@ public abstract class AbstractDiskCacheAttributes extends AbstractAuxiliaryCache private static final Log log = Log.getLog(AbstractDiskCacheAttributes.class); /** Default amount of time to allow for key persistence on shutdown */ - private static final int DEFAULT_shutdownSpoolTimeLimit = 60; + private static final Duration DEFAULT_shutdownSpoolTimeLimit = Duration.ofSeconds(60); /** Path to disk */ private File diskPath; @@ -52,7 +53,7 @@ public abstract class AbstractDiskCacheAttributes extends AbstractAuxiliaryCache * This default determines how long the shutdown will wait for the key spool and data defrag to * finish. */ - private int shutdownSpoolTimeLimit = DEFAULT_shutdownSpoolTimeLimit; + private Duration shutdownSpoolTimeLimit = DEFAULT_shutdownSpoolTimeLimit; /** Type of disk limit: SIZE or COUNT */ private DiskLimitType diskLimitType = DiskLimitType.COUNT; @@ -88,14 +89,14 @@ public abstract class AbstractDiskCacheAttributes extends AbstractAuxiliaryCache } /** - * Gets the amount of time in seconds we will wait for elements to move to disk during shutdown + * Gets the amount of time we will wait for elements to move to disk during shutdown * for a particular region. * <p> * - * @return the time in seconds. + * @return the time. */ @Override - public int getShutdownSpoolTimeLimit() + public Duration getShutdownSpoolTimeLimit() { return this.shutdownSpoolTimeLimit; } @@ -191,7 +192,7 @@ public abstract class AbstractDiskCacheAttributes extends AbstractAuxiliaryCache */ public void setShutdownSpoolTimeLimit(final int shutdownSpoolTimeLimit) { - this.shutdownSpoolTimeLimit = shutdownSpoolTimeLimit; + this.shutdownSpoolTimeLimit = Duration.ofSeconds(shutdownSpoolTimeLimit); } /** diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/behavior/IDiskCacheAttributes.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/behavior/IDiskCacheAttributes.java index 9e9b783a..2c85cd98 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/behavior/IDiskCacheAttributes.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/behavior/IDiskCacheAttributes.java @@ -20,6 +20,7 @@ package org.apache.commons.jcs4.auxiliary.disk.behavior; */ import java.io.File; +import java.time.Duration; import org.apache.commons.jcs4.auxiliary.AuxiliaryCacheAttributes; @@ -63,12 +64,12 @@ public interface IDiskCacheAttributes int getMaxPurgatorySize(); /** - * Gets the amount of time in seconds we will wait for elements to move to + * Gets the amount of time we will wait for elements to move to * disk during shutdown for a particular region. * - * @return the time in seconds. + * @return the time. */ - int getShutdownSpoolTimeLimit(); + Duration getShutdownSpoolTimeLimit(); /** * If this is true then remove all is not prohibited. diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskCache.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskCache.java index a776cea4..6dcc7898 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskCache.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskCache.java @@ -21,6 +21,7 @@ package org.apache.commons.jcs4.auxiliary.disk.block; import java.io.File; import java.io.IOException; +import java.time.Duration; import java.util.Arrays; import java.util.HashSet; import java.util.List; @@ -624,11 +625,11 @@ public class BlockDiskCache<K, V> { // add this region to the persistence thread. // TODO we might need to stagger this a bit. - long interval = getAuxiliaryCacheAttributes().getKeyPersistenceIntervalSeconds(); - if ( interval > 0 ) + Duration interval = getAuxiliaryCacheAttributes().getKeyPersistenceInterval(); + if ( interval.toSeconds() > 0 ) { future = scheduledExecutor.scheduleAtFixedRate(keyStore::saveKeys, - interval, interval, TimeUnit.SECONDS); + interval.toSeconds(), interval.toSeconds(), TimeUnit.SECONDS); } } diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskCacheAttributes.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskCacheAttributes.java index 1555fdd1..177b8071 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskCacheAttributes.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskCacheAttributes.java @@ -1,5 +1,7 @@ package org.apache.commons.jcs4.auxiliary.disk.block; +import java.time.Duration; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -34,7 +36,7 @@ public class BlockDiskCacheAttributes private static final int DEFAULT_MAX_KEY_SIZE = 5000; /** How often should we persist the keys. */ - private static final long DEFAULT_KEY_PERSISTENCE_INTERVAL_SECONDS = 5 * 60; + private static final Duration DEFAULT_KEY_PERSISTENCE_INTERVAL = Duration.ofSeconds(5 * 60); /** The size per block in bytes. */ private int blockSizeBytes; @@ -43,7 +45,7 @@ public class BlockDiskCacheAttributes private int maxKeySize = DEFAULT_MAX_KEY_SIZE; /** The keys will be persisted at this interval. -1 mean never. */ - private long keyPersistenceIntervalSeconds = DEFAULT_KEY_PERSISTENCE_INTERVAL_SECONDS; + private Duration keyPersistenceInterval = DEFAULT_KEY_PERSISTENCE_INTERVAL; /** * @return the blockSizeBytes. @@ -56,9 +58,9 @@ public class BlockDiskCacheAttributes /** * @return the keyPersistenceIntervalSeconds. */ - public long getKeyPersistenceIntervalSeconds() + public Duration getKeyPersistenceInterval() { - return keyPersistenceIntervalSeconds; + return keyPersistenceInterval; } /** @@ -84,7 +86,7 @@ public class BlockDiskCacheAttributes */ public void setKeyPersistenceIntervalSeconds( final long keyPersistenceIntervalSeconds ) { - this.keyPersistenceIntervalSeconds = keyPersistenceIntervalSeconds; + this.keyPersistenceInterval = Duration.ofSeconds(keyPersistenceIntervalSeconds); } /** @@ -109,7 +111,7 @@ public class BlockDiskCacheAttributes str.append( "\n MaxKeySize [" + getMaxKeySize() + "]" ); str.append( "\n MaxPurgatorySize [" + getMaxPurgatorySize() + "]" ); str.append( "\n BlockSizeBytes [" + getBlockSizeBytes() + "]" ); - str.append( "\n KeyPersistenceIntervalSeconds [" + getKeyPersistenceIntervalSeconds() + "]" ); + str.append( "\n KeyPersistenceIntervalSeconds [" + getKeyPersistenceInterval() + "]" ); str.append( "\n DiskLimitType [" + getDiskLimitType() + "]" ); return str.toString(); } diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/JDBCDiskCacheAttributes.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/JDBCDiskCacheAttributes.java index 55538429..da388bb5 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/JDBCDiskCacheAttributes.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/JDBCDiskCacheAttributes.java @@ -1,5 +1,7 @@ package org.apache.commons.jcs4.auxiliary.disk.jdbc; +import java.time.Duration; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -37,7 +39,7 @@ public class JDBCDiskCacheAttributes public static final int DEFAULT_MAX_TOTAL = 10; /** This is the default setting for the cleanup routine. */ - public static final int DEFAULT_SHRINKER_INTERVAL_SECONDS = 300; + public static final Duration DEFAULT_SHRINKER_INTERVAL = Duration.ofSeconds(300); /** The default Pool Name to which the connection pool will be keyed. */ public static final String DEFAULT_POOL_NAME = "jcs"; @@ -58,7 +60,7 @@ public class JDBCDiskCacheAttributes private String jndiPath; /** The time between two JNDI lookups */ - private long jndiTTL; + private Duration jndiTTL; /** The table name */ private String tableName = DEFAULT_TABLE_NAME; @@ -70,7 +72,7 @@ public class JDBCDiskCacheAttributes private int maxTotal = DEFAULT_MAX_TOTAL; /** How often should we remove expired. */ - private int shrinkerIntervalSeconds = DEFAULT_SHRINKER_INTERVAL_SECONDS; + private Duration shrinkerInterval = DEFAULT_SHRINKER_INTERVAL; /** Should we remove expired in the background. */ private boolean useDiskShrinker = true; @@ -116,7 +118,7 @@ public class JDBCDiskCacheAttributes /** * @return the jndiTTL */ - public long getJndiTTL() + public Duration getJndiTTL() { return jndiTTL; } @@ -140,9 +142,9 @@ public class JDBCDiskCacheAttributes /** * @return the shrinkerIntervalSeconds. */ - public int getShrinkerIntervalSeconds() + public Duration getShrinkerInterval() { - return shrinkerIntervalSeconds; + return shrinkerInterval; } /** @@ -214,7 +216,7 @@ public class JDBCDiskCacheAttributes */ public void setJndiTTL(final long jndiTTL) { - this.jndiTTL = jndiTTL; + this.jndiTTL = Duration.ofMillis(jndiTTL); } /** @@ -238,7 +240,7 @@ public class JDBCDiskCacheAttributes */ public void setShrinkerIntervalSeconds( final int shrinkerIntervalSecondsArg ) { - this.shrinkerIntervalSeconds = shrinkerIntervalSecondsArg; + this.shrinkerInterval = Duration.ofSeconds(shrinkerIntervalSecondsArg); } /** @@ -301,7 +303,7 @@ public class JDBCDiskCacheAttributes buf.append( "\n TestBeforeInsert [" + isTestBeforeInsert() + "]" ); buf.append( "\n MaxActive [" + getMaxTotal() + "]" ); buf.append( "\n AllowRemoveAll [" + isAllowRemoveAll() + "]" ); - buf.append( "\n ShrinkerIntervalSeconds [" + getShrinkerIntervalSeconds() + "]" ); + buf.append( "\n ShrinkerIntervalSeconds [" + getShrinkerInterval() + "]" ); buf.append( "\n useDiskShrinker [" + isUseDiskShrinker() + "]" ); return buf.toString(); } diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/JDBCDiskCacheFactory.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/JDBCDiskCacheFactory.java index 688062c9..2927e148 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/JDBCDiskCacheFactory.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/JDBCDiskCacheFactory.java @@ -117,10 +117,10 @@ public class JDBCDiskCacheFactory if ( cattr.isUseDiskShrinker() ) { final ScheduledExecutorService shrinkerService = getScheduledExecutorService(); - final ShrinkerThread shrinkerThread = shrinkerThreadMap.computeIfAbsent(cattr.getTableName(), key -> { + final ShrinkerThread shrinkerThread = shrinkerThreadMap.computeIfAbsent(cattr.getTableName(), key -> + { final ShrinkerThread newShrinkerThread = new ShrinkerThread(); - - final long intervalMillis = Math.max( 999, cattr.getShrinkerIntervalSeconds() * 1000 ); + final long intervalMillis = Math.max(999, cattr.getShrinkerInterval().toMillis()); log.info( "Setting the shrinker to run every [{0}] ms. for table [{1}]", intervalMillis, key ); shrinkerService.scheduleAtFixedRate(newShrinkerThread, 0, intervalMillis, TimeUnit.MILLISECONDS); diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/ShrinkerThread.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/ShrinkerThread.java index 10ad6936..c2c52452 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/ShrinkerThread.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/ShrinkerThread.java @@ -1,5 +1,7 @@ package org.apache.commons.jcs4.auxiliary.disk.jdbc; +import java.time.Duration; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -36,7 +38,7 @@ public class ShrinkerThread private static final Log log = Log.getLog( ShrinkerThread.class ); /** Default time period to use. */ - private static final long DEFAULT_PAUSE_BETWEEN_REGION_CALLS_MILLIS = 5000; + private static final Duration DEFAULT_PAUSE_BETWEEN_REGION_CALLS = Duration.ofMillis(5000); /** A set of JDBCDiskCache objects to call deleteExpired on. */ private final CopyOnWriteArraySet<JDBCDiskCache<?, ?>> shrinkSet = @@ -47,7 +49,7 @@ public class ShrinkerThread * of regions. Delete can lock the table. We want to give clients a chance to get some work * done. */ - private long pauseBetweenRegionCallsMillis = DEFAULT_PAUSE_BETWEEN_REGION_CALLS_MILLIS; + private Duration pauseBetweenRegionCalls = DEFAULT_PAUSE_BETWEEN_REGION_CALLS; /** * Does nothing special. @@ -91,11 +93,11 @@ public class ShrinkerThread if ( i.hasNext() ) { log.info( "Pausing for [{0}] ms before shrinking the next region.", - getPauseBetweenRegionCallsMillis() ); + getPauseBetweenRegionCalls().toMillis() ); try { - Thread.sleep( getPauseBetweenRegionCallsMillis() ); + Thread.sleep( getPauseBetweenRegionCalls().toMillis() ); } catch ( final InterruptedException e ) { @@ -109,11 +111,11 @@ public class ShrinkerThread * How long should we wait between calls to deleteExpired when we are iterating through the list * of regions. * - * @return the pauseBetweenRegionCallsMillis. + * @return the pauseBetweenRegionCalls. */ - public long getPauseBetweenRegionCallsMillis() + public Duration getPauseBetweenRegionCalls() { - return pauseBetweenRegionCallsMillis; + return pauseBetweenRegionCalls; } /** @@ -140,6 +142,6 @@ public class ShrinkerThread */ public void setPauseBetweenRegionCallsMillis( final long pauseBetweenRegionCallsMillis ) { - this.pauseBetweenRegionCallsMillis = pauseBetweenRegionCallsMillis; + this.pauseBetweenRegionCalls = Duration.ofMillis(pauseBetweenRegionCallsMillis); } } diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/dsfactory/JndiDataSourceFactory.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/dsfactory/JndiDataSourceFactory.java index a54dbf7d..20790397 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/dsfactory/JndiDataSourceFactory.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/dsfactory/JndiDataSourceFactory.java @@ -149,8 +149,8 @@ public class JndiDataSourceFactory implements DataSourceFactory this.path = config.getJndiPath(); log.debug("JNDI path: {0}", path); - this.ttl = config.getJndiTTL(); - log.debug("Time between context lookups: {0}", ttl); + this.ttl = config.getJndiTTL().toMillis(); + log.debug("Time between context lookups: {0} ms.", ttl); final Hashtable<String, Object> env = new Hashtable<>(); ctx = new InitialContext(env); diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPCacheAttributes.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPCacheAttributes.java index d0034666..24ff04e7 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPCacheAttributes.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPCacheAttributes.java @@ -1,5 +1,7 @@ package org.apache.commons.jcs4.auxiliary.lateral.socket.tcp; +import java.time.Duration; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -55,10 +57,10 @@ public class LateralTCPCacheAttributes private static final boolean DEFAULT_FILTER_REMOVE_BY_HASH_CODE = true; /** Default - Only block for 1 second before timing out on a read.*/ - private static final int DEFAULT_SOCKET_TIME_OUT = 1000; + private static final Duration DEFAULT_SOCKET_TIME_OUT = Duration.ofMillis(1000); /** Default - Only block for 2 seconds before timing out on startup.*/ - private static final int DEFAULT_OPEN_TIMEOUT = 2000; + private static final Duration DEFAULT_OPEN_TIMEOUT = Duration.ofMillis(2000); /** TCP -------------------------------------------- */ private String tcpServers = ""; @@ -100,10 +102,10 @@ public class LateralTCPCacheAttributes private boolean filterRemoveByHashCode = DEFAULT_FILTER_REMOVE_BY_HASH_CODE; /** Only block for socketTimeOut seconds before timing out on a read. */ - private int socketTimeOut = DEFAULT_SOCKET_TIME_OUT; + private Duration socketTimeOut = DEFAULT_SOCKET_TIME_OUT; /** Only block for openTimeOut seconds before timing out on startup. */ - private int openTimeOut = DEFAULT_OPEN_TIMEOUT; + private Duration openTimeOut = DEFAULT_OPEN_TIMEOUT; /** Default receive setting */ @@ -125,7 +127,7 @@ public class LateralTCPCacheAttributes * @return the openTimeOut */ @Override - public int getOpenTimeOut() + public Duration getOpenTimeOut() { return openTimeOut; } @@ -134,7 +136,7 @@ public class LateralTCPCacheAttributes * @return the socketTimeOut */ @Override - public int getSocketTimeOut() + public Duration getSocketTimeOut() { return socketTimeOut; } @@ -342,7 +344,7 @@ public class LateralTCPCacheAttributes */ public void setOpenTimeOut( final int openTimeOut ) { - this.openTimeOut = openTimeOut; + this.openTimeOut = Duration.ofMillis(openTimeOut); } /** @@ -350,7 +352,7 @@ public class LateralTCPCacheAttributes */ public void setSocketTimeOut( final int socketTimeOut ) { - this.socketTimeOut = socketTimeOut; + this.socketTimeOut = Duration.ofMillis(socketTimeOut); } /** diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPListener.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPListener.java index 53231d13..4017a860 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPListener.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPListener.java @@ -26,6 +26,7 @@ import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; +import java.time.Duration; import java.util.Iterator; import java.util.Map; import java.util.Set; @@ -55,7 +56,7 @@ public class LateralTCPListener<K, V> private static final Log log = Log.getLog( LateralTCPListener.class ); /** How long the server will block on an accept(). 0 is infinite. */ - private static final int acceptTimeOut = 1000; + private static final Duration acceptTimeOut = Duration.ofMillis(1000); /** Map of available instances, keyed by port */ private static final ConcurrentHashMap<String, ILateralCacheListener<?, ?>> instances = @@ -526,7 +527,7 @@ public class LateralTCPListener<K, V> // Check to see if we've been asked to exit, and exit while (!terminated.get()) { - final int activeKeys = selector.select(acceptTimeOut); + final int activeKeys = selector.select(acceptTimeOut.toMillis()); if (activeKeys == 0) { continue; diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPSender.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPSender.java index 0369a3c9..98c57f87 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPSender.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/LateralTCPSender.java @@ -22,6 +22,7 @@ package org.apache.commons.jcs4.auxiliary.lateral.socket.tcp; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.channels.AsynchronousSocketChannel; +import java.time.Duration; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -44,8 +45,8 @@ public class LateralTCPSender private static final Log log = Log.getLog( LateralTCPSender.class ); /** Config */ - private final int socketOpenTimeOut; - private final int socketSoTimeOut; + private final Duration socketOpenTimeOut; + private final Duration socketSoTimeOut; /** The serializer. */ private final IElementSerializer serializer; @@ -132,7 +133,7 @@ public class LateralTCPSender final InetSocketAddress hostAddress = new InetSocketAddress(host, port); final Future<Void> future = client.connect(hostAddress); - future.get(this.socketOpenTimeOut, TimeUnit.MILLISECONDS); + future.get(this.socketOpenTimeOut.toMillis(), TimeUnit.MILLISECONDS); } catch (final IOException | InterruptedException | ExecutionException | TimeoutException ioe) { @@ -165,7 +166,7 @@ public class LateralTCPSender lock.lock(); try { - serializer.serializeTo(led, client, socketSoTimeOut); + serializer.serializeTo(led, client, socketSoTimeOut.toMillis()); } finally { @@ -204,7 +205,7 @@ public class LateralTCPSender { // write object to listener send(led); - response = serializer.deSerializeFrom(client, socketSoTimeOut, null); + response = serializer.deSerializeFrom(client, socketSoTimeOut.toMillis(), null); } catch ( final IOException | ClassNotFoundException ioe ) { diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/behavior/ILateralTCPCacheAttributes.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/behavior/ILateralTCPCacheAttributes.java index be3b7f23..7abe1836 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/behavior/ILateralTCPCacheAttributes.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/lateral/socket/tcp/behavior/ILateralTCPCacheAttributes.java @@ -1,5 +1,7 @@ package org.apache.commons.jcs4.auxiliary.lateral.socket.tcp.behavior; +import java.time.Duration; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -38,12 +40,12 @@ public interface ILateralTCPCacheAttributes /** * @return the openTimeOut */ - int getOpenTimeOut(); + Duration getOpenTimeOut(); /** * @return the socketTimeOut */ - int getSocketTimeOut(); + Duration getSocketTimeOut(); /** * Gets the tcpListenerHost attribute of the ILateralCacheAttributes object diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/AbstractRemoteAuxiliaryCache.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/AbstractRemoteAuxiliaryCache.java index dce1e4c3..022f14be 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/AbstractRemoteAuxiliaryCache.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/AbstractRemoteAuxiliaryCache.java @@ -20,6 +20,7 @@ package org.apache.commons.jcs4.auxiliary.remote; */ import java.io.IOException; +import java.time.Duration; import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -95,11 +96,12 @@ public abstract class AbstractRemoteAuxiliaryCache<K, V> // use a pool if it is greater than 0 log.debug( "GetTimeoutMillis() = {0}", - () -> getAuxiliaryCacheAttributes().getGetTimeoutMillis() ); + () -> getAuxiliaryCacheAttributes().getGetTimeout() ); - if ( getAuxiliaryCacheAttributes().getGetTimeoutMillis() > 0 ) + if (!getAuxiliaryCacheAttributes().getGetTimeout().isNegative()) { - pool = ThreadPoolManager.getInstance().getExecutorService( getAuxiliaryCacheAttributes().getThreadPoolName() ); + pool = ThreadPoolManager.getInstance().getExecutorService( + getAuxiliaryCacheAttributes().getThreadPoolName()); log.debug( "Thread Pool = {0}", pool ); usePoolForGet = true; } @@ -287,7 +289,7 @@ public abstract class AbstractRemoteAuxiliaryCache<K, V> public ICacheElement<K, V> getUsingPool( final K key ) throws IOException { - final int timeout = getAuxiliaryCacheAttributes().getGetTimeoutMillis(); + final Duration timeout = getAuxiliaryCacheAttributes().getGetTimeout(); try { @@ -297,7 +299,7 @@ public abstract class AbstractRemoteAuxiliaryCache<K, V> final Future<ICacheElement<K, V>> future = pool.submit(command); // used timed get in order to timeout - final ICacheElement<K, V> ice = future.get(timeout, TimeUnit.MILLISECONDS); + final ICacheElement<K, V> ice = future.get(timeout.toMillis(), TimeUnit.MILLISECONDS); if ( ice == null ) { diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/CommonRemoteCacheAttributes.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/CommonRemoteCacheAttributes.java index 4a801fcf..773172a9 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/CommonRemoteCacheAttributes.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/CommonRemoteCacheAttributes.java @@ -1,5 +1,7 @@ package org.apache.commons.jcs4.auxiliary.remote; +import java.time.Duration; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -58,7 +60,7 @@ public abstract class CommonRemoteCacheAttributes private boolean localClusterConsistency; /** Read and connect timeout */ - private int rmiSocketFactoryTimeoutMillis = DEFAULT_RMI_SOCKET_FACTORY_TIMEOUT_MILLIS; + private Duration rmiSocketFactoryTimeout = DEFAULT_RMI_SOCKET_FACTORY_TIMEOUT; /** * Gets the clusterServers attribute of the RemoteCacheAttributes object. @@ -138,12 +140,12 @@ public abstract class CommonRemoteCacheAttributes } /** - * @return the rmiSocketFactoryTimeoutMillis. + * @return the rmiSocketFactoryTimeout. */ @Override - public int getRmiSocketFactoryTimeoutMillis() + public Duration getRmiSocketFactoryTimeout() { - return rmiSocketFactoryTimeoutMillis; + return rmiSocketFactoryTimeout; } /** @@ -252,7 +254,7 @@ public abstract class CommonRemoteCacheAttributes */ public void setRmiSocketFactoryTimeoutMillis( final int rmiSocketFactoryTimeoutMillis ) { - this.rmiSocketFactoryTimeoutMillis = rmiSocketFactoryTimeoutMillis; + this.rmiSocketFactoryTimeout = Duration.ofMillis(rmiSocketFactoryTimeoutMillis); } /** diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteCache.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteCache.java index 5fc6cdef..404f9295 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteCache.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteCache.java @@ -68,7 +68,8 @@ public class RemoteCache<K, V> super( cattr, remote, listener ); this.monitor = monitor; - RemoteUtils.configureGlobalCustomSocketFactory( getAuxiliaryCacheAttributes().getRmiSocketFactoryTimeoutMillis() ); + RemoteUtils.configureGlobalCustomSocketFactory( + getAuxiliaryCacheAttributes().getRmiSocketFactoryTimeout() ); } /** diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteCacheAttributes.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteCacheAttributes.java index a92caec7..5f50edc9 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteCacheAttributes.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteCacheAttributes.java @@ -1,5 +1,7 @@ package org.apache.commons.jcs4.auxiliary.remote; +import java.time.Duration; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -53,7 +55,7 @@ public class RemoteCacheAttributes private String threadPoolName = "remote_cache_client"; /** Must be greater than 0 for a pool to be used. */ - private int getTimeoutMillis = -1; + private Duration getTimeout = Duration.ofMillis(-1); /** * Can we receive from the server. You might have a 0 local store and keep everything on the @@ -103,12 +105,12 @@ public class RemoteCacheAttributes } /** - * @return getTimeoutMillis + * @return getTimeout */ @Override - public int getGetTimeoutMillis() + public Duration getGetTimeout() { - return getTimeoutMillis; + return getTimeout; } /** @@ -195,7 +197,7 @@ public class RemoteCacheAttributes */ public void setGetTimeoutMillis( final int millis ) { - getTimeoutMillis = millis; + getTimeout = Duration.ofMillis(millis); } /** @@ -245,7 +247,7 @@ public class RemoteCacheAttributes { final StringBuilder buf = new StringBuilder(super.toString()); buf.append( "\n receive = [" + isReceive() + "]" ); - buf.append( "\n getTimeoutMillis = [" + getGetTimeoutMillis() + "]" ); + buf.append( "\n getTimeoutMillis = [" + getGetTimeout() + "]" ); buf.append( "\n threadPoolName = [" + getThreadPoolName() + "]" ); buf.append( "\n localClusterConsistency = [" + isLocalClusterConsistency() + "]" ); buf.append( "\n zombieQueueMaxSize = [" + getZombieQueueMaxSize() + "]" ); diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteUtils.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteUtils.java index 53f89ea9..c4a43b3c 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteUtils.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/RemoteUtils.java @@ -32,6 +32,7 @@ import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.RMISocketFactory; +import java.time.Duration; import java.util.Properties; import org.apache.commons.jcs4.log.Log; @@ -51,17 +52,17 @@ public class RemoteUtils * configured for the specific object. * <p> * - * @param timeoutMillis + * @param timeout */ - public static void configureGlobalCustomSocketFactory(final int timeoutMillis) + public static void configureGlobalCustomSocketFactory(final Duration timeout) { try { // Don't set a socket factory if the setting is -1 - if (timeoutMillis > 0) + if (!timeout.isNegative()) { - log.info("RmiSocketFactoryTimeoutMillis [{0}]. " - + " Configuring a custom socket factory.", timeoutMillis); + log.info("RmiSocketFactoryTimeout [{0}]. " + + " Configuring a custom socket factory.", timeout); // use this socket factory to add a timeout. RMISocketFactory.setSocketFactory(new RMISocketFactory() @@ -78,9 +79,9 @@ public class RemoteUtils throws IOException { final Socket socket = new Socket(); - socket.setSoTimeout(timeoutMillis); + socket.setSoTimeout((int)timeout.toMillis()); socket.setSoLinger(false, 0); - socket.connect(new InetSocketAddress(host, port), timeoutMillis); + socket.connect(new InetSocketAddress(host, port), (int)timeout.toMillis()); return socket; } }); diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/behavior/ICommonRemoteCacheAttributes.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/behavior/ICommonRemoteCacheAttributes.java index 5e497fb6..db698ab7 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/behavior/ICommonRemoteCacheAttributes.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/behavior/ICommonRemoteCacheAttributes.java @@ -1,5 +1,7 @@ package org.apache.commons.jcs4.auxiliary.remote.behavior; +import java.time.Duration; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -30,7 +32,7 @@ public interface ICommonRemoteCacheAttributes extends AuxiliaryCacheAttributes { /** The default timeout for the custom RMI socket factory */ - int DEFAULT_RMI_SOCKET_FACTORY_TIMEOUT_MILLIS = 10000; + Duration DEFAULT_RMI_SOCKET_FACTORY_TIMEOUT = Duration.ofMillis(10000); /** * Gets the clusterServers attribute of the IRemoteCacheAttributes object @@ -87,9 +89,9 @@ public interface ICommonRemoteCacheAttributes * <p> * We have a default setting. The default rmi behavior should never be used. * - * @return int milliseconds + * @return timeout on the rmi socket factory */ - int getRmiSocketFactoryTimeoutMillis(); + Duration getRmiSocketFactoryTimeout(); /** * Should cluster updates be propagated to the locals diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/behavior/IRemoteCacheAttributes.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/behavior/IRemoteCacheAttributes.java index 5397384e..d10dda23 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/behavior/IRemoteCacheAttributes.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/behavior/IRemoteCacheAttributes.java @@ -1,5 +1,7 @@ package org.apache.commons.jcs4.auxiliary.remote.behavior; +import java.time.Duration; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -92,9 +94,9 @@ public interface IRemoteCacheAttributes * -1 and 0 mean no timeout, this is the default if the timeout is -1 or 0, no threadpool will * be used. * - * @return the time in millis + * @return the time */ - int getGetTimeoutMillis(); + Duration getGetTimeout(); /** * Gets the localPort attribute of the IRemoteCacheAttributes object diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/server/RemoteCacheServerAttributes.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/server/RemoteCacheServerAttributes.java index dc6a20bd..066886e0 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/server/RemoteCacheServerAttributes.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/server/RemoteCacheServerAttributes.java @@ -1,5 +1,7 @@ package org.apache.commons.jcs4.auxiliary.remote.server; +import java.time.Duration; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -48,7 +50,7 @@ public class RemoteCacheServerAttributes private boolean useRegistryKeepAlive = DEFAULT_USE_REGISTRY_KEEP_ALIVE; /** The delay between runs */ - private long registryKeepAliveDelayMillis = 15 * 1000; + private Duration registryKeepAliveDelay = Duration.ofSeconds(15); /** Default constructor for the RemoteCacheAttributes object */ public RemoteCacheServerAttributes() @@ -67,12 +69,12 @@ public class RemoteCacheServerAttributes } /** - * @return the registryKeepAliveDelayMillis + * @return the registryKeepAliveDelay */ @Override - public long getRegistryKeepAliveDelayMillis() + public Duration getRegistryKeepAliveDelay() { - return registryKeepAliveDelayMillis; + return registryKeepAliveDelay; } /** @@ -133,7 +135,7 @@ public class RemoteCacheServerAttributes */ public void setRegistryKeepAliveDelayMillis( final long registryKeepAliveDelayMillis ) { - this.registryKeepAliveDelayMillis = registryKeepAliveDelayMillis; + this.registryKeepAliveDelay = Duration.ofMillis(registryKeepAliveDelayMillis); } /** @@ -166,9 +168,9 @@ public class RemoteCacheServerAttributes buf.append( "\n servicePort = [" + getServicePort() + "]" ); buf.append( "\n allowClusterGet = [" + isAllowClusterGet() + "]" ); buf.append( "\n configFileName = [" + getConfigFileName() + "]" ); - buf.append( "\n rmiSocketFactoryTimeoutMillis = [" + getRmiSocketFactoryTimeoutMillis() + "]" ); + buf.append( "\n rmiSocketFactoryTimeout = [" + getRmiSocketFactoryTimeout() + "]" ); buf.append( "\n useRegistryKeepAlive = [" + isUseRegistryKeepAlive() + "]" ); - buf.append( "\n registryKeepAliveDelayMillis = [" + getRegistryKeepAliveDelayMillis() + "]" ); + buf.append( "\n registryKeepAliveDelayMillis = [" + getRegistryKeepAliveDelay() + "]" ); buf.append( "\n eventQueueType = [" + getEventQueueType() + "]" ); buf.append( "\n eventQueuePoolName = [" + getEventQueuePoolName() + "]" ); return buf.toString(); diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/server/RemoteCacheServerFactory.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/server/RemoteCacheServerFactory.java index fba068d0..67d44a96 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/server/RemoteCacheServerFactory.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/server/RemoteCacheServerFactory.java @@ -454,7 +454,7 @@ public class RemoteCacheServerFactory final RMISocketFactory customRMISocketFactory = configureObjectSpecificCustomFactory( props ); - RemoteUtils.configureGlobalCustomSocketFactory( rcsa.getRmiSocketFactoryTimeoutMillis() ); + RemoteUtils.configureGlobalCustomSocketFactory( rcsa.getRmiSocketFactoryTimeout() ); // CONFIGURE THE EVENT LOGGER final ICacheEventLogger cacheEventLogger = configureCacheEventLogger( props ); @@ -486,7 +486,7 @@ public class RemoteCacheServerFactory new DaemonThreadFactory("JCS-RemoteCacheServerFactory-")); } keepAliveDaemon.scheduleAtFixedRate(() -> keepAlive(host, port, cacheEventLogger), - 0, rcsa.getRegistryKeepAliveDelayMillis(), TimeUnit.MILLISECONDS); + 0, rcsa.getRegistryKeepAliveDelay().toMillis(), TimeUnit.MILLISECONDS); } } } diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/server/TimeoutConfigurableRMISocketFactory.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/server/TimeoutConfigurableRMISocketFactory.java index a2628bb0..10a28395 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/server/TimeoutConfigurableRMISocketFactory.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/server/TimeoutConfigurableRMISocketFactory.java @@ -25,6 +25,7 @@ import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.rmi.server.RMISocketFactory; +import java.time.Duration; /** * This can be injected into the remote cache server as follows: @@ -43,10 +44,10 @@ public class TimeoutConfigurableRMISocketFactory private static final long serialVersionUID = 1489909775271203334L; /** The socket read timeout */ - private int readTimeout = 5000; + private Duration readTimeout = Duration.ofMillis(5000); /** The socket open timeout */ - private int openTimeout = 5000; + private Duration openTimeout = Duration.ofMillis(5000); /** * @param port @@ -71,16 +72,16 @@ public class TimeoutConfigurableRMISocketFactory throws IOException { final Socket socket = new Socket(); - socket.setSoTimeout( readTimeout ); + socket.setSoTimeout( (int)readTimeout.toMillis() ); socket.setSoLinger( false, 0 ); - socket.connect( new InetSocketAddress( host, port ), openTimeout ); + socket.connect( new InetSocketAddress( host, port ), (int)openTimeout.toMillis() ); return socket; } /** * @return the openTimeout */ - public int getOpenTimeout() + public Duration getOpenTimeoutDuration() { return openTimeout; } @@ -88,7 +89,7 @@ public class TimeoutConfigurableRMISocketFactory /** * @return the readTimeout */ - public int getReadTimeout() + public Duration getReadTimeoutDuration() { return readTimeout; } @@ -98,7 +99,7 @@ public class TimeoutConfigurableRMISocketFactory */ public void setOpenTimeout( final int openTimeout ) { - this.openTimeout = openTimeout; + this.openTimeout = Duration.ofMillis(openTimeout); } /** @@ -106,6 +107,6 @@ public class TimeoutConfigurableRMISocketFactory */ public void setReadTimeout( final int readTimeout ) { - this.readTimeout = readTimeout; + this.readTimeout = Duration.ofMillis(readTimeout); } } diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/server/behavior/IRemoteCacheServerAttributes.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/server/behavior/IRemoteCacheServerAttributes.java index 57ecd95c..cae5c3e5 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/server/behavior/IRemoteCacheServerAttributes.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/remote/server/behavior/IRemoteCacheServerAttributes.java @@ -1,5 +1,7 @@ package org.apache.commons.jcs4.auxiliary.remote.server.behavior; +import java.time.Duration; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -36,9 +38,9 @@ public interface IRemoteCacheServerAttributes String getConfigFileName(); /** - * @return the registryKeepAliveDelayMillis + * @return the registryKeepAliveDelay */ - long getRegistryKeepAliveDelayMillis(); + Duration getRegistryKeepAliveDelay(); /** * Gets the localPort attribute of the IRemoteCacheAttributes object. diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/AbstractCacheEventQueue.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/AbstractCacheEventQueue.java index ad558b45..0641897b 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/AbstractCacheEventQueue.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/AbstractCacheEventQueue.java @@ -20,6 +20,7 @@ package org.apache.commons.jcs4.engine; */ import java.io.IOException; +import java.time.Duration; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; @@ -78,7 +79,7 @@ public abstract class AbstractCacheEventQueue<K, V> try { - Thread.sleep( waitBeforeRetry ); + Thread.sleep( waitBeforeRetry.toMillis() ); } catch ( final InterruptedException ie ) { @@ -189,13 +190,13 @@ public abstract class AbstractCacheEventQueue<K, V> private static final Log log = Log.getLog( AbstractCacheEventQueue.class ); /** Default */ - protected static final int DEFAULT_WAIT_TO_DIE_MILLIS = 10000; + private static final Duration DEFAULT_WAIT_TO_DIE = Duration.ofMillis(10000); /** * time to wait for an event before snuffing the background thread if the queue is empty. make * configurable later */ - private int waitToDieMillis = DEFAULT_WAIT_TO_DIE_MILLIS; + private Duration waitToDie = DEFAULT_WAIT_TO_DIE; /** * When the events are pulled off the queue, then tell the listener to handle the specific event @@ -213,7 +214,7 @@ public abstract class AbstractCacheEventQueue<K, V> private int maxFailure; /** In milliseconds */ - private int waitBeforeRetry; + private Duration waitBeforeRetry; /** * This means that the queue is functional. If we reached the max number of failures, the queue @@ -288,9 +289,9 @@ public abstract class AbstractCacheEventQueue<K, V> * * @return int */ - public int getWaitToDieMillis() + protected Duration getWaitToDie() { - return waitToDieMillis; + return waitToDie; } /** @@ -314,7 +315,7 @@ public abstract class AbstractCacheEventQueue<K, V> this.listenerId = listenerId; this.cacheName = cacheName; this.maxFailure = maxFailure <= 0 ? 3 : maxFailure; - this.waitBeforeRetry = waitBeforeRetry <= 0 ? 500 : waitBeforeRetry; + this.waitBeforeRetry = Duration.ofMillis(waitBeforeRetry <= 0 ? 500 : waitBeforeRetry); log.debug( "Constructed: {0}", this ); } @@ -351,9 +352,9 @@ public abstract class AbstractCacheEventQueue<K, V> * * @param wtdm the ms for the q to sit idle. */ - public void setWaitToDieMillis( final int wtdm ) + protected void setWaitToDie( final Duration wtdm ) { - waitToDieMillis = wtdm; + waitToDie = wtdm; } /** diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/CacheEventQueue.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/CacheEventQueue.java index d959751a..da738431 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/CacheEventQueue.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/CacheEventQueue.java @@ -23,8 +23,8 @@ import java.util.concurrent.ExecutorService; import org.apache.commons.jcs4.engine.behavior.ICacheListener; import org.apache.commons.jcs4.utils.threadpool.PoolConfiguration; -import org.apache.commons.jcs4.utils.threadpool.ThreadPoolManager; import org.apache.commons.jcs4.utils.threadpool.PoolConfiguration.WhenBlockedPolicy; +import org.apache.commons.jcs4.utils.threadpool.ThreadPoolManager; /** * An event queue is used to propagate ordered cache events to one and only one target listener. @@ -70,7 +70,8 @@ public class CacheEventQueue<K, V> { // create a default pool with one worker thread to mimic the SINGLE queue behavior return ThreadPoolManager.getInstance().createPool( - new PoolConfiguration(false, 0, 1, 1, getWaitToDieMillis(), WhenBlockedPolicy.RUN, 1), + new PoolConfiguration(false, 0, 1, 1, getWaitToDie().toMillisPart(), + WhenBlockedPolicy.RUN, 1), "CacheEventQueue.QProcessor-" + getCacheName()); } diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/PooledCacheEventQueue.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/PooledCacheEventQueue.java index 23e4297e..0b5716d3 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/PooledCacheEventQueue.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/PooledCacheEventQueue.java @@ -1,5 +1,7 @@ package org.apache.commons.jcs4.engine; +import java.time.Duration; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -84,21 +86,21 @@ public class PooledCacheEventQueue<K, V> /** * Destroy the queue * - * @param waitSeconds number of seconds to wait for the queue to drain + * @param wait time to wait for the queue to drain */ @Override - public synchronized void destroy(final int waitSeconds) + public synchronized void destroy(final Duration wait) { if ( isWorking() ) { setWorking(false); pool.shutdown(); - if (waitSeconds > 0) + if (wait.toSeconds() > 0) { try { - if (!pool.awaitTermination(waitSeconds, TimeUnit.SECONDS)) + if (!pool.awaitTermination(wait.toSeconds(), TimeUnit.SECONDS)) { log.info( "No longer waiting for event queue to finish: {0}", this::getStatistics); diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/behavior/ICacheEventQueue.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/behavior/ICacheEventQueue.java index 8f1e97b0..aa56e868 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/behavior/ICacheEventQueue.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/behavior/ICacheEventQueue.java @@ -20,6 +20,7 @@ package org.apache.commons.jcs4.engine.behavior; */ import java.io.IOException; +import java.time.Duration; import org.apache.commons.jcs4.engine.stats.behavior.IStats; @@ -82,15 +83,15 @@ public interface ICacheEventQueue<K, V> */ default void destroy() { - destroy(0); + destroy(Duration.ZERO); } /** * Destroy the queue * - * @param waitSeconds number of seconds to wait for the queue to drain + * @param wait time to wait for the queue to drain */ - void destroy(int waitSeconds); + void destroy(Duration wait); /** * Gets the listenerId attribute of the ICacheEventQueue object diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/behavior/IElementSerializer.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/behavior/IElementSerializer.java index 8c385666..20e89060 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/behavior/IElementSerializer.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/behavior/IElementSerializer.java @@ -64,7 +64,7 @@ public interface IElementSerializer * @throws ClassNotFoundException thrown if we don't know the object. * @since 3.1 */ - default <T> T deSerializeFrom(final AsynchronousByteChannel ic, final int readTimeoutMs, final ClassLoader loader) + default <T> T deSerializeFrom(final AsynchronousByteChannel ic, final long readTimeoutMs, final ClassLoader loader) throws IOException, ClassNotFoundException { final ByteBuffer bufferSize = ByteBuffer.allocate(4); @@ -202,7 +202,7 @@ public interface IElementSerializer * @throws IOException if serialization or writing fails * @since 3.1 */ - default <T> int serializeTo(final T obj, final AsynchronousByteChannel oc, final int writeTimeoutMs) + default <T> int serializeTo(final T obj, final AsynchronousByteChannel oc, final long writeTimeoutMs) throws IOException { final byte[] serialized = serialize(obj); diff --git a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/remote/server/RemoteCacheServerFactoryUnitTest.java b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/remote/server/RemoteCacheServerFactoryUnitTest.java index 47680772..e30f6ff4 100644 --- a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/remote/server/RemoteCacheServerFactoryUnitTest.java +++ b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/remote/server/RemoteCacheServerFactoryUnitTest.java @@ -20,6 +20,7 @@ package org.apache.commons.jcs4.auxiliary.remote.server; */ import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; import java.rmi.server.RMISocketFactory; @@ -55,8 +56,8 @@ class RemoteCacheServerFactoryUnitTest void testConfigureObjectSpecificCustomFactory_withProperty_TimeoutConfigurableRMIScoketFactory() { // SETUP - final int readTimeout = 1234; - final int openTimeout = 1234; + final long readTimeout = 1234; + final long openTimeout = 1234; final Properties props = new Properties(); props.put( IRemoteCacheConstants.CUSTOM_RMI_SOCKET_FACTORY_PROPERTY_PREFIX, TimeoutConfigurableRMISocketFactory.class.getName() ); props.put( IRemoteCacheConstants.CUSTOM_RMI_SOCKET_FACTORY_PROPERTY_PREFIX + ".readTimeout", String.valueOf( readTimeout ) ); @@ -67,9 +68,10 @@ class RemoteCacheServerFactoryUnitTest // VERIFY assertNotNull( result, "Should have a custom socket factory." ); - assertEquals( readTimeout, ( (TimeoutConfigurableRMISocketFactory) result ).getReadTimeout(), + assertInstanceOf(TimeoutConfigurableRMISocketFactory.class, result, "Should be a TimeoutConfigurableRMISocketFactory"); + assertEquals( readTimeout, ((TimeoutConfigurableRMISocketFactory) result).getReadTimeoutDuration().toMillis(), "Wrong readTimeout" ); - assertEquals( openTimeout, ( (TimeoutConfigurableRMISocketFactory) result ).getOpenTimeout(), + assertEquals( openTimeout, ((TimeoutConfigurableRMISocketFactory) result).getOpenTimeoutDuration().toMillis(), "Wrong readTimeout" ); } @@ -134,7 +136,7 @@ class RemoteCacheServerFactoryUnitTest final RemoteCacheServerAttributes result = RemoteCacheServerFactory.configureRemoteCacheServerAttributes( props ); // VERIFY - assertEquals( registryKeepAliveDelayMillis, result.getRegistryKeepAliveDelayMillis(), + assertEquals( registryKeepAliveDelayMillis, result.getRegistryKeepAliveDelay().toMillis(), "Wrong registryKeepAliveDelayMillis" ); } @@ -151,7 +153,7 @@ class RemoteCacheServerFactoryUnitTest final RemoteCacheServerAttributes result = RemoteCacheServerFactory.configureRemoteCacheServerAttributes( props ); // VERIFY - assertEquals( rmiSocketFactoryTimeoutMillis, result.getRmiSocketFactoryTimeoutMillis(), + assertEquals( rmiSocketFactoryTimeoutMillis, result.getRmiSocketFactoryTimeout().toMillis(), "Wrong rmiSocketFactoryTimeoutMillis" ); } @@ -166,8 +168,8 @@ class RemoteCacheServerFactoryUnitTest final RemoteCacheServerAttributes result = RemoteCacheServerFactory.configureRemoteCacheServerAttributes( props ); // VERIFY - assertEquals( ICommonRemoteCacheAttributes.DEFAULT_RMI_SOCKET_FACTORY_TIMEOUT_MILLIS, - result.getRmiSocketFactoryTimeoutMillis(), "Wrong timeout" ); + assertEquals( ICommonRemoteCacheAttributes.DEFAULT_RMI_SOCKET_FACTORY_TIMEOUT, + result.getRmiSocketFactoryTimeout(), "Wrong timeout" ); } /** Verify that we get the useRegistryKeepAlive value */ diff --git a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/EventQueueConcurrentLoadTest.java b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/EventQueueConcurrentLoadTest.java index 54ba58ef..ba4a470e 100644 --- a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/EventQueueConcurrentLoadTest.java +++ b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/EventQueueConcurrentLoadTest.java @@ -22,6 +22,7 @@ package org.apache.commons.jcs4.engine; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; +import java.time.Duration; import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.jcs4.engine.behavior.ICacheElement; @@ -99,7 +100,7 @@ class EventQueueConcurrentLoadTest { listen = new CacheListenerImpl<>(); queue = new CacheEventQueue<>( listen, 1L, "testCache1", maxFailure, waitBeforeRetry ); - queue.setWaitToDieMillis( idleTime ); + queue.setWaitToDie(Duration.ofMillis(idleTime)); } /**
