Make size of all threads pools dynamically configurable
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 21 Nov 2008 15:46:12 +0000 (15:46 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Fri, 21 Nov 2008 15:46:12 +0000 (15:46 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@719602 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/core/StandardThreadExecutor.java
java/org/apache/tomcat/util/net/AprEndpoint.java
java/org/apache/tomcat/util/net/JIoEndpoint.java
java/org/apache/tomcat/util/net/NioEndpoint.java

index bf6fa4e..3093fff 100644 (file)
@@ -130,14 +130,23 @@ public class StandardThreadExecutor implements Executor {
 
     public void setMaxIdleTime(int maxIdleTime) {
         this.maxIdleTime = maxIdleTime;
+        if (executor != null) {
+            executor.setKeepAliveTime(maxIdleTime, TimeUnit.MILLISECONDS);
+        }
     }
 
     public void setMaxThreads(int maxThreads) {
         this.maxThreads = maxThreads;
+        if (executor != null) {
+            executor.setMaximumPoolSize(maxThreads);
+        }
     }
 
     public void setMinSpareThreads(int minSpareThreads) {
         this.minSpareThreads = minSpareThreads;
+        if (executor != null) {
+            executor.setCorePoolSize(minSpareThreads);
+        }
     }
 
     public void setName(String name) {
index 6b0cebd..1127f07 100644 (file)
@@ -36,6 +36,7 @@ import org.apache.tomcat.jni.SSLContext;
 import org.apache.tomcat.jni.SSLSocket;
 import org.apache.tomcat.jni.Socket;
 import org.apache.tomcat.jni.Status;
+import org.apache.tomcat.util.net.JIoEndpoint.Worker;
 import org.apache.tomcat.util.res.StringManager;
 
 /**
@@ -179,7 +180,14 @@ public class AprEndpoint {
      * Maximum amount of worker threads.
      */
     protected int maxThreads = 200;
-    public void setMaxThreads(int maxThreads) { this.maxThreads = maxThreads; }
+    public void setMaxThreads(int maxThreads) {
+        this.maxThreads = maxThreads;
+        if (running) {
+            synchronized(workers) {
+                workers.resize(maxThreads);
+            }
+        }
+    }
     public int getMaxThreads() { return maxThreads; }
 
 
@@ -1883,12 +1891,18 @@ public class AprEndpoint {
         }
         
         /** 
-         * Put the object into the queue.
+         * Put the object into the queue. If the queue is full (for example if
+         * the queue has been reduced in size) the object will be dropped.
          * 
-         * @param   object      the object to be appended to the queue (first element). 
+         * @param   object  the object to be appended to the queue (first
+         *                  element).
          */
         public void push(Worker worker) {
-            workers[end++] = worker;
+            if (end < workers.length) {
+                workers[end++] = worker;
+            } else {
+                curThreads--;
+            }
         }
         
         /**
@@ -1923,6 +1937,22 @@ public class AprEndpoint {
         public int size() {
             return (end);
         }
+        
+        /**
+         * Resize the queue. If there are too many objects in the queue for the
+         * new size, drop the excess.
+         * 
+         * @param newSize
+         */
+        public void resize(int newSize) {
+            Worker[] newWorkers = new Worker[newSize];
+            int len = workers.length;
+            if (newSize < len) {
+                len = newSize;
+            }
+            System.arraycopy(workers, 0, newWorkers, 0, len);
+            workers = newWorkers;
+        }
     }
 
 
index 15986ee..400d1b6 100644 (file)
@@ -174,7 +174,14 @@ public class JIoEndpoint {
      * Maximum amount of worker threads.
      */
     protected int maxThreads = 200;
-    public void setMaxThreads(int maxThreads) { this.maxThreads = maxThreads; }
+    public void setMaxThreads(int maxThreads) {
+        this.maxThreads = maxThreads;
+        if (running) {
+            synchronized(workers) {
+                workers.resize(maxThreads);
+            }
+        }
+    }
     public int getMaxThreads() { return maxThreads; }
 
 
@@ -777,12 +784,18 @@ public class JIoEndpoint {
         }
         
         /** 
-         * Put the object into the queue.
+         * Put the object into the queue. If the queue is full (for example if
+         * the queue has been reduced in size) the object will be dropped.
          * 
-         * @param   object      the object to be appended to the queue (first element). 
+         * @param   object  the object to be appended to the queue (first
+         *                  element).
          */
         public void push(Worker worker) {
-            workers[end++] = worker;
+            if (end < workers.length) {
+                workers[end++] = worker;
+            } else {
+                curThreads--;
+            }
         }
         
         /**
@@ -817,6 +830,22 @@ public class JIoEndpoint {
         public int size() {
             return (end);
         }
+        
+        /**
+         * Resize the queue. If there are too many objects in the queue for the
+         * new size, drop the excess.
+         * 
+         * @param newSize
+         */
+        public void resize(int newSize) {
+            Worker[] newWorkers = new Worker[newSize];
+            int len = workers.length;
+            if (newSize < len) {
+                len = newSize;
+            }
+            System.arraycopy(workers, 0, newWorkers, 0, len);
+            workers = newWorkers;
+        }
     }
 
 }
index 7ced0ba..2ce7c68 100644 (file)
@@ -52,6 +52,7 @@ import javax.net.ssl.TrustManagerFactory;
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.util.IntrospectionUtils;
+import org.apache.tomcat.util.net.JIoEndpoint.Worker;
 import org.apache.tomcat.util.net.SecureNioChannel.ApplicationBufferHandler;
 import org.apache.tomcat.util.res.StringManager;
 
@@ -350,7 +351,14 @@ public class NioEndpoint {
      * Maximum amount of worker threads.
      */
     protected int maxThreads = 200;
-    public void setMaxThreads(int maxThreads) { this.maxThreads = maxThreads; }
+    public void setMaxThreads(int maxThreads) {
+        this.maxThreads = maxThreads;
+        if (running) {
+            synchronized(workers) {
+                workers.resize(maxThreads);
+            }
+        }
+    }
     public int getMaxThreads() { return maxThreads; }
 
 
@@ -2026,12 +2034,18 @@ public class NioEndpoint {
         }
 
         /** 
-         * Put the object into the queue.
+         * Put the object into the queue. If the queue is full (for example if
+         * the queue has been reduced in size) the object will be dropped.
          * 
-         * @param   object      the object to be appended to the queue (first element). 
+         * @param   object  the object to be appended to the queue (first
+         *                  element).
          */
         public void push(Worker worker) {
-            workers[end++] = worker;
+            if (end < workers.length) {
+                workers[end++] = worker;
+            } else {
+                curThreads--;
+            }
         }
 
         /**
@@ -2066,6 +2080,22 @@ public class NioEndpoint {
         public int size() {
             return (end);
         }
+        
+        /**
+         * Resize the queue. If there are too many objects in the queue for the
+         * new size, drop the excess.
+         * 
+         * @param newSize
+         */
+        public void resize(int newSize) {
+            Worker[] newWorkers = new Worker[newSize];
+            int len = workers.length;
+            if (newSize < len) {
+                len = newSize;
+            }
+            System.arraycopy(workers, 0, newWorkers, 0, len);
+            workers = newWorkers;
+        }
     }