Author: tv
Date: Mon Jan 16 16:24:31 2017
New Revision: 1779059

URL: http://svn.apache.org/viewvc?rev=1779059&view=rev
Log:
Avoid duplicate code. Added test.

Modified:
    
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/PoolConfiguration.java
    
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManager.java
    
commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManagerUnitTest.java
    
commons/proper/jcs/trunk/commons-jcs-core/src/test/test-conf/thread_pool.properties

Modified: 
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/PoolConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/PoolConfiguration.java?rev=1779059&r1=1779058&r2=1779059&view=diff
==============================================================================
--- 
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/PoolConfiguration.java
 (original)
+++ 
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/PoolConfiguration.java
 Mon Jan 16 16:24:31 2017
@@ -27,23 +27,46 @@ package org.apache.commons.jcs.utils.thr
 public final class PoolConfiguration
     implements Cloneable
 {
+    /**
+     * DEFAULT SETTINGS
+     */
+    private static final boolean DEFAULT_USE_BOUNDARY = true;
+
+    /** Default queue size limit */
+    private static final int DEFAULT_BOUNDARY_SIZE = 2000;
+
+    /** Default max size */
+    private static final int DEFAULT_MAXIMUM_POOL_SIZE = 150;
+
+    /** Default min */
+    private static final int DEFAULT_MINIMUM_POOL_SIZE = 
Runtime.getRuntime().availableProcessors();
+
+    /** Default keep alive */
+    private static final int DEFAULT_KEEPALIVE_TIME = 1000 * 60 * 5;
+
+    /** Default when blocked */
+    private static final WhenBlockedPolicy DEFAULT_WHEN_BLOCKED_POLICY = 
WhenBlockedPolicy.RUN;
+
+    /** Default startup size */
+    private static final int DEFAULT_STARTUP_SIZE = DEFAULT_MINIMUM_POOL_SIZE;
+
     /** Should we bound the queue */
-    private boolean useBoundary = true;
+    private boolean useBoundary = DEFAULT_USE_BOUNDARY;
 
     /** If the queue is bounded, how big can it get */
-    private int boundarySize = 2000;
+    private int boundarySize = DEFAULT_BOUNDARY_SIZE;
 
     /** only has meaning if a boundary is used */
-    private int maximumPoolSize = 150;
+    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 = 4;
+    private int minimumPoolSize = DEFAULT_MINIMUM_POOL_SIZE;
 
     /** How long idle threads above the minimum should be kept alive. */
-    private int keepAliveTime = 1000 * 60 * 5;
+    private int keepAliveTime = DEFAULT_KEEPALIVE_TIME;
 
     public enum WhenBlockedPolicy {
         /** abort when queue is full and max threads is reached. */
@@ -63,10 +86,10 @@ public final class PoolConfiguration
     }
 
     /** should be ABORT, BLOCK, RUN, WAIT, DISCARDOLDEST, */
-    private WhenBlockedPolicy whenBlockedPolicy = WhenBlockedPolicy.RUN;
+    private WhenBlockedPolicy whenBlockedPolicy = DEFAULT_WHEN_BLOCKED_POLICY;
 
     /** The number of threads to create on startup */
-    private int startUpSize = 4;
+    private int startUpSize = DEFAULT_MINIMUM_POOL_SIZE;
 
     /**
      * @param useBoundary The useBoundary to set.
@@ -89,7 +112,9 @@ public final class PoolConfiguration
      */
     public PoolConfiguration()
     {
-        // nop
+        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 );
     }
 
     /**

Modified: 
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManager.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManager.java?rev=1779059&r1=1779058&r2=1779059&view=diff
==============================================================================
--- 
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManager.java
 (original)
+++ 
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManager.java
 Mon Jan 16 16:24:31 2017
@@ -27,7 +27,7 @@ import java.util.concurrent.LinkedBlocki
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 
-import 
org.apache.commons.jcs.utils.threadpool.PoolConfiguration.WhenBlockedPolicy;
+import org.apache.commons.jcs.utils.config.PropertySetter;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -71,31 +71,8 @@ public class ThreadPoolManager
     /** The logger */
     private static final Log log = LogFactory.getLog( ThreadPoolManager.class 
);
 
