Author: kkolinko
Date: Sun Jun 24 08:56:20 2012
New Revision: 1353234

URL: http://svn.apache.org/viewvc?rev=1353234&view=rev
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=52996
In StandardThreadExecutor:
Add the ability to configure a job queue size.
Add a variant of execute method that allows to specify a timeout for how
long we want to try to add something to the queue.
Based on a patch by Ruediger Pluem.
It is backport of r723889 from trunk.

Modified:
    tomcat/tc6.0.x/trunk/STATUS.txt
    
tomcat/tc6.0.x/trunk/java/org/apache/catalina/core/StandardThreadExecutor.java
    tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml
    tomcat/tc6.0.x/trunk/webapps/docs/config/executor.xml

Modified: tomcat/tc6.0.x/trunk/STATUS.txt
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/STATUS.txt?rev=1353234&r1=1353233&r2=1353234&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/STATUS.txt (original)
+++ tomcat/tc6.0.x/trunk/STATUS.txt Sun Jun 24 08:56:20 2012
@@ -158,18 +158,6 @@ PATCHES PROPOSED TO BACKPORT:
   +1: markt, schultz, kkolinko
   -1:
 
-* Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=52996
-  In StandardThreadExecutor:
-  Add the ability to configure a job queue size.
-  Add a variant of execute method that allows to specify a timeout for how
-  long we want to try to add something to the queue.
-  Based on a patch by Ruediger Pluem.
-  It is backport of r723889 from trunk.
-  https://issues.apache.org/bugzilla/attachment.cgi?id=28907  (patch)
-  http://svn.apache.org/viewvc?view=revision&revision=1348105 (documentation 
update)
-  +1: kkolinko, fhanik, markt
-  -1:
-
 * Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=53047
   Allow database realms configured with an all roles mode that is 
authentication
   only to not have to define a role table

Modified: 
tomcat/tc6.0.x/trunk/java/org/apache/catalina/core/StandardThreadExecutor.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/catalina/core/StandardThreadExecutor.java?rev=1353234&r1=1353233&r2=1353234&view=diff
==============================================================================
--- 
tomcat/tc6.0.x/trunk/java/org/apache/catalina/core/StandardThreadExecutor.java 
(original)
+++ 
tomcat/tc6.0.x/trunk/java/org/apache/catalina/core/StandardThreadExecutor.java 
Sun Jun 24 08:56:20 2012
@@ -78,6 +78,11 @@ public class StandardThreadExecutor impl
      */
     protected AtomicInteger submittedTasksCount;
     
