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 c10ff697b2f6b9115f6947139724624283515e49 Author: Thomas Vandahl <[email protected]> AuthorDate: Tue Feb 17 20:11:42 2026 +0100 More records --- .../jcs4/utils/threadpool/PoolConfiguration.java | 246 ++++----------------- .../jcs4/utils/threadpool/ThreadPoolManager.java | 26 ++- 2 files changed, 54 insertions(+), 218 deletions(-) diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/threadpool/PoolConfiguration.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/threadpool/PoolConfiguration.java index 591ac082..d7c4d5b3 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/threadpool/PoolConfiguration.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/threadpool/PoolConfiguration.java @@ -22,10 +22,34 @@ package org.apache.commons.jcs4.utils.threadpool; /** * This object holds configuration data for a thread pool. */ -public final class PoolConfiguration - implements Cloneable +public record PoolConfiguration( + /** Should we bound the queue */ + boolean useBoundary, + + /** If the queue is bounded, how big can it get */ + int boundarySize, + + /** Only has meaning if a boundary is used */ + int maximumPoolSize, + + /** + * the exact number that will be used in a boundless queue. If the queue has a boundary, more + * will be created if the queue fills. + */ + int minimumPoolSize, + + /** How long idle threads above the minimum should be kept alive. */ + int keepAliveTime, + + /** Should be ABORT, BLOCK, RUN, WAIT, DISCARDOLDEST, */ + WhenBlockedPolicy whenBlockedPolicy, + + /** The number of threads to create on startup */ + int startUpSize +) implements Cloneable { - public enum WhenBlockedPolicy { + public enum WhenBlockedPolicy + { /** Abort when queue is full and max threads is reached. */ ABORT, @@ -62,211 +86,21 @@ public final class PoolConfiguration /** Default startup size */ private static final int DEFAULT_STARTUP_SIZE = DEFAULT_MINIMUM_POOL_SIZE; - /** Should we bound the queue */ - private boolean useBoundary = DEFAULT_USE_BOUNDARY; - - /** If the queue is bounded, how big can it get */ - private int boundarySize = DEFAULT_BOUNDARY_SIZE; - - /** Only has meaning if a boundary is used */ - private int maximumPoolSize = DEFAULT_MAXIMUM_POOL_SIZE; - - /** - * the exact number that will be used in a boundless queue. If the queue has a boundary, more - * will be created if the queue fills. - */ - private int minimumPoolSize = DEFAULT_MINIMUM_POOL_SIZE; - - /** How long idle threads above the minimum should be kept alive. */ - private int keepAliveTime = DEFAULT_KEEPALIVE_TIME; - - /** Should be ABORT, BLOCK, RUN, WAIT, DISCARDOLDEST, */ - private WhenBlockedPolicy whenBlockedPolicy = DEFAULT_WHEN_BLOCKED_POLICY; - - /** The number of threads to create on startup */ - private int startUpSize = DEFAULT_MINIMUM_POOL_SIZE; - /** * Default */ - public PoolConfiguration() - { - this( DEFAULT_USE_BOUNDARY, DEFAULT_BOUNDARY_SIZE, DEFAULT_MAXIMUM_POOL_SIZE, - DEFAULT_MINIMUM_POOL_SIZE, DEFAULT_KEEPALIVE_TIME, - DEFAULT_WHEN_BLOCKED_POLICY, DEFAULT_STARTUP_SIZE ); - } - - /** - * Constructs a completely configured instance. - * - * @param useBoundary - * @param boundarySize - * @param maximumPoolSize - * @param minimumPoolSize - * @param keepAliveTime - * @param whenBlockedPolicy - * @param startUpSize - */ - public PoolConfiguration( final boolean useBoundary, final int boundarySize, final int maximumPoolSize, final int minimumPoolSize, - final int keepAliveTime, final WhenBlockedPolicy whenBlockedPolicy, final int startUpSize ) - { - setUseBoundary( useBoundary ); - setBoundarySize( boundarySize ); - setMaximumPoolSize( maximumPoolSize ); - setMinimumPoolSize( minimumPoolSize ); - setKeepAliveTime( keepAliveTime ); - setWhenBlockedPolicy( whenBlockedPolicy ); - setStartUpSize( startUpSize ); - } - - /** - * Copies the instance variables to another instance. - * - * @return PoolConfiguration - */ - @Override - public PoolConfiguration clone() - { - return new PoolConfiguration( isUseBoundary(), boundarySize, maximumPoolSize, minimumPoolSize, keepAliveTime, - getWhenBlockedPolicy(), startUpSize ); - } - - /** - * @return the boundarySize. - */ - public int getBoundarySize() - { - return boundarySize; - } - - /** - * @return the keepAliveTime. - */ - public int getKeepAliveTime() - { - return keepAliveTime; - } - - /** - * @return the maximumPoolSize. - */ - public int getMaximumPoolSize() - { - return maximumPoolSize; - } - - /** - * @return the minimumPoolSize. - */ - public int getMinimumPoolSize() - { - return minimumPoolSize; - } - - /** - * @return the startUpSize. - */ - public int getStartUpSize() - { - return startUpSize; - } - - /** - * @return the whenBlockedPolicy. - */ - public WhenBlockedPolicy getWhenBlockedPolicy() - { - return whenBlockedPolicy; - } - - /** - * @return the useBoundary. - */ - public boolean isUseBoundary() - { - return useBoundary; - } - - /** - * @param boundarySize The boundarySize to set. - */ - public void setBoundarySize( final int boundarySize ) - { - this.boundarySize = boundarySize; - } - - /** - * @param keepAliveTime The keepAliveTime to set. - */ - public void setKeepAliveTime( final int keepAliveTime ) - { - this.keepAliveTime = keepAliveTime; - } + private static PoolConfiguration DEFAULT = new PoolConfiguration(DEFAULT_USE_BOUNDARY, + DEFAULT_BOUNDARY_SIZE, DEFAULT_MAXIMUM_POOL_SIZE, DEFAULT_MINIMUM_POOL_SIZE, + DEFAULT_KEEPALIVE_TIME, DEFAULT_WHEN_BLOCKED_POLICY, DEFAULT_STARTUP_SIZE); /** - * @param maximumPoolSize The maximumPoolSize to set. + * @return an object containing the default settings */ - public void setMaximumPoolSize( final int maximumPoolSize ) + public static PoolConfiguration defaults() { - this.maximumPoolSize = maximumPoolSize; + return DEFAULT; } - /** - * @param minimumPoolSize The minimumPoolSize to set. - */ - public void setMinimumPoolSize( final int minimumPoolSize ) - { - this.minimumPoolSize = minimumPoolSize; - } - - /** - * @param startUpSize The startUpSize to set. - */ - public void setStartUpSize( final int startUpSize ) - { - this.startUpSize = startUpSize; - } - - /** - * @param useBoundary The useBoundary to set. - */ - public void setUseBoundary( final boolean useBoundary ) - { - this.useBoundary = useBoundary; - } - - /** - * @param whenBlockedPolicy The whenBlockedPolicy to set. - */ - public void setWhenBlockedPolicy( final String whenBlockedPolicy ) - { - if ( whenBlockedPolicy != null ) - { - final WhenBlockedPolicy policy = WhenBlockedPolicy.valueOf(whenBlockedPolicy.trim().toUpperCase()); - setWhenBlockedPolicy(policy); - } - else - { - // the value is null, default to RUN - this.whenBlockedPolicy = WhenBlockedPolicy.RUN; - } - } - - /** - * @param whenBlockedPolicy The whenBlockedPolicy to set. - */ - public void setWhenBlockedPolicy( final WhenBlockedPolicy whenBlockedPolicy ) - { - if ( whenBlockedPolicy != null ) - { - this.whenBlockedPolicy = whenBlockedPolicy; - } - else - { - // the value is null, default to RUN - this.whenBlockedPolicy = WhenBlockedPolicy.RUN; - } - } /** * To string for debugging purposes. @@ -276,13 +110,13 @@ public final class PoolConfiguration public String toString() { final StringBuilder buf = new StringBuilder(); - buf.append( "useBoundary = [" + isUseBoundary() + "] " ); - buf.append( "boundarySize = [" + boundarySize + "] " ); - buf.append( "maximumPoolSize = [" + maximumPoolSize + "] " ); - buf.append( "minimumPoolSize = [" + minimumPoolSize + "] " ); - buf.append( "keepAliveTime = [" + keepAliveTime + "] " ); - buf.append( "whenBlockedPolicy = [" + getWhenBlockedPolicy() + "] " ); - buf.append( "startUpSize = [" + startUpSize + "]" ); + buf.append( "useBoundary = [" + useBoundary() + "] " ); + buf.append( "boundarySize = [" + boundarySize() + "] " ); + buf.append( "maximumPoolSize = [" + maximumPoolSize() + "] " ); + buf.append( "minimumPoolSize = [" + minimumPoolSize() + "] " ); + buf.append( "keepAliveTime = [" + keepAliveTime() + "] " ); + buf.append( "whenBlockedPolicy = [" + whenBlockedPolicy() + "] " ); + buf.append( "startUpSize = [" + startUpSize() + "]" ); return buf.toString(); } } diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/threadpool/ThreadPoolManager.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/threadpool/ThreadPoolManager.java index 4750296b..4365ec57 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/threadpool/ThreadPoolManager.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/threadpool/ThreadPoolManager.java @@ -33,7 +33,7 @@ import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import org.apache.commons.jcs4.log.Log; -import org.apache.commons.jcs4.utils.config.PropertySetter; +import org.apache.commons.jcs4.utils.config.ConfigurationBuilder; /** * This manages threadpools for an application @@ -142,8 +142,10 @@ public static void dispose() */ private static PoolConfiguration loadConfig( final String root, final PoolConfiguration defaultPoolConfiguration ) { - final PoolConfiguration config = defaultPoolConfiguration.clone(); - PropertySetter.setProperties( config, props, root + "." ); + final PoolConfiguration config = ConfigurationBuilder + .create(PoolConfiguration.class, defaultPoolConfiguration) + .fromProperties(props, root + ".") + .build(); log.debug( "{0} PoolConfiguration = {1}", root, config ); @@ -197,8 +199,8 @@ public static void dispose() } // set initial default and then override if new settings are available - defaultConfig = loadConfig( DEFAULT_PROP_NAME_ROOT, new PoolConfiguration() ); - defaultSchedulerConfig = loadConfig( DEFAULT_PROP_NAME_SCHEDULER_ROOT, new PoolConfiguration() ); + defaultConfig = loadConfig(DEFAULT_PROP_NAME_ROOT, PoolConfiguration.defaults()); + defaultSchedulerConfig = loadConfig(DEFAULT_PROP_NAME_SCHEDULER_ROOT, PoolConfiguration.defaults()); } /** @@ -224,10 +226,10 @@ public static void dispose() public ExecutorService createPool( final PoolConfiguration config, final String threadNamePrefix, final int threadPriority ) { BlockingQueue<Runnable> queue = null; - if ( config.isUseBoundary() ) + if ( config.useBoundary() ) { log.debug( "Creating a Bounded Buffer to use for the pool" ); - queue = new LinkedBlockingQueue<>(config.getBoundarySize()); + queue = new LinkedBlockingQueue<>(config.boundarySize()); } else { @@ -236,15 +238,15 @@ public static void dispose() } final ThreadPoolExecutor pool = new ThreadPoolExecutor( - config.getStartUpSize(), - config.getMaximumPoolSize(), - config.getKeepAliveTime(), + config.startUpSize(), + config.maximumPoolSize(), + config.keepAliveTime(), TimeUnit.MILLISECONDS, queue, new DaemonThreadFactory(threadNamePrefix, threadPriority)); // when blocked policy - switch (config.getWhenBlockedPolicy()) + switch (config.whenBlockedPolicy()) { case ABORT: pool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy()); @@ -280,7 +282,7 @@ public static void dispose() { return Executors.newScheduledThreadPool( - config.getMaximumPoolSize(), + config.maximumPoolSize(), new DaemonThreadFactory(threadNamePrefix, threadPriority)); }