-    /**
-     * DEFAULT SETTINGS
-     */
-    private static final boolean useBoundary_DEFAULT = true;
-
-    /** Default queue size limit */
-    private static final int boundarySize_DEFAULT = 2000;
-
-    /** Default max size */
-    private static final int maximumPoolSize_DEFAULT = 150;
-
-    /** Default min */
-    private static final int minimumPoolSize_DEFAULT = 
Runtime.getRuntime().availableProcessors();
-
-    /** Default keep alive */
-    private static final int keepAliveTime_DEFAULT = 1000 * 60 * 5;
-
-    /** Default when blocked */
-    private static final WhenBlockedPolicy whenBlockedPolicy_DEFAULT = 
WhenBlockedPolicy.RUN;
-
-    /** Default startup size */
-    private static final int startUpSize_DEFAULT = minimumPoolSize_DEFAULT;
-
     /** The default config, created using property defaults if present, else 
those above. */
-    private static PoolConfiguration defaultConfig;
+    private PoolConfiguration defaultConfig;
 
     /** the root property name */
     private static final String PROP_NAME_ROOT = "thread_pool";
@@ -285,7 +262,7 @@ public class ThreadPoolManager
     /**
      * Initialize the ThreadPoolManager and create all the pools defined in 
the configuration.
      */
