2

I'm using the new java.net.http.HttpClient and from my tests after about 378026 times of calling it, I start to get the following:

Caused by: java.net.ConnectException: Cannot assign requested address
        at java.net.http/jdk.internal.net.http.common.Utils.toConnectException(Utils.java:964)
        at java.net.http/jdk.internal.net.http.PlainHttpConnection.connectAsync(PlainHttpConnection.java:179)
        at java.net.http/jdk.internal.net.http.AsyncSSLConnection.connectAsync(AsyncSSLConnection.java:56)
        at java.net.http/jdk.internal.net.http.Http2Connection.createAsync(Http2Connection.java:369)
        at java.net.http/jdk.internal.net.http.Http2ClientImpl.getConnectionFor(Http2ClientImpl.java:127)
        at java.net.http/jdk.internal.net.http.ExchangeImpl.get(ExchangeImpl.java:88)
        at java.net.http/jdk.internal.net.http.Exchange.establishExchange(Exchange.java:293)
        at java.net.http/jdk.internal.net.http.Exchange.responseAsyncImpl0(Exchange.java:425)
        at java.net.http/jdk.internal.net.http.Exchange.responseAsyncImpl(Exchange.java:330)
        at java.net.http/jdk.internal.net.http.Exchange.responseAsync(Exchange.java:322)
        at java.net.http/jdk.internal.net.http.MultiExchange.responseAsyncImpl(MultiExchange.java:304)
        at java.net.http/jdk.internal.net.http.MultiExchange.lambda$responseAsync0$2(MultiExchange.java:250)
        at java.base/java.util.concurrent.CompletableFuture$UniCompose.tryFire(CompletableFuture.java:1072)
        at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:506)
        at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1705)
        at java.net.http/jdk.internal.net.http.HttpClientImpl$DelegatingExecutor.execute(HttpClientImpl.java:153)
        at java.base/java.util.concurrent.CompletableFuture.completeAsync(CompletableFuture.java:2591)
        at java.net.http/jdk.internal.net.http.MultiExchange.responseAsync(MultiExchange.java:204)
        at java.net.http/jdk.internal.net.http.HttpClientImpl.sendAsync(HttpClientImpl.java:632)

Below is the class that I use:

public class JavaHttpClient {
    HttpClient httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(30))
            .followRedirects(HttpClient.Redirect.ALWAYS).build();

    public String getRequest(String incomingUrl) throws IOException, InterruptedException {
        HttpRequest request = HttpRequest.newBuilder().uri(URI.create(incomingUrl)).GET().build();
        HttpResponse<String> response = httpClient.send(request, BodyHandlers.ofString());

        return response.body();
    }

}

I don't understand why this is happening. I assume something is being left open that should have been closed?

7
  • 2
    If you can close something, do it. Commented Jun 5, 2019 at 22:44
  • But there is no close method for HttpClient Commented Jun 5, 2019 at 22:54
  • 5
    Looks like it's a bug bugs.openjdk.java.net/browse/JDK-8221395 need to switch to okhttp or Apache httpclient Commented Jun 5, 2019 at 22:58
  • If you just want to do the web service RESTful(GET or POST), I will suggest you change HttpClient to Okhttp library. github.com/square/okhttp Commented Jun 6, 2019 at 1:10
  • Another aspect important to the question would be what test are you running? Commented Jun 6, 2019 at 2:45

1 Answer 1

1

The HttpClient will close all opened connections when it's no longer referenced and there's no operations in progress (all responses have been received).

Sign up to request clarification or add additional context in comments.

3 Comments

I don't find any of that stated in the documentation. More likely it will close opened connections according to an internal and undefined connection-pooling scheme.
HTTP/1.1 connection are closed after the keep-alive timeout occur. But all remaining opened connections in the HTTP/1.1 and HTTP/2 pools will also be closed as stated above.
Note: A close() method was added to the java.net.http client in Java 21, which makes it possible to close the client without waiting for the GC to reclaim it: docs.oracle.com/en/java/javase/23/docs/api/java.net.http/java/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.