Skip to content

Commit

Permalink
[Http client] avaje#442 extract helper method for newVirtualThreadPer…
Browse files Browse the repository at this point in the history
…TaskExecutor
  • Loading branch information
rbygrave committed Nov 14, 2024
1 parent d9ff775 commit 50dd8db
Showing 1 changed file with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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.
*/
Expand Down

0 comments on commit 50dd8db

Please sign in to comment.