This is an automated email from the ASF dual-hosted git repository.
markt pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/10.1.x by this push:
new 4416934d4b Add useVirtualThreads attribute to endpoints.
4416934d4b is described below
commit 4416934d4b34b27ecb0761c460f12c8b26573db5
Author: Mark Thomas <[email protected]>
AuthorDate: Fri May 12 17:30:27 2023 +0100
Add useVirtualThreads attribute to endpoints.
Refactor VirtualThreadExecutor so it can be used by the Endpoint
---
.../apache/catalina/core/LocalStrings.properties | 5 +--
...tor.java => StandardVirtualThreadExecutor.java} | 21 +++++++-----
.../apache/tomcat/util/net/AbstractEndpoint.java | 23 ++++++++++---
.../tomcat/util/threads/VirtualThreadExecutor.java | 40 ++++++++++++++++++++++
webapps/docs/changelog.xml | 4 +--
webapps/docs/config/ajp.xml | 7 ++++
webapps/docs/config/executor.xml | 4 +--
webapps/docs/config/http.xml | 7 ++++
8 files changed, 92 insertions(+), 19 deletions(-)
diff --git a/java/org/apache/catalina/core/LocalStrings.properties
b/java/org/apache/catalina/core/LocalStrings.properties
index 07b88b476e..afc4812915 100644
--- a/java/org/apache/catalina/core/LocalStrings.properties
+++ b/java/org/apache/catalina/core/LocalStrings.properties
@@ -281,6 +281,9 @@ standardService.stop.name=Stopping service [{0}]
standardThreadExecutor.notStarted=The executor has not been started
+standardVirtualThreadExecutor.notStarted=The executor has not been started
+standardVirtualThreadExecutor.noVirtualThreads=Virtual threads require a
minimum Java version of Java 21
+
standardWrapper.allocate=Error allocating a servlet instance
standardWrapper.allocateException=Allocate exception for servlet [{0}]
standardWrapper.deallocateException=Deallocate exception for servlet [{0}]
@@ -304,5 +307,3 @@ standardWrapper.waiting=Waiting for [{0}] instance(s) to be
deallocated for Serv
threadLocalLeakPreventionListener.containerEvent.error=Exception processing
container event [{0}]
threadLocalLeakPreventionListener.lifecycleEvent.error=Exception processing
lifecycle event [{0}]
-
-virtualThreadExecutor.noVirtualThreads=Virtual threads require a minimum Java
version of Java 21
\ No newline at end of file
diff --git a/java/org/apache/catalina/core/VirtualThreadExecutor.java
b/java/org/apache/catalina/core/StandardVirtualThreadExecutor.java
similarity index 78%
rename from java/org/apache/catalina/core/VirtualThreadExecutor.java
rename to java/org/apache/catalina/core/StandardVirtualThreadExecutor.java
index dd8739a179..23663c4e4e 100644
--- a/java/org/apache/catalina/core/VirtualThreadExecutor.java
+++ b/java/org/apache/catalina/core/StandardVirtualThreadExecutor.java
@@ -22,18 +22,17 @@ import org.apache.catalina.LifecycleState;
import org.apache.catalina.util.LifecycleMBeanBase;
import org.apache.tomcat.util.compat.JreCompat;
import org.apache.tomcat.util.res.StringManager;
+import org.apache.tomcat.util.threads.VirtualThreadExecutor;
/**
* An executor that uses a new virtual thread for each task.
*/
-public class VirtualThreadExecutor extends LifecycleMBeanBase implements
Executor {
+public class StandardVirtualThreadExecutor extends LifecycleMBeanBase
implements Executor {
- private static final StringManager sm =
StringManager.getManager(VirtualThreadExecutor.class);
-
- private final JreCompat jreCompat = JreCompat.getInstance();
+ private static final StringManager sm =
StringManager.getManager(StandardVirtualThreadExecutor.class);
private String name;
- private Object threadBuilder;
+ private java.util.concurrent.Executor executor;
private String namePrefix;
public void setName(String name) {
@@ -55,7 +54,11 @@ public class VirtualThreadExecutor extends
LifecycleMBeanBase implements Executo
@Override
public void execute(Runnable command) {
- JreCompat.getInstance().threadBuilderStart(threadBuilder, command);
+ if (executor == null) {
+ throw new
IllegalStateException(sm.getString("standardVirtualThreadExecutor.notStarted"));
+ } else {
+ executor.execute(command);
+ }
}
@@ -63,19 +66,19 @@ public class VirtualThreadExecutor extends
LifecycleMBeanBase implements Executo
protected void initInternal() throws LifecycleException {
super.initInternal();
if (!JreCompat.isJre21Available()) {
- throw new
LifecycleException(sm.getString("virtualThreadExecutor.noVirtualThreads"));
+ throw new
LifecycleException(sm.getString("standardVirtualThreadExecutor.noVirtualThreads"));
}
}
@Override
protected void startInternal() throws LifecycleException {
- threadBuilder = jreCompat.createVirtualThreadBuilder(getNamePrefix());
+ executor = new VirtualThreadExecutor(getNamePrefix());
setState(LifecycleState.STARTING);
}
@Override
protected void stopInternal() throws LifecycleException {
- threadBuilder = null;
+ executor = null;
setState(LifecycleState.STOPPING);
}
diff --git a/java/org/apache/tomcat/util/net/AbstractEndpoint.java
b/java/org/apache/tomcat/util/net/AbstractEndpoint.java
index c0f6ee9c44..91f539f687 100644
--- a/java/org/apache/tomcat/util/net/AbstractEndpoint.java
+++ b/java/org/apache/tomcat/util/net/AbstractEndpoint.java
@@ -58,6 +58,7 @@ import org.apache.tomcat.util.threads.ResizableExecutor;
import org.apache.tomcat.util.threads.TaskQueue;
import org.apache.tomcat.util.threads.TaskThreadFactory;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
+import org.apache.tomcat.util.threads.VirtualThreadExecutor;
/**
* @param <S> The type used by the socket wrapper associated with this
endpoint.
@@ -585,6 +586,15 @@ public abstract class AbstractEndpoint<S,U> {
public Executor getExecutor() { return executor; }
+ private boolean useVirtualThreads = false;
+ public void setUseVirtualThreads(boolean useVirtualThreads) {
+ this.useVirtualThreads = useVirtualThreads;
+ }
+ public boolean getVirtualThreads() {
+ return useVirtualThreads;
+ }
+
+
/**
* External Executor based thread pool for utility tasks.
*/
@@ -1024,12 +1034,17 @@ public abstract class AbstractEndpoint<S,U> {
public void createExecutor() {
internalExecutor = true;
- TaskQueue taskqueue = new TaskQueue();
- TaskThreadFactory tf = new TaskThreadFactory(getName() + "-exec-",
daemon, getThreadPriority());
- executor = new ThreadPoolExecutor(getMinSpareThreads(),
getMaxThreads(), 60, TimeUnit.SECONDS,taskqueue, tf);
- taskqueue.setParent( (ThreadPoolExecutor) executor);
+ if (useVirtualThreads) {
+ executor = new VirtualThreadExecutor(getName() + "-exec-");
+ } else {
+ TaskQueue taskqueue = new TaskQueue();
+ TaskThreadFactory tf = new TaskThreadFactory(getName() + "-exec-",
daemon, getThreadPriority());
+ executor = new ThreadPoolExecutor(getMinSpareThreads(),
getMaxThreads(), 60, TimeUnit.SECONDS,taskqueue, tf);
+ taskqueue.setParent( (ThreadPoolExecutor) executor);
+ }
}
+
public void shutdownExecutor() {
Executor executor = this.executor;
if (executor != null && internalExecutor) {
diff --git a/java/org/apache/tomcat/util/threads/VirtualThreadExecutor.java
b/java/org/apache/tomcat/util/threads/VirtualThreadExecutor.java
new file mode 100644
index 0000000000..93a00f8d42
--- /dev/null
+++ b/java/org/apache/tomcat/util/threads/VirtualThreadExecutor.java
@@ -0,0 +1,40 @@
+/*
+ * 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.Executor;
+
+import org.apache.tomcat.util.compat.JreCompat;
+
+/**
+ * An executor that uses a new virtual thread for each task.
+ */
+public class VirtualThreadExecutor implements Executor {
+
+ private final JreCompat jreCompat = JreCompat.getInstance();
+
+ private Object threadBuilder;
+
+ public VirtualThreadExecutor(String namePrefix) {
+ threadBuilder = jreCompat.createVirtualThreadBuilder(namePrefix);
+ }
+
+ @Override
+ public void execute(Runnable command) {
+ jreCompat.threadBuilderStart(threadBuilder, command);
+ }
+}
\ No newline at end of file
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 75c0ae975d..f38a017f6d 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -113,8 +113,8 @@
<code>start()</code>/<code>stop()</code> methods. (markt)
</scode>
<add>
- Add <code>org.apache.catalina.core.VirtualThreadExecutor</code>, a
- virtual thread based executor that may be used with one or more
+ Add
<code>org.apache.catalina.core.StandardVirtualThreadExecutor</code>,
+ a virtual thread based executor that may be used with one or more
Connectors to process requests received by those Connectors using
virtual threads. This Executor requires a minimum Java version of Java
21. (markt)
diff --git a/webapps/docs/config/ajp.xml b/webapps/docs/config/ajp.xml
index 3b614394c1..b8984720f3 100644
--- a/webapps/docs/config/ajp.xml
+++ b/webapps/docs/config/ajp.xml
@@ -576,6 +576,13 @@
<code>false</code>.</p>
</attribute>
+ <attribute name="useVirtualThreads" required="false">
+ <p>(bool) Use this attribute to enable or disable usage of virtual
threads
+ with the internal executor. If an executor is associated with this
+ connector, this attribute is ignored. The default value is
+ <code>false</code>.</p>
+ </attribute>
+
</attributes>
</subsection>
diff --git a/webapps/docs/config/executor.xml b/webapps/docs/config/executor.xml
index 59cf023605..b5e48099f5 100644
--- a/webapps/docs/config/executor.xml
+++ b/webapps/docs/config/executor.xml
@@ -128,8 +128,8 @@
<p>This implemtenation uses a new virtual thread to execute each task
assigned to the Executor. This Executor requires
a minimum Java version of Java 21.</p>
- <p>The <code>className</code> attribute must be
<code>org.apache.catalina.core.VirtualThreadExecutor</code> to use
- this implementation.</p>
+ <p>The <code>className</code> attribute must be
<code>org.apache.catalina.core.StandardVirtualThreadExecutor</code> to
+ use this implementation.</p>
<p>The virtual thread implementation supports the follow attributes:</p>
diff --git a/webapps/docs/config/http.xml b/webapps/docs/config/http.xml
index 6dcc4bb9f4..6d7db9712a 100644
--- a/webapps/docs/config/http.xml
+++ b/webapps/docs/config/http.xml
@@ -727,6 +727,13 @@
Internet-Draft</a>. The default value is <code>true</code>.</p>
</attribute>
+ <attribute name="useVirtualThreads" required="false">
+ <p>(bool) Use this attribute to enable or disable usage of virtual
threads
+ with the internal executor. If an executor is associated with this
+ connector, this attribute is ignored. The default value is
+ <code>false</code>.</p>
+ </attribute>
+
</attributes>
</subsection>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]