This is an automated email from the ASF dual-hosted git repository.

liujun pushed a commit to branch 3.3
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.3 by this push:
     new 4f32e0aba3 Support project loom as thread pool factory (#13111)
4f32e0aba3 is described below

commit 4f32e0aba363a413749fd90a34f3fc31a034be63
Author: Albumen Kevin <[email protected]>
AuthorDate: Tue Sep 26 11:03:23 2023 +0800

    Support project loom as thread pool factory (#13111)
    
    fixes #10768
---
 .artifacts                                         |  1 +
 dubbo-plugin/dubbo-plugin-loom/pom.xml             | 48 ++++++++++++++++
 .../threadpool/support/loom/VirtualThreadPool.java | 42 ++++++++++++++
 .../org.apache.dubbo.common.threadpool.ThreadPool  |  1 +
 .../support/loom/VirtualThreadPoolTest.java        | 66 ++++++++++++++++++++++
 dubbo-plugin/pom.xml                               | 12 ++++
 .../java/org/apache/dubbo/dependency/FileTest.java |  1 +
 7 files changed, 171 insertions(+)

diff --git a/.artifacts b/.artifacts
index 6565cbab5c..a5f400f416 100644
--- a/.artifacts
+++ b/.artifacts
@@ -140,3 +140,4 @@ dubbo-plugin-context
 dubbo-plugin-classloader-filter
 dubbo-plugin-proxy-bytebuddy
 dubbo-plugin-qos-trace
+dubbo-plugin-loom
diff --git a/dubbo-plugin/dubbo-plugin-loom/pom.xml 
b/dubbo-plugin/dubbo-plugin-loom/pom.xml
new file mode 100644
index 0000000000..ba865fe8a6
--- /dev/null
+++ b/dubbo-plugin/dubbo-plugin-loom/pom.xml
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+  -->
+<project xmlns="http://maven.apache.org/POM/4.0.0";
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+    <parent>
+        <groupId>org.apache.dubbo</groupId>
+        <artifactId>dubbo-plugin</artifactId>
+        <version>${revision}</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>com.aliyun.dubbo</groupId>
+    <artifactId>dubbo-plugin-loom</artifactId>
+
+    <properties>
+        <maven.compiler.source>21</maven.compiler.source>
+        <maven.compiler.target>21</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <skip_maven_deploy>false</skip_maven_deploy>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.dubbo</groupId>
+            <artifactId>dubbo-common</artifactId>
+            <version>${project.version}</version>
+            <optional>true</optional>
+        </dependency>
+    </dependencies>
+</project>
diff --git 
a/dubbo-plugin/dubbo-plugin-loom/src/main/java/org/apache/dubbo/common/threadpool/support/loom/VirtualThreadPool.java
 
b/dubbo-plugin/dubbo-plugin-loom/src/main/java/org/apache/dubbo/common/threadpool/support/loom/VirtualThreadPool.java
new file mode 100644
index 0000000000..c78c0b2b10
--- /dev/null
+++ 
b/dubbo-plugin/dubbo-plugin-loom/src/main/java/org/apache/dubbo/common/threadpool/support/loom/VirtualThreadPool.java
@@ -0,0 +1,42 @@
+/*
+ * 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.dubbo.common.threadpool.support.loom;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.threadpool.ThreadPool;
+
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+
+import static 
org.apache.dubbo.common.constants.CommonConstants.DEFAULT_THREAD_NAME;
+import static 
org.apache.dubbo.common.constants.CommonConstants.THREAD_NAME_KEY;
+
+/**
+ * Creates a thread pool that use virtual thread
+ *
+ * @see Executors#newVirtualThreadPerTaskExecutor()
+ */
+public class VirtualThreadPool implements ThreadPool {
+    @Override
+    public Executor getExecutor(URL url) {
+        String name = url.getParameter(THREAD_NAME_KEY, (String) 
url.getAttribute(THREAD_NAME_KEY, DEFAULT_THREAD_NAME));
+        return Executors.newThreadPerTaskExecutor(
+            Thread.ofVirtual()
+                .name(name, 1)
+                .factory());
+    }
+}
diff --git 
a/dubbo-plugin/dubbo-plugin-loom/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.threadpool.ThreadPool
 
b/dubbo-plugin/dubbo-plugin-loom/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.threadpool.ThreadPool
new file mode 100644
index 0000000000..e9f3348ad4
--- /dev/null
+++ 
b/dubbo-plugin/dubbo-plugin-loom/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.threadpool.ThreadPool
@@ -0,0 +1 @@
+virtual=org.apache.dubbo.common.threadpool.support.loom.VirtualThreadPool
diff --git 
a/dubbo-plugin/dubbo-plugin-loom/src/test/java/org/apache/dubbo/common/threadpool/support/loom/VirtualThreadPoolTest.java
 
b/dubbo-plugin/dubbo-plugin-loom/src/test/java/org/apache/dubbo/common/threadpool/support/loom/VirtualThreadPoolTest.java
new file mode 100644
index 0000000000..42653cd8bd
--- /dev/null
+++ 
b/dubbo-plugin/dubbo-plugin-loom/src/test/java/org/apache/dubbo/common/threadpool/support/loom/VirtualThreadPoolTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.dubbo.common.threadpool.support.loom;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.threadpool.ThreadPool;
+
+import org.hamcrest.Matchers;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledForJreRange;
+import org.junit.jupiter.api.condition.JRE;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Executor;
+
+import static org.apache.dubbo.common.constants.CommonConstants.QUEUES_KEY;
+import static 
org.apache.dubbo.common.constants.CommonConstants.THREAD_NAME_KEY;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.startsWith;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class VirtualThreadPoolTest {
+
+    @Test
+    @EnabledForJreRange(min = JRE.JAVA_21)
+    void getExecutor1() throws Exception {
+        URL url = URL.valueOf("dubbo://10.20.130.230:20880/context/path?" +
+            THREAD_NAME_KEY + "=demo");
+        ThreadPool threadPool = new VirtualThreadPool();
+        Executor executor = threadPool.getExecutor(url);
+
+        final CountDownLatch latch = new CountDownLatch(1);
+        executor.execute(() -> {
+            Thread thread = Thread.currentThread();
+            assertTrue(thread.isVirtual());
+            assertThat(thread.getName(), startsWith("demo"));
+            latch.countDown();
+        });
+
+        latch.await();
+        assertThat(latch.getCount(), is(0L));
+    }
+
+    @Test
+    @EnabledForJreRange(min = JRE.JAVA_21)
+    void getExecutor2() {
+        URL url = URL.valueOf("dubbo://10.20.130.230:20880/context/path?" + 
QUEUES_KEY + "=1");
+        ThreadPool threadPool = new VirtualThreadPool();
+        assertThat(threadPool.getExecutor(url).getClass().getName(), 
Matchers.is("java.util.concurrent.ThreadPerTaskExecutor"));
+    }
+}
diff --git a/dubbo-plugin/pom.xml b/dubbo-plugin/pom.xml
index a7cf59881f..e919be86f2 100644
--- a/dubbo-plugin/pom.xml
+++ b/dubbo-plugin/pom.xml
@@ -63,4 +63,16 @@
             <scope>test</scope>
         </dependency>
     </dependencies>
+
+    <profiles>
+        <profile>
+            <id>loom</id>
+            <activation>
+                <jdk>[21,)</jdk>
+            </activation>
+            <modules>
+                <module>dubbo-plugin-loom</module>
+            </modules>
+        </profile>
+    </profiles>
 </project>
diff --git 
a/dubbo-test/dubbo-test-modules/src/test/java/org/apache/dubbo/dependency/FileTest.java
 
b/dubbo-test/dubbo-test-modules/src/test/java/org/apache/dubbo/dependency/FileTest.java
index c150a71088..6059919675 100644
--- 
a/dubbo-test/dubbo-test-modules/src/test/java/org/apache/dubbo/dependency/FileTest.java
+++ 
b/dubbo-test/dubbo-test-modules/src/test/java/org/apache/dubbo/dependency/FileTest.java
@@ -51,6 +51,7 @@ class FileTest {
         ignoredModules.add(Pattern.compile("dubbo-core-spi"));
         ignoredModules.add(Pattern.compile("dubbo-demo.*"));
         ignoredModules.add(Pattern.compile("dubbo-annotation-processor"));
+        ignoredModules.add(Pattern.compile("dubbo-plugin-loom.*"));
 
         ignoredArtifacts.add(Pattern.compile("dubbo-demo.*"));
         ignoredArtifacts.add(Pattern.compile("dubbo-test.*"));

Reply via email to