From 4a9d8363515504eb58d510c05038bb4e989d572e Mon Sep 17 00:00:00 2001 From: markt Date: Fri, 21 Nov 2008 15:46:12 +0000 Subject: [PATCH] Make size of all threads pools dynamically configurable git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@719602 13f79535-47bb-0310-9956-ffa450edef68 --- .../catalina/core/StandardThreadExecutor.java | 9 +++++ java/org/apache/tomcat/util/net/AprEndpoint.java | 38 +++++++++++++++++++--- java/org/apache/tomcat/util/net/JIoEndpoint.java | 37 ++++++++++++++++++--- java/org/apache/tomcat/util/net/NioEndpoint.java | 38 +++++++++++++++++++--- 4 files changed, 110 insertions(+), 12 deletions(-) diff --git a/java/org/apache/catalina/core/StandardThreadExecutor.java b/java/org/apache/catalina/core/StandardThreadExecutor.java index bf6fa4ee3..3093fff55 100644 --- a/java/org/apache/catalina/core/StandardThreadExecutor.java +++ b/java/org/apache/catalina/core/StandardThreadExecutor.java @@ -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) { diff --git a/java/org/apache/tomcat/util/net/AprEndpoint.java b/java/org/apache/tomcat/util/net/AprEndpoint.java index 6b0cebdc0..1127f07c9 100644 --- a/java/org/apache/tomcat/util/net/AprEndpoint.java +++ b/java/org/apache/tomcat/util/net/AprEndpoint.java @@ -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; + } } diff --git a/java/org/apache/tomcat/util/net/JIoEndpoint.java b/java/org/apache/tomcat/util/net/JIoEndpoint.java index 15986ee1b..400d1b6a1 100644 --- a/java/org/apache/tomcat/util/net/JIoEndpoint.java +++ b/java/org/apache/tomcat/util/net/JIoEndpoint.java @@ -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; + } } } diff --git a/java/org/apache/tomcat/util/net/NioEndpoint.java b/java/org/apache/tomcat/util/net/NioEndpoint.java index 7ced0bafd..2ce7c680f 100644 --- a/java/org/apache/tomcat/util/net/NioEndpoint.java +++ b/java/org/apache/tomcat/util/net/NioEndpoint.java @@ -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; + } } -- 2.11.0