Search code examples
ssljettyhttp2

Jetty HttpClientTransportOverHTTP2 with SSL


I am trying to make a simple GET request using Jetty's HttpClientTransportOverHTTP2, but it fails. Here is my code:

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.http2.client.HTTP2Client;
import org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2;
import org.eclipse.jetty.util.ssl.SslContextFactory;

public class JettyClientExample {

    public static void main(String[] args) throws Exception {
        HttpClientTransportOverHTTP2 clientTransport = new HttpClientTransportOverHTTP2(new HTTP2Client());
        HttpClient client = new HttpClient(clientTransport,  new SslContextFactory(true));
        client.start();
        ContentResponse response = client.GET("https://http2.akamai.com");
        System.out.println("Version: " + response.getVersion());
        System.out.println("Status: " + response.getStatus());
        System.out.println("Content: " + response.getContentAsString());
    }
}

And here is the Exception I get in the line client.GET("https://http2.akamai.com")

Exception in thread "main" java.util.concurrent.ExecutionException: java.nio.channels.ClosedChannelException
    at org.eclipse.jetty.client.util.FutureResponseListener.getResult(FutureResponseListener.java:118)
    at org.eclipse.jetty.client.util.FutureResponseListener.get(FutureResponseListener.java:101)
    at org.eclipse.jetty.client.HttpRequest.send(HttpRequest.java:653)
    at org.eclipse.jetty.client.HttpClient.GET(HttpClient.java:343)
    at org.eclipse.jetty.client.HttpClient.GET(HttpClient.java:328)
    at de.consol.labs.h2c.examples.client.okhttp.JettyClientExample.main(JettyClientExample.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.nio.channels.ClosedChannelException
    at org.eclipse.jetty.http2.HTTP2Session.onShutdown(HTTP2Session.java:779)
    at org.eclipse.jetty.http2.HTTP2Connection$HTTP2Producer.produce(HTTP2Connection.java:181)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:162)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.execute(ExecuteProduceConsume.java:101)
    at org.eclipse.jetty.http2.HTTP2Connection.onOpen(HTTP2Connection.java:80)
    at org.eclipse.jetty.http2.client.HTTP2ClientConnectionFactory$HTTP2ClientConnection.onOpen(HTTP2ClientConnectionFactory.java:105)
    at org.eclipse.jetty.io.ssl.SslConnection.onOpen(SslConnection.java:152)
    at org.eclipse.jetty.io.ClientConnectionFactory$Helper.open(ClientConnectionFactory.java:70)
    at org.eclipse.jetty.io.ClientConnectionFactory$Helper.replaceConnection(ClientConnectionFactory.java:63)
    at org.eclipse.jetty.io.NegotiatingClientConnection.replaceConnection(NegotiatingClientConnection.java:113)
    at org.eclipse.jetty.io.NegotiatingClientConnection.onFillable(NegotiatingClientConnection.java:89)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:245)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
    at org.eclipse.jetty.io.ssl.SslConnection.onFillable(SslConnection.java:192)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:245)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
    at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:75)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:147)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)
    at java.lang.Thread.run(Thread.java:745)

I am using version 9.3.3.v20150827, and I put the ALPN JAR into my boot classpath with -Xbootclasspath/p:<path>.

For some reason, I didn't find any working example of HttpClientTransportOverHTTP2 with SSL on the Internet.

What am I missing?


Solution

  • You hit this bug, which is fixed in Jetty 9.3.4.