This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 9.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit f7129cf0eeaea499f513a03e34b4546ddec8d33f Author: PauloMigAlmeida <paulo.miguel.almeida.rode...@gmail.com> AuthorDate: Mon May 26 21:35:06 2025 +1200 Refactor TaskQueue to use RetryableQueue interface Additional clean-up by markt --- .../apache/tomcat/util/threads/RetryableQueue.java | 52 ++++++++++++++++++++++ java/org/apache/tomcat/util/threads/TaskQueue.java | 27 ++--------- .../tomcat/util/threads/ThreadPoolExecutor.java | 4 +- webapps/docs/changelog.xml | 11 +++++ 4 files changed, 68 insertions(+), 26 deletions(-) diff --git a/java/org/apache/tomcat/util/threads/RetryableQueue.java b/java/org/apache/tomcat/util/threads/RetryableQueue.java new file mode 100644 index 0000000000..f636b10dfa --- /dev/null +++ b/java/org/apache/tomcat/util/threads/RetryableQueue.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.tomcat.util.threads; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.TimeUnit; + +public interface RetryableQueue<T> extends BlockingQueue<T> { + + /** + * Used to add a task to the queue if the task has been rejected by the Executor. + * + * @param o The task to add to the queue + * + * @return {@code true} if the task was added to the queue, + * otherwise {@code false} + */ + boolean force(T o); + + /** + * Used to add a task to the queue if the task has been rejected by the Executor. + * + * @param o The task to add to the queue + * @param timeout The timeout to use when adding the task + * @param unit The units in which the timeout is expressed + * + * @return {@code true} if the task was added to the queue, + * otherwise {@code false} + * + * @throws InterruptedException If the call is interrupted before the + * timeout expires + * + * @deprecated Unused. Will be removed in Tomcat 10.1.x. + */ + @Deprecated + boolean force(Runnable o, long timeout, TimeUnit unit) throws InterruptedException; +} diff --git a/java/org/apache/tomcat/util/threads/TaskQueue.java b/java/org/apache/tomcat/util/threads/TaskQueue.java index 755658a7de..67fe06963b 100644 --- a/java/org/apache/tomcat/util/threads/TaskQueue.java +++ b/java/org/apache/tomcat/util/threads/TaskQueue.java @@ -30,7 +30,7 @@ import org.apache.tomcat.util.res.StringManager; * there are idle threads and you won't be able to force items onto the queue * itself. */ -public class TaskQueue extends LinkedBlockingQueue<Runnable> { +public class TaskQueue extends LinkedBlockingQueue<Runnable> implements RetryableQueue<Runnable> { private static final long serialVersionUID = 1L; protected static final StringManager sm = StringManager.getManager(TaskQueue.class); @@ -54,14 +54,7 @@ public class TaskQueue extends LinkedBlockingQueue<Runnable> { } - /** - * Used to add a task to the queue if the task has been rejected by the Executor. - * - * @param o The task to add to the queue - * - * @return {@code true} if the task was added to the queue, - * otherwise {@code false} - */ + @Override public boolean force(Runnable o) { if (parent == null || parent.isShutdown()) { throw new RejectedExecutionException(sm.getString("taskQueue.notRunning")); @@ -70,21 +63,7 @@ public class TaskQueue extends LinkedBlockingQueue<Runnable> { } - /** - * Used to add a task to the queue if the task has been rejected by the Executor. - * - * @param o The task to add to the queue - * @param timeout The timeout to use when adding the task - * @param unit The units in which the timeout is expressed - * - * @return {@code true} if the task was added to the queue, - * otherwise {@code false} - * - * @throws InterruptedException If the call is interrupted before the - * timeout expires - * - * @deprecated Unused. Will be removed in Tomcat 10.1.x. - */ + @Override @Deprecated public boolean force(Runnable o, long timeout, TimeUnit unit) throws InterruptedException { if (parent == null || parent.isShutdown()) { diff --git a/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java b/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java index 9c0a38590b..acfcb611c1 100644 --- a/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java +++ b/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java @@ -1399,12 +1399,12 @@ public class ThreadPoolExecutor extends AbstractExecutorService { try { executeInternal(command); } catch (RejectedExecutionException rx) { - if (getQueue() instanceof TaskQueue) { + if (getQueue() instanceof RetryableQueue) { // If the Executor is close to maximum pool size, concurrent // calls to execute() may result (due to Tomcat's use of // TaskQueue) in some tasks being rejected rather than queued. // If this happens, add them to the queue. - final TaskQueue queue = (TaskQueue) getQueue(); + final RetryableQueue<Runnable> queue = (RetryableQueue<Runnable>) getQueue(); try { if (!queue.force(command, timeout, unit)) { submittedCount.decrementAndGet(); diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 40cdf0f2b5..25ade009f2 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -118,6 +118,17 @@ </fix> </changelog> </subsection> + <subsection name="Coyote"> + <changelog> + <scode> + <pr>861</pr>: Refactor <code>TaskQueue</code> to use the new interface + <code>RetryableQueue</code> which enables better integration of custom + <code>Executor</code>s which provide their own + <code>BlockingQueue</code> implementation. Pull request provided by + Paulo Almeida. (markt) + </scode> + </changelog> + </subsection> <subsection name="Jasper"> <changelog> <fix> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org