Skip to content

Commit 50dd8db

Browse files
committed
[Http client] avaje#442 extract helper method for newVirtualThreadPerTaskExecutor
1 parent d9ff775 commit 50dd8db

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

http-client/src/main/java/io/avaje/http/client/DHttpClientBuilder.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import static java.util.Objects.requireNonNull;
2424

25-
import java.lang.invoke.MethodHandle;
2625
import java.lang.invoke.MethodHandles;
2726
import java.lang.invoke.MethodType;
2827

@@ -106,19 +105,7 @@ private java.net.http.HttpClient defaultClient() {
106105
if (executor != null) {
107106
builder.executor(executor);
108107
} else if (Integer.getInteger("java.specification.version") >= 21) {
109-
try {
110-
ExecutorService virtualExecutorService =
111-
(ExecutorService)
112-
MethodHandles.lookup()
113-
.findStatic(
114-
Executors.class,
115-
"newVirtualThreadPerTaskExecutor",
116-
MethodType.methodType(ExecutorService.class))
117-
.invokeExact();
118-
builder.executor(virtualExecutorService);
119-
} catch (Throwable t) {
120-
// Impossible
121-
}
108+
builder.executor(virtualThreadExecutor());
122109
}
123110
if (proxy != null) {
124111
builder.proxy(proxy);
@@ -138,6 +125,17 @@ private java.net.http.HttpClient defaultClient() {
138125
return builder.build();
139126
}
140127

128+
private static ExecutorService virtualThreadExecutor() {
129+
try {
130+
return (ExecutorService)
131+
MethodHandles.lookup()
132+
.findStatic(Executors.class, "newVirtualThreadPerTaskExecutor", MethodType.methodType(ExecutorService.class))
133+
.invokeExact();
134+
} catch (Throwable e) {
135+
return null;
136+
}
137+
}
138+
141139
/**
142140
* Create a reasonable default BodyAdapter if avaje-jsonb or Jackson are present.
143141
*/

0 commit comments

Comments
 (0)