Search in sources :

Example 6 with Http2StreamChannelBootstrap

use of io.netty.handler.codec.http2.Http2StreamChannelBootstrap in project netty by netty.

the class Http2FrameClient method main.

public static void main(String[] args) throws Exception {
    final EventLoopGroup clientWorkerGroup = new NioEventLoopGroup();
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {
        final SslProvider provider = SslProvider.isAlpnSupported(SslProvider.OPENSSL) ? SslProvider.OPENSSL : SslProvider.JDK;
        sslCtx = SslContextBuilder.forClient().sslProvider(provider).ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE).trustManager(InsecureTrustManagerFactory.INSTANCE).applicationProtocolConfig(new ApplicationProtocolConfig(Protocol.ALPN, SelectorFailureBehavior.NO_ADVERTISE, SelectedListenerFailureBehavior.ACCEPT, ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1)).build();
    } else {
        sslCtx = null;
    }
    try {
        final Bootstrap b = new Bootstrap();
        b.group(clientWorkerGroup);
        b.channel(NioSocketChannel.class);
        b.option(ChannelOption.SO_KEEPALIVE, true);
        b.remoteAddress(HOST, PORT);
        b.handler(new Http2ClientFrameInitializer(sslCtx));
        // Start the client.
        final Channel channel = b.connect().syncUninterruptibly().channel();
        System.out.println("Connected to [" + HOST + ':' + PORT + ']');
        final Http2ClientStreamFrameResponseHandler streamFrameResponseHandler = new Http2ClientStreamFrameResponseHandler();
        final Http2StreamChannelBootstrap streamChannelBootstrap = new Http2StreamChannelBootstrap(channel);
        final Http2StreamChannel streamChannel = streamChannelBootstrap.open().syncUninterruptibly().getNow();
        streamChannel.pipeline().addLast(streamFrameResponseHandler);
        // Send request (a HTTP/2 HEADERS frame - with ':method = GET' in this case)
        final DefaultHttp2Headers headers = new DefaultHttp2Headers();
        headers.method("GET");
        headers.path(PATH);
        headers.scheme(SSL ? "https" : "http");
        final Http2HeadersFrame headersFrame = new DefaultHttp2HeadersFrame(headers, true);
        streamChannel.writeAndFlush(headersFrame);
        System.out.println("Sent HTTP/2 GET request to " + PATH);
        // Wait for the responses (or for the latch to expire), then clean up the connections
        if (!streamFrameResponseHandler.responseSuccessfullyCompleted()) {
            System.err.println("Did not get HTTP/2 response in expected time.");
        }
        System.out.println("Finished HTTP/2 request, will close the connection.");
        // Wait until the connection is closed.
        channel.close().syncUninterruptibly();
    } finally {
        clientWorkerGroup.shutdownGracefully();
    }
}
Also used : DefaultHttp2HeadersFrame(io.netty.handler.codec.http2.DefaultHttp2HeadersFrame) Http2HeadersFrame(io.netty.handler.codec.http2.Http2HeadersFrame) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) Http2StreamChannel(io.netty.handler.codec.http2.Http2StreamChannel) ApplicationProtocolConfig(io.netty.handler.ssl.ApplicationProtocolConfig) DefaultHttp2HeadersFrame(io.netty.handler.codec.http2.DefaultHttp2HeadersFrame) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Http2StreamChannelBootstrap(io.netty.handler.codec.http2.Http2StreamChannelBootstrap) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) Bootstrap(io.netty.bootstrap.Bootstrap) Http2StreamChannelBootstrap(io.netty.handler.codec.http2.Http2StreamChannelBootstrap) SslProvider(io.netty.handler.ssl.SslProvider) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext) Http2StreamChannel(io.netty.handler.codec.http2.Http2StreamChannel)

Aggregations

Channel (io.netty.channel.Channel)4 Http2StreamChannelBootstrap (io.netty.handler.codec.http2.Http2StreamChannelBootstrap)4 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 StreamException (io.netty.handler.codec.http2.Http2Exception.StreamException)2 Http2StreamChannel (io.netty.handler.codec.http2.Http2StreamChannel)2 Test (org.junit.Test)2 NettyUtils (com.github.ambry.commons.NettyUtils)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 ChannelId (io.netty.channel.ChannelId)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 ChannelOutboundInvoker (io.netty.channel.ChannelOutboundInvoker)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)1 DefaultHttp2HeadersFrame (io.netty.handler.codec.http2.DefaultHttp2HeadersFrame)1 Http2GoAwayFrame (io.netty.handler.codec.http2.Http2GoAwayFrame)1 Http2HeadersFrame (io.netty.handler.codec.http2.Http2HeadersFrame)1