+    /**
+     * The maximum number of elements that can queue up before we reject them
+     */
+    protected int maxQueueSize = Integer.MAX_VALUE;
+
     private LifecycleSupport lifecycle = new LifecycleSupport(this);
     // ---------------------------------------------- Constructors
     public StandardThreadExecutor() {
@@ -89,7 +94,7 @@ public class StandardThreadExecutor impl
     // ---------------------------------------------- Public Methods
     public void start() throws LifecycleException {
         lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
-        TaskQueue taskqueue = new TaskQueue();
+        TaskQueue taskqueue = new TaskQueue(maxQueueSize);
         TaskThreadFactory tf = new TaskThreadFactory(namePrefix);
         lifecycle.fireLifecycleEvent(START_EVENT, null);
         executor = new ThreadPoolExecutor(getMinSpareThreads(), 
getMaxThreads(), maxIdleTime, TimeUnit.MILLISECONDS,taskqueue, tf) {
@@ -114,7 +119,27 @@ public class StandardThreadExecutor impl
         submittedTasksCount = null;
         lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null);
     }
-    
+
+    // This method is not used by Tomcat 6, but is available in later versions
+    public void execute(Runnable command, long timeout, TimeUnit unit) {
+        if ( executor != null ) {
+            submittedTasksCount.incrementAndGet();
+            try {
+                executor.execute(command);
+            } catch (RejectedExecutionException rx) {
+                //there could have been contention around the queue
+                try {
+                    if ( !( (TaskQueue) 
executor.getQueue()).force(command,timeout,unit) ) {
+                        submittedTasksCount.decrementAndGet();
+                        throw new RejectedExecutionException("Work queue 
full.");
+                    }
+                }catch (InterruptedException x) {
+                    throw new RejectedExecutionException("Interrupted.",x);
+                }
+            }
+        } else throw new IllegalStateException("StandardThreadPool not 
started.");
+    }
+
     public void execute(Runnable command) {
         if ( executor != null ) {
                submittedTasksCount.incrementAndGet();
@@ -124,7 +149,7 @@ public class StandardThreadExecutor impl
                 //there could have been contention around the queue
                 if ( !( (TaskQueue) executor.getQueue()).force(command) ) {
                        submittedTasksCount.decrementAndGet();
-                       throw new RejectedExecutionException();
+                       throw new RejectedExecutionException("Work queue 
full.");
                 }
             }
         } else throw new IllegalStateException("StandardThreadPool not 
started.");
@@ -196,6 +221,14 @@ public class StandardThreadExecutor impl
         this.name = name;
     }
     
+    public void setMaxQueueSize(int size) {
+        this.maxQueueSize = size;
+    }
+
+    public int getMaxQueueSize() {
+        return maxQueueSize;
+    }
+
     /**
      * Add a LifecycleEvent listener to this component.
      *
@@ -257,8 +290,8 @@ public class StandardThreadExecutor impl
             super();
         }
 
-        public TaskQueue(int initialCapacity) {
-            super(initialCapacity);
+        public TaskQueue(int capacity) {
+            super(capacity);
         }
 
         public TaskQueue(Collection<? extends Runnable> c) {
@@ -274,6 +307,11 @@ public class StandardThreadExecutor impl
             return super.offer(o); //forces the item onto the queue, to be 
used if the task is rejected
         }
 
+        public boolean force(Runnable o, long timeout, TimeUnit unit) throws 
InterruptedException {
+            if ( parent.isShutdown() ) throw new 
RejectedExecutionException("Executor not running, can't force a command into 
the queue");
+            return super.offer(o,timeout,unit); //forces the item onto the 
queue, to be used if the task is rejected
+        }
+
         public boolean offer(Runnable o) {
             //we can't do any checks
             if (parent==null) return super.offer(o);

Modified: tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml?rev=1353234&r1=1353233&r2=1353234&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml Sun Jun 24 08:56:20 2012
@@ -118,6 +118,14 @@
         <bug>52719</bug>: Fix a theoretical resource leak in the JAR validation
         that checks for non-permitted classes in web application JARs. (markt)
       </fix>
+      <add>
+        <bug>52996</bug>: In <code>StandardThreadExecutor</code>:
+        Add the ability to configure a job queue size
+        (<code>maxQueueSize</code> attribute).
+        Add a variant of execute method that allows to specify a timeout for
+        how long we want to try to add something to the queue.
+        Based on a patch by R&#252;diger Pl&#252;m. (kkolinko)
+      </add>
       <fix>
         <bug>53050</bug>: Fix handling of entropy value when initializing
         session id generator in session manager. Based on proposal by

Modified: tomcat/tc6.0.x/trunk/webapps/docs/config/executor.xml
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/webapps/docs/config/executor.xml?rev=1353234&r1=1353233&r2=1353234&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/webapps/docs/config/executor.xml (original)
+++ tomcat/tc6.0.x/trunk/webapps/docs/config/executor.xml Sun Jun 24 08:56:20 
2012
@@ -102,6 +102,10 @@
       <p>(int) The number of milliseconds before an idle thread shutsdown, 
unless the number of active threads are less
          or equal to minSpareThreads. Default value is <code>60000</code>(1 
minute)</p>
     </attribute>
+    <attribute name="maxQueueSize" required="false">
+      <p>(int) The maximum number of runnable tasks that can queue up awaiting
+        execution before we reject them. Default value is 
<code>Integer.MAX_VALUE</code></p>
+    </attribute>
   </attributes>
 
 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to