From d0882858cdabbe8f0f40243899b79caa310f08e3 Mon Sep 17 00:00:00 2001 From: He-Pin Date: Sun, 19 Jan 2025 12:15:34 +0800 Subject: [PATCH] feat: Add LoadMetrics support for virtual thread executor. --- .../pekko/dispatch/VirtualThreadSupport.scala | 17 +++++++++++++++++ project/JdkOptions.scala | 2 ++ 2 files changed, 19 insertions(+) diff --git a/actor/src/main/scala/org/apache/pekko/dispatch/VirtualThreadSupport.scala b/actor/src/main/scala/org/apache/pekko/dispatch/VirtualThreadSupport.scala index 955cdc17102..430b3bfc451 100644 --- a/actor/src/main/scala/org/apache/pekko/dispatch/VirtualThreadSupport.scala +++ b/actor/src/main/scala/org/apache/pekko/dispatch/VirtualThreadSupport.scala @@ -73,6 +73,23 @@ private[dispatch] object VirtualThreadSupport { } } + /** + * Try to get the default scheduler of virtual thread. + */ + def getVirtualThreadDefaultScheduler: ForkJoinPool = + try { + require(isSupported, "Virtual thread is not supported.") + val clazz = Class.forName("java.lang.VirtualThread") + val fieldName = "DEFAULT_SCHEDULER" + val field = clazz.getDeclaredField(fieldName) + field.setAccessible(true) + field.get(null).asInstanceOf[ForkJoinPool] + } catch { + case NonFatal(e) => + // --add-opens java.base/java.lang=ALL-UNNAMED + throw new UnsupportedOperationException("Failed to create newThreadPerTaskExecutor.", e) + } + /** * Try to get the default scheduler of virtual thread. */ diff --git a/project/JdkOptions.scala b/project/JdkOptions.scala index 852a2bb82da..27fca13cbed 100644 --- a/project/JdkOptions.scala +++ b/project/JdkOptions.scala @@ -49,6 +49,8 @@ object JdkOptions extends AutoPlugin { lazy val versionSpecificJavaOptions = if (isJdk17orHigher) { + // for virtual threads + "--add-opens=java.base/java.lang=ALL-UNNAMED" :: // for aeron "--add-opens=java.base/sun.nio.ch=ALL-UNNAMED" :: // for LevelDB