Jetty HTTP/2客户端接收服务器推送示例

Jetty HTTP/2客户端接收服务器推送示例,jetty,server-push,http2,nghttp2,Jetty,Server Push,Http2,Nghttp2,Jetty的HTTP/2客户端和服务器推送支持已在Jetty 9.3 RC()中实现。但是,我没有找到任何与此相关的文档或示例代码。任何人都可以提供一个示例代码,例如从这个站点接收推送的资源:(公共服务器) ---更新1--- 正如斯伯德所说,我已经试过测试了。但是,在执行这一行之后 mvn compile exec:java 我偶然发现了这个错误 [INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ http2client --- 2

Jetty的HTTP/2客户端和服务器推送支持已在Jetty 9.3 RC()中实现。但是,我没有找到任何与此相关的文档或示例代码。任何人都可以提供一个示例代码,例如从这个站点接收推送的资源:(公共服务器)

---更新1--- 正如斯伯德所说,我已经试过测试了。但是,在执行这一行之后

mvn compile exec:java
我偶然发现了这个错误

[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ http2client ---
2015-05-05 01:52:47.808:INFO::com.example.Client.main(): Logging initialized @3096ms
[WARNING]
java.lang.reflect.InvocationTargetException
    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:483)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.util.concurrent.TimeoutException
    at org.eclipse.jetty.util.FuturePromise.get(FuturePromise.java:130)
    at com.example.Client.main(Client.java:55)
    ... 6 more
2015-05-05 19:05:25.094:INFO::main: Logging initialized @220ms
Exception in thread "main" java.util.concurrent.ExecutionException: java.nio.channels.ClosedChannelException
    at org.eclipse.jetty.util.FuturePromise.get(FuturePromise.java:138)
    at org.eclipse.jetty.http2.client.Client.main(Client.java:55)
    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:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.nio.channels.ClosedChannelException
    at org.eclipse.jetty.http2.HTTP2Flusher.append(HTTP2Flusher.java:110)
    at org.eclipse.jetty.http2.HTTP2Session.frame(HTTP2Session.java:577)
    at org.eclipse.jetty.http2.HTTP2Session.frames(HTTP2Session.java:559)
    at org.eclipse.jetty.http2.client.HTTP2ClientConnectionFactory$HTTP2ClientConnection.onOpen(HTTP2ClientConnectionFactory.java:121)
这是我的
pom.xml

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>http2client</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>http2client</name>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupId>org.eclipse.jetty.http2</groupId>
        <artifactId>http2-client</artifactId>
        <version>9.3.0.M2</version>
    </dependency>
    <dependency>
        <groupId>org.mortbay.jetty.alpn</groupId>
        <artifactId>alpn-boot</artifactId>
        <version>8.1.3.v20150130</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.4.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>com.example.Client</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>
---更新2---

将我的
pom.xml
标记更改为:(显式使用JDK 8并添加-Xbootclasspath以指向Jetty提供的alpn-boot.jar)。我正在使用Java8更新31

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.4.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>java</executable>
                <arguments>
                    <argument>-Xbootclasspath/p:/path/to/alpn-boot-8.1.3.v20150130.jar</argument>
                    <argument>-classpath</argument>
                    <classpath/>
                    <argument>com.example.Client</argument>
                </arguments>
            </configuration>
        </plugin>
    </plugins>
</build>
我在尝试连接到(Client.java文件中的默认主机)时出错

连接到时会出现此错误

---更新3---

采取另一种方法:当我提取整个jetty项目的所有主分支,然后在
jetty.project/jetty-http2/http2 client
上创建一个Intellij项目时,它就适用于公共服务器和服务器。但是当我在我的自签名证书http2服务器上测试它时(使用驻留在我的虚拟机中的nghttp2+nginx),我得到了这个错误

[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ http2client ---
2015-05-05 01:52:47.808:INFO::com.example.Client.main(): Logging initialized @3096ms
[WARNING]
java.lang.reflect.InvocationTargetException
    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:483)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.util.concurrent.TimeoutException
    at org.eclipse.jetty.util.FuturePromise.get(FuturePromise.java:130)
    at com.example.Client.main(Client.java:55)
    ... 6 more
2015-05-05 19:05:25.094:INFO::main: Logging initialized @220ms
Exception in thread "main" java.util.concurrent.ExecutionException: java.nio.channels.ClosedChannelException
    at org.eclipse.jetty.util.FuturePromise.get(FuturePromise.java:138)
    at org.eclipse.jetty.http2.client.Client.main(Client.java:55)
    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:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.nio.channels.ClosedChannelException
    at org.eclipse.jetty.http2.HTTP2Flusher.append(HTTP2Flusher.java:110)
    at org.eclipse.jetty.http2.HTTP2Session.frame(HTTP2Session.java:577)
    at org.eclipse.jetty.http2.HTTP2Session.frames(HTTP2Session.java:559)
    at org.eclipse.jetty.http2.client.HTTP2ClientConnectionFactory$HTTP2ClientConnection.onOpen(HTTP2ClientConnectionFactory.java:121)

问题中报告的链接为Jetty的
HttpClient
提供了一个HTTP/2传输

Jetty的
HttpClient
向必须使用HTTP 1.0、1.1和2.0的应用程序公开通用HTTP API,因此
HttpClient
不公开任何API来接收HTTP/2推送资源,因为这些只是HTTP/2的一种特殊机制

如果您真的想与HTTP/2 API交互,可以使用Jetty的
HTTP2Client
,它向应用程序公开一个较低级别的API,即HTTP/2专用API


您可以找到一个连接到推送资源的网站的完整示例(在本例中)。

回答更新3

使用SslContextFactory的另一个构造函数,该构造函数具有布尔参数trustAll certificates:

SslContextFactory sslContextFactory = new SslContextFactory(true);
整个代码:

HTTP2Client client = new HTTP2Client();
SslContextFactory sslContextFactory = new SslContextFactory();
client.addBean(sslContextFactory);
client.start();

谢谢你,我已经尝试了这个文件,但是一直出错。请参阅我的更新答案您必须按照指定将ALPN引导jar放入bootclasspath。你做到了吗?您还必须使用JDK 8。如果你这样做了,你想点击哪台服务器?是的,我正在使用JDK 8,现在我已经添加了ALPN引导jar,但仍然会出错。我正在尝试连接到并请检查我的更新2。您是否使用Jetty主分支的最新代码?我只使用版本9.3.0.M2的maven依赖项,并将主分支中的
Client.java
复制到我的项目中(将包更改为
package com.example
并添加
import org.eclipse.jetty.http2.client.*
这是正确的方法吗?
SslContextFactory sslContextFactory = new SslContextFactory(true);
HTTP2Client client = new HTTP2Client();
SslContextFactory sslContextFactory = new SslContextFactory();
client.addBean(sslContextFactory);
client.start();