2015-05-03 8 views
5

Il client HTTP/2 di Jetty con supporto push server è stato implementato in Jetty 9.3 RC (Link). Tuttavia, non ho trovato alcuna documentazione o codice di esempio relativo a questo. Qualcuno potrebbe fornire un codice di esempio per esempio per ricevere la risorsa spinto da questo sito: https://nghttp2.org (server pubblico che ha enabled http2 server push)Jetty Client HTTP/2 riceve esempio push server

--- --- UPDATE 1 Ho cercato di testare this file come sbordet ha disse. Tuttavia, dopo l'esecuzione di questa linea

mvn compile exec:java 

mi sono imbattuto su questo errore

[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 

Ecco il mio 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> 

Ed ecco la mia directory del progetto

|-- pom.xml 
|-- src 
| `-- main 
|  `-- java 
|   `-- com 
|    `-- example 
|     `-- Client.java 
`-- target 
    |-- classes 
    | `-- com 
    |  `-- example 
    |   |-- Client$1.class 
    |   `-- Client.class 
    `-- maven-status 
     `-- maven-compiler-plugin 
      `-- compile 
       `-- default-compile 
        |-- createdFiles.lst 
        `-- inputFiles.lst 

--- --- UPDATE 2

cambiato la mia tag pom.xml<build> a questo: (usate in modo esplicito JDK 8 e aggiungere -Xbootclasspath per puntare al alpn-boot.jar fornito dal molo). Sto usando Java 8 aggiornamento 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> 

Dopo aver eseguire questo comando:

mvn clean compile exec:exec 

ho ottenuto questo errore quando si tenta di connettersi a https://webtide.com/ (l'host di default nel file di Client.java)

[INFO] --- exec-maven-plugin:1.4.0:exec (default-cli) @ http2client --- 
2015-05-05 13:19:25.499:INFO::main: Logging initialized @153ms 
Exception in thread "main" java.util.concurrent.TimeoutException 
    at org.eclipse.jetty.util.FuturePromise.get(FuturePromise.java:130) 
    at com.example.Client.main(Client.java:55) 

E questo errore durante la connessione a https://nghttp2.org/

[INFO] --- exec-maven-plugin:1.4.0:exec (default-cli) @ http2client --- 
2015-05-05 13:29:12.106:INFO::main: Logging initialized @196ms 
Exception in thread "main" java.util.concurrent.TimeoutException 
    at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Phaser.java:800) 
    at com.example.Client.main(Client.java:90) 

--- --- UPDATE 3

adottare un approccio diverso: Quando tiro tutto il ramo principale di tutto il progetto molo, e quindi creare un progetto IntelliJ a jetty.project/jetty-http2/http2-client allora funziona per i server pubblici https://webtide.com e https://nghttp2.org. Ma quando ho test sul mio server di certificati HTTP2 auto-firmato (utilizzando nghttp2 + nginx, risiedeva nella mia macchina virtuale), allora ottengo questo errore

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) 

risposta

3

Il link riportato in questione presenta una 2 trasporto/HTTP per Jetty's HttpClient.

Jetty's HttpClient espone una API HTTP generica alle applicazioni che devono funzionare con HTTP 1.0, 1.1 e 2.0 e come tale HttpClient non espone alcuna API per ricevere risorse push HTTP/2, poiché questi sono un meccanismo peculiare di HTTP/Solo 2.

Se si desidera interagire con un'API HTTP/2, è possibile utilizzare Jetty's HTTP2Client, che espone un'API di livello inferiore, specifica HTTP/2, alle applicazioni.

È possibile trovare un esempio completo di connessione a un sito Web che spinge risorse (in questo caso https://webtide.com) here.

+0

Grazie, ho provato il file ma continua a ricevere errori. Si prega di consultare la mia risposta aggiornata –

+0

È necessario inserire il jar di avvio ALPN nel percorso bootclass, come specificato [qui] (https://www.eclipse.org/jetty/documentation/current/alpn-chapter.html). L'hai fatto? Devi anche usare JDK 8. Se lo hai fatto, quale server stai cercando di colpire? – sbordet

+0

Sì, sto usando JDK 8 e ora ho aggiunto il jar di avvio ALPN, ma continuo ad avere un errore. Stavo cercando di connettermi a https://webtide.com e https://nghttp2.org. Controlla il mio aggiornamento 2. –

1

risposta per UPDATE 3:

utilizzare un altro costruttore della SslContextFactory, che ha un argomento booleano trustAll certificati:

SslContextFactory sslContextFactory = new SslContextFactory(true); 

L'intero codice:

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