Initial stab at incorporating some Loom features into Helidon in a way that will work under JDK 11 or 16-loom-ea (#2417)

Signed-off-by: Laird Nelson <laird.nelson@oracle.com>
This commit is contained in:
Laird Nelson
2020-10-29 10:52:39 -07:00
committed by GitHub
parent d59a0b8e19
commit c701e38737
3 changed files with 89 additions and 5 deletions

View File

@@ -27,6 +27,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
@@ -108,10 +109,34 @@ public class ServerCdiExtension implements Extension {
// make sure all configuration is in place
if (null == jaxRsExecutorService) {
jaxRsExecutorService = ServerThreadPoolSupplier.builder()
Config serverConfig = config.get("server");
final java.lang.reflect.Method m;
if (serverConfig.get("virtual-threads").asBoolean().orElse(false)) {
java.lang.reflect.Method temp = null;
try {
temp = Executors.class.getDeclaredMethod("newVirtualThreadExecutor");
} catch (final ReflectiveOperationException notLoomEarlyAccess) {
temp = null;
} finally {
m = temp;
}
} else {
m = null;
}
if (m != null) {
jaxRsExecutorService = () -> {
try {
return (ExecutorService) m.invoke(null);
} catch (final ReflectiveOperationException reflectiveOperationException) {
throw new IllegalStateException(reflectiveOperationException.getMessage(), reflectiveOperationException);
}
};
} else {
jaxRsExecutorService = ServerThreadPoolSupplier.builder()
.name("server")
.config(config.get("server.executor-service"))
.config(serverConfig.get("server.executor-service"))
.build();
}
}
// redirect to the first page when root is accessed (if configured)

29
pom.xml
View File

@@ -1456,5 +1456,34 @@ helidon-parent,helidon-dependencies,helidon-bom,helidon-se,helidon-mp,io.grpc,he
</plugins>
</build>
</profile>
<profile>
<id>loom</id>
<activation>
<jdk>16-loom</jdk>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>16</source>
<target>16</target>
<release>16</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<source>16</source>
<release>16</release>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
</project>

View File

@@ -16,7 +16,9 @@
package io.helidon.webserver.jersey;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;
import io.helidon.common.configurable.ThreadPoolSupplier;
@@ -30,23 +32,51 @@ class AsyncExecutorProvider implements ExecutorServiceProvider {
private final Supplier<ExecutorService> executorServiceSupplier;
AsyncExecutorProvider(Supplier<ExecutorService> supplier) {
this.executorServiceSupplier = supplier;
this.executorServiceSupplier = Objects.requireNonNull(supplier);
}
static ExecutorServiceProvider create(Config config) {
return new AsyncExecutorProvider(ThreadPoolSupplier.builder()
Config asyncExecutorServiceConfig = config.get("async-executor-service");
final java.lang.reflect.Method m;
if (asyncExecutorServiceConfig.get("virtual-threads").asBoolean().orElse(false)) {
java.lang.reflect.Method temp = null;
try {
temp = Executors.class.getDeclaredMethod("newVirtualThreadExecutor");
} catch (final ReflectiveOperationException notLoom) {
temp = null;
} finally {
m = temp;
}
} else {
m = null;
}
if (m != null) {
return new AsyncExecutorProvider(() -> {
try {
return (ExecutorService) m.invoke(null);
} catch (final ReflectiveOperationException reflectiveOperationException) {
throw new IllegalStateException(reflectiveOperationException.getMessage(), reflectiveOperationException);
}
});
} else {
return new AsyncExecutorProvider(ThreadPoolSupplier.builder()
.corePoolSize(1)
.maxPoolSize(10)
.prestart(false)
.threadNamePrefix("helidon-jersey-async")
.config(config.get("async-executor-service"))
.config(asyncExecutorServiceConfig)
.build());
}
}
static ExecutorServiceProvider create(ExecutorService executor) {
return new AsyncExecutorProvider(() -> executor);
}
static ExecutorServiceProvider create(Supplier<ExecutorService> executorServiceSupplier) {
return new AsyncExecutorProvider(executorServiceSupplier);
}
@Override
public ExecutorService getExecutorService() {
return executorServiceSupplier.get();