正如我的标题所述,我使用Netty运行http2的客户端。
但是,虽然我发送的标题是成功并且打印为'ok',但是我发送的数据却遇到超时错误和打印异常。
我很困惑为什么会这样。
这是我的代码和控制台输出:
代码:
public class Http2client2 {
public static void main(String[] args) throws Exception {
EventLoopGroup workerGroup = new NioEventLoopGroup();
Http2ClientInitializer initializer = new Http2ClientInitializer(null,Integer.MAX_VALUE);
try {
// Configure the client.
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE,true);
b.remoteAddress(HOST,PORT);
b.handler(initializer);
// Start the client.
Channel channel = b.connect().syncUninterruptibly().channel();
System.out.println("Connected to [" + HOST + ':' + PORT + ']');
// Wait for the HTTP/2 upgrade to occur.
Http2SettingsHandler http2SettingsHandler = initializer.settingsHandler();
http2SettingsHandler.awaitSettings(50000,TimeUnit.SECONDS);
HttpResponseHandler responseHandler = initializer.responseHandler();
int streamId = 3;
HttpScheme scheme = SSL ? HttpScheme.HTTPS : HttpScheme.HTTP;
AsciiString hostName = new AsciiString(HOST + ':' + PORT);
System.err.println("Sending request(s)...");
channel.flush();
ChannelHandlerContext ctx = channel.pipeline().lastContext();
Http2ConnectionEncoder encoder = initializer.getconnectionHandler().encoder();
ChannelFuture future1 = encoder.writeHeaders(ctx,streamId,new DefaultHttp2Headers(),false,ctx.newPromise());
future1.get(1000,TimeUnit.MILLISECONDS);
System.out.println("ok");
System.out.println(future1.isSuccess());
channel.flush();
ChannelFuture future2 = encoder.writeData(ctx,Unpooled.buffer(100),true,ctx.newPromise());
future2.get(1000,TimeUnit.MILLISECONDS);
System.out.println("ok");
System.out.println(future2.isSuccess());
channel.flush();
responseHandler.awaitResponses(5,TimeUnit.SECONDS);
System.out.println("Finished HTTP/2 request(s)");
TimeUnit.SECONDS.sleep(5);
// Wait until the connection is closed.
channel.close().sync();
} finally {
workerGroup.shutdownGracefully();
}
}
}
输出:
ok
true
00:40:22.753 [nioEventLoopGroup-2-1] INFO com.ericsson.sep.occ.simulator.http2.client.Http2ClientInitializer - [id: 0x4eb9df35,L:/127.0.0.1:55354 ! R:/127.0.0.1:8080] OUTBOUND RST_STREAM: streamId=3 errorCode=5
Exception in thread "main" java.util.concurrent.TimeoutException
at io.netty.util.concurrent.DefaultPromise.get(DefaultPromise.java:358)
at com.ericsson.sep.occ.simulator.http2.client.Http2client2.main(Http2client2.java:58)
00:40:22.754 [nioEventLoopGroup-2-1] INFO com.ericsson.sep.occ.simulator.http2.client.Http2ClientInitializer - [id: 0x4eb9df35,L:/127.0.0.1:55354 ! R:/127.0.0.1:8080] OUTBOUND GO_AWAY: lastStreamId=0 errorCode=2 length=0 bytes=
00:40:22.758 [nioEventLoopGroup-2-1] DEBUG io.netty.handler.codec.http2.Http2ConnectionHandler - [id: 0x4eb9df35,L:/127.0.0.1:55354 ! R:/127.0.0.1:8080] Sending goaWAY failed: lastStreamId '0',errorCode '2',debugData ''. Forcing shutdown of the connection.
java.nio.channels.ClosedChannelException: null
at io.netty.channel.AbstractChannel$AbstractUnsafe.newClosedChannelException(AbstractChannel.java:958)
at io.netty.channel.AbstractChannel$AbstractUnsafe.write(AbstractChannel.java:866)
at io.netty.channel.DefaultChannelPipeline$HeadContext.write(DefaultChannelPipeline.java:1379)
Process finished with exit code 1
那么,该怎么办?
我认为我的代码是错误的,但是在哪里?