-    private static void configure()
+    private void configure()
     {
         if ( log.isDebugEnabled() )
         {
@@ -300,10 +277,7 @@ public class ThreadPoolManager
 
         // set intial default and then override if new
         // settings are available
-        defaultConfig = new PoolConfiguration( useBoundary_DEFAULT, 
boundarySize_DEFAULT, maximumPoolSize_DEFAULT,
-                                               minimumPoolSize_DEFAULT, 
keepAliveTime_DEFAULT,
-                                               whenBlockedPolicy_DEFAULT, 
startUpSize_DEFAULT );
-
+        defaultConfig = new PoolConfiguration();
         defaultConfig = loadConfig( DEFAULT_PROP_NAME_ROOT );
     }
 
@@ -313,75 +287,14 @@ public class ThreadPoolManager
      * @param root
      * @return PoolConfiguration
      */
-    private static PoolConfiguration loadConfig( String root )
+    private PoolConfiguration loadConfig( String root )
     {
         PoolConfiguration config = defaultConfig.clone();
+        PropertySetter.setProperties( config, props, root + "." );
 
-        try
-        {
-            config.setUseBoundary( Boolean.parseBoolean( props.getProperty( 
root + ".useBoundary", "false" ) ) );
-        }
-        catch ( NumberFormatException nfe )
-        {
-            log.error( "useBoundary not a boolean.", nfe );
-        }
-
-        // load default if they exist
-        try
-        {
-            config.setBoundarySize( Integer.parseInt( props.getProperty( root 
+ ".boundarySize", "2000" ) ) );
-        }
-        catch ( NumberFormatException nfe )
-        {
-            log.error( "boundarySize not a number.", nfe );
-        }
-
-        // maximum pool size
-        try
-        {
-            config.setMaximumPoolSize( Integer.parseInt( props.getProperty( 
root + ".maximumPoolSize", "150" ) ) );
-        }
-        catch ( NumberFormatException nfe )
-        {
-            log.error( "maximumPoolSize not a number.", nfe );
-        }
-
-        // minimum pool size
-        try
-        {
-            config.setMinimumPoolSize( Integer.parseInt( props.getProperty( 
root + ".minimumPoolSize", "4" ) ) );
-        }
-        catch ( NumberFormatException nfe )
-        {
-            log.error( "minimumPoolSize not a number.", nfe );
-        }
-
-        // keep alive
-        try
-        {
-            config.setKeepAliveTime( Integer.parseInt( props.getProperty( root 
+ ".keepAliveTime", "300000" ) ) );
-        }
-        catch ( NumberFormatException nfe )
-        {
-            log.error( "keepAliveTime not a number.", nfe );
-        }
-
-        // when blocked
-        config.setWhenBlockedPolicy( props.getProperty( root + 
".whenBlockedPolicy", "RUN" ) );
-
-        // startupsize
-        try
-        {
-            config.setStartUpSize( Integer.parseInt( props.getProperty( root + 
".startUpSize", "4" ) ) );
-        }
-        catch ( NumberFormatException nfe )
-        {
-            log.error( "startUpSize not a number.", nfe );
-        }
-
-        if ( log.isInfoEnabled() )
+        if ( log.isDebugEnabled() )
         {
-            log.info( root + " PoolConfiguration = " + config );
+            log.debug( root + " PoolConfiguration = " + config );
         }
 
         return config;

Modified: 
commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManagerUnitTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManagerUnitTest.java?rev=1779059&r1=1779058&r2=1779059&view=diff
==============================================================================
--- 
commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManagerUnitTest.java
 (original)
+++ 
commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManagerUnitTest.java
 Mon Jan 16 16:24:31 2017
@@ -21,12 +21,14 @@ package org.apache.commons.jcs.utils.thr
 
 import java.util.ArrayList;
 import java.util.Properties;
+import java.util.concurrent.RejectedExecutionHandler;
 import java.util.concurrent.ThreadPoolExecutor;
-
-import junit.framework.TestCase;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.commons.jcs.utils.props.PropertyLoader;
 
+import junit.framework.TestCase;
+
 /**
  * Verify that the manager can create pools as intended by the default and
  * specified file names.
@@ -63,6 +65,39 @@ public class ThreadPoolManagerUnitTest
     }
 
     /**
+     * Make sure it can load a certain configuration
+     */
+    public void testSpecialConfig()
+    {
+        Properties props = PropertyLoader.loadProperties( 
"thread_pool.properties" );
+        ThreadPoolManager.setProps( props );
+        ThreadPoolManager mgr = ThreadPoolManager.getInstance();
+        assertNotNull( mgr );
+
+        ThreadPoolExecutor pool = mgr.getPool( "aborttest" );
+        assertNotNull( pool );
+
+        int poolSize = pool.getCorePoolSize();
+        int expectedPoolSize = Integer.parseInt( props.getProperty( 
"thread_pool.aborttest.startUpSize" ) );
+        assertEquals( expectedPoolSize, poolSize);
+
+        int minPoolSize = pool.getPoolSize();
+        int expectedMinPoolSize = Integer.parseInt( props.getProperty( 
"thread_pool.aborttest.minimumPoolSize" ) );
+        assertEquals( expectedMinPoolSize, minPoolSize );
+
+        int maxPoolSize = pool.getMaximumPoolSize();
+        int expectedMaxPoolSize = Integer.parseInt( props.getProperty( 
"thread_pool.aborttest.maximumPoolSize" ) );
+        assertEquals( expectedMaxPoolSize, maxPoolSize );
+
+        long keepAliveTime = pool.getKeepAliveTime(TimeUnit.MILLISECONDS);
+        long expectedKeepAliveTime = Long.parseLong( props.getProperty( 
"thread_pool.aborttest.keepAliveTime" ) );
+        assertEquals( expectedKeepAliveTime, keepAliveTime );
+        
+        RejectedExecutionHandler whenBlockedPolicy = 
pool.getRejectedExecutionHandler();
+        assertTrue( whenBlockedPolicy instanceof 
ThreadPoolExecutor.AbortPolicy );
+    }
+    
+    /**
      * Try to get an undefined pool from an existing default file.
      */
     public void testDefaultConfigUndefinedPool()

Modified: 
commons/proper/jcs/trunk/commons-jcs-core/src/test/test-conf/thread_pool.properties
URL: 
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/test/test-conf/thread_pool.properties?rev=1779059&r1=1779058&r2=1779059&view=diff
==============================================================================
--- 
commons/proper/jcs/trunk/commons-jcs-core/src/test/test-conf/thread_pool.properties
 (original)
+++ 
commons/proper/jcs/trunk/commons-jcs-core/src/test/test-conf/thread_pool.properties
 Mon Jan 16 16:24:31 2017
@@ -44,13 +44,13 @@ thread_pool.maxtest.keepAliveTime=350002
 thread_pool.maxtest.whenBlockedPolicy=RUN
 thread_pool.maxtest.startUpSize=5
 
-# wait test thread pool config
-thread_pool.waittest.boundarySize=1
-thread_pool.waittest.maximumPoolSize=11
-thread_pool.waittest.minimumPoolSize=1
-thread_pool.waittest.keepAliveTime=1
-thread_pool.waittest.whenBlockedPolicy=WAIT
-thread_pool.waittest.startUpSize=1
+# abort test thread pool config
+thread_pool.aborttest.boundarySize=1
+thread_pool.aborttest.maximumPoolSize=11
+thread_pool.aborttest.minimumPoolSize=1
+thread_pool.aborttest.keepAliveTime=1
+thread_pool.aborttest.whenBlockedPolicy=ABORT
+thread_pool.aborttest.startUpSize=1
 
 # with boundary test thread pool config
 thread_pool.withbound.useBoundary=true
@@ -58,7 +58,7 @@ thread_pool.withbound.boundarySize=1000
 thread_pool.withbound.maximumPoolSize=11
 thread_pool.withbound.minimumPoolSize=1
 thread_pool.withbound.keepAliveTime=1
-thread_pool.withbound.whenBlockedPolicy=WAIT
+thread_pool.withbound.whenBlockedPolicy=ABORT
 thread_pool.withbound.startUpSize=1
 
 
@@ -68,5 +68,5 @@ thread_pool.nobound.boundarySize=1000
 thread_pool.nobound.maximumPoolSize=11
 thread_pool.nobound.minimumPoolSize=1
 thread_pool.nobound.keepAliveTime=1
-thread_pool.nobound.whenBlockedPolicy=WAIT
+thread_pool.nobound.whenBlockedPolicy=ABORT
 thread_pool.nobound.startUpSize=1


Reply via email to