Skip to content

Commit

Permalink
[Http-Client] Use virtual thread executor by default (#442)
Browse files Browse the repository at this point in the history
* use virtual thread executor by default

* only try if > 21

* [Http client] #442 extract helper method for newVirtualThreadPerTaskExecutor

---------

Co-authored-by: Rob Bygrave <[email protected]>
  • Loading branch information
SentryMan and rbygrave authored Dec 5, 2024
1 parent 5e44ea3 commit 8998f3b
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Function;

import static java.util.Objects.requireNonNull;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

final class DHttpClientBuilder implements HttpClient.Builder, HttpClient.Builder.State {

private java.net.http.HttpClient client;
Expand Down Expand Up @@ -99,6 +104,8 @@ private java.net.http.HttpClient defaultClient() {
}
if (executor != null) {
builder.executor(executor);
} else if (Integer.getInteger("java.specification.version") >= 21) {
builder.executor(virtualThreadExecutor());
}
if (proxy != null) {
builder.proxy(proxy);
Expand All @@ -118,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 8998f3b

Please sign in to comment.