From 50dd8db73da38af1069b2d3543457f80bdf733f0 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Thu, 14 Nov 2024 21:57:02 +1300 Subject: [PATCH] [Http client] #442 extract helper method for newVirtualThreadPerTaskExecutor --- .../avaje/http/client/DHttpClientBuilder.java | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/http-client/src/main/java/io/avaje/http/client/DHttpClientBuilder.java b/http-client/src/main/java/io/avaje/http/client/DHttpClientBuilder.java index 09a8a96a..72e83fb1 100644 --- a/http-client/src/main/java/io/avaje/http/client/DHttpClientBuilder.java +++ b/http-client/src/main/java/io/avaje/http/client/DHttpClientBuilder.java @@ -22,7 +22,6 @@ import static java.util.Objects.requireNonNull; -import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; @@ -106,19 +105,7 @@ private java.net.http.HttpClient defaultClient() { if (executor != null) { builder.executor(executor); } else if (Integer.getInteger("java.specification.version") >= 21) { - try { - ExecutorService virtualExecutorService = - (ExecutorService) - MethodHandles.lookup() - .findStatic( - Executors.class, - "newVirtualThreadPerTaskExecutor", - MethodType.methodType(ExecutorService.class)) - .invokeExact(); - builder.executor(virtualExecutorService); - } catch (Throwable t) { - // Impossible - } + builder.executor(virtualThreadExecutor()); } if (proxy != null) { builder.proxy(proxy); @@ -138,6 +125,17 @@ private java.net.http.HttpClient defaultClient() { return builder.build(); } + private static ExecutorService virtualThreadExecutor() { + try { + return (ExecutorService) + MethodHandles.lookup() + .findStatic(Executors.class, "newVirtualThreadPerTaskExecutor", MethodType.methodType(ExecutorService.class)) + .invokeExact(); + } catch (Throwable e) { + return null; + } + } + /** * Create a reasonable default BodyAdapter if avaje-jsonb or Jackson are present. */