Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 29 additions & 45 deletions http-client/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.avaje</groupId>
Expand Down Expand Up @@ -95,66 +97,48 @@

<build>
<plugins>

<plugin> <!-- Multi-Release with 21 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<archive>
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
</archive>
</configuration>
</plugin>

<!-- Multi-Release with 21 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-generator</artifactId>
<version>12.0</version>
</path>
</annotationProcessorPaths>
</configuration>
<executions>
<execution>
<id>default-testCompile</id>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-generator</artifactId>
<version>12.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</execution>
<!-- Compile for base version Java 11 -->
<execution>
<id>base</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<release>11</release>
<compileSourceRoots>
<compileSourceRoot>${project.basedir}/src/main/java</compileSourceRoot>
</compileSourceRoots>
</configuration>
</execution>
<!-- Compile for Java 21 -->
<execution>
<id>java21</id>
<id>compile-java-21</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<release>21</release>
<compileSourceRoots>
<compileSourceRoot>${project.basedir}/src/main/java21</compileSourceRoot>
<compileSourceRoot>
${project.basedir}/src/main/java21</compileSourceRoot>
</compileSourceRoots>
<outputDirectory>${project.build.outputDirectory}/META-INF/versions/21</outputDirectory>
<multiReleaseOutput>true</multiReleaseOutput>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
</archive>
</configuration>
</plugin>

<!-- generated by avaje inject -->
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.avaje.http.client;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.net.Authenticator;
import java.net.CookieHandler;
import java.net.CookieManager;
Expand All @@ -15,8 +13,6 @@
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Function;

import javax.net.ssl.SSLContext;
Expand Down Expand Up @@ -55,8 +51,7 @@ final class DHttpClientBuilder implements HttpClient.Builder, HttpClient.Builder
private final Set<String> suppressed = new HashSet<>();

private void configureRetryHandler(BeanScope beanScope) {
beanScope.getOptional(RetryHandler.class)
.ifPresent(this::setRetryHandler);
beanScope.getOptional(RetryHandler.class).ifPresent(this::setRetryHandler);
}

private void setRetryHandler(RetryHandler retryHandler) {
Expand Down Expand Up @@ -108,8 +103,8 @@ private java.net.http.HttpClient defaultClient() {
}
if (executor != null) {
builder.executor(executor);
} else if (Integer.getInteger("java.specification.version") >= 21) {
builder.executor(virtualThreadExecutor());
} else if (Runtime.version().feature() >= 21) {
builder.executor(JDK21Functions.getExecutor());
}
if (proxy != null) {
builder.proxy(proxy);
Expand All @@ -129,17 +124,6 @@ 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 Expand Up @@ -192,8 +176,7 @@ private DHttpClientContext buildClient() {
buildIntercept());
}

DHttpClientBuilder() {
}
DHttpClientBuilder() {}

@Override
public HttpClient.Builder client(java.net.http.HttpClient client) {
Expand Down Expand Up @@ -377,5 +360,4 @@ public Duration requestTimeout() {
public RetryHandler retryHandler() {
return retryHandler;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import io.avaje.applog.AppLog;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Type;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
Expand Down Expand Up @@ -124,9 +122,8 @@ public HttpClient.Metrics metrics() {
public HttpClient.Metrics metrics(boolean reset) {
if (reset) {
return new DMetrics(metricResTotal.sumThenReset(), metricResError.sumThenReset(), metricResBytes.sumThenReset(), metricResMicros.sumThenReset(), metricResMaxMicros.getThenReset());
} else {
return new DMetrics(metricResTotal.sum(), metricResError.sum(), metricResBytes.sum(), metricResMicros.sum(), metricResMaxMicros.get());
}
return new DMetrics(metricResTotal.sum(), metricResError.sum(), metricResBytes.sum(), metricResMicros.sum(), metricResMaxMicros.get());
}

void metricsString(int stringBody) {
Expand Down Expand Up @@ -395,14 +392,6 @@ String maxResponseBody(String body) {
@Override
public void close() {
this.closed = true;
if (Integer.getInteger("java.specification.version") >= 21) {
try {
MethodHandles.lookup()
.findVirtual(java.net.http.HttpClient.class, "close", MethodType.methodType(void.class))
.invokeExact(httpClient);
} catch (Throwable t) {
throw new IllegalStateException("Failed to close java.net.http.HttpClient instance");
}
}
JDK21Functions.closeClient(httpClient);
}
}
18 changes: 18 additions & 0 deletions http-client/src/main/java/io/avaje/http/client/JDK21Functions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.avaje.http.client;

import java.net.http.HttpClient;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

final class JDK21Functions {
private JDK21Functions() {}

// only executed in tests
static ExecutorService getExecutor() {
return Executors.newCachedThreadPool();
}

static void closeClient(HttpClient client) {
// no-op
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.avaje.http.client;

import java.net.http.HttpClient;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

final class JDK21Functions {
private JDK21Functions() {}

static ExecutorService getExecutor() {
return Executors.newVirtualThreadPerTaskExecutor();
}

static void closeClient(HttpClient client) {
client.close();
}
}