Class HTTP2Client

  • All Implemented Interfaces:
    Container, Destroyable, Dumpable, Dumpable.DumpableContainer, LifeCycle

    @ManagedObject
    public class HTTP2Client
    extends ContainerLifeCycle

    HTTP2Client provides an asynchronous, non-blocking implementation to send HTTP/2 frames to a server.

    Typical usage:

     // Create and start HTTP2Client.
     HTTP2Client client = new HTTP2Client();
     SslContextFactory sslContextFactory = new SslContextFactory();
     client.addBean(sslContextFactory);
     client.start();
    
     // Connect to host.
     String host = "webtide.com";
     int port = 443;
    
     FuturePromise<Session> sessionPromise = new FuturePromise<>();
     client.connect(sslContextFactory, new InetSocketAddress(host, port), new ServerSessionListener.Adapter(), sessionPromise);
    
     // Obtain the client Session object.
     Session session = sessionPromise.get(5, TimeUnit.SECONDS);
    
     // Prepare the HTTP request headers.
     HttpFields requestFields = new HttpFields();
     requestFields.put("User-Agent", client.getClass().getName() + "/" + Jetty.VERSION);
     // Prepare the HTTP request object.
     MetaData.Request request = new MetaData.Request("PUT", new HttpURI("https://" + host + ":" + port + "/"), HttpVersion.HTTP_2, requestFields);
     // Create the HTTP/2 HEADERS frame representing the HTTP request.
     HeadersFrame headersFrame = new HeadersFrame(request, null, false);
    
     // Prepare the listener to receive the HTTP response frames.
     Stream.Listener responseListener = new new Stream.Listener.Adapter()
     {
          @Override
          public void onHeaders(Stream stream, HeadersFrame frame)
          {
              System.err.println(frame);
          }
    
          @Override
          public void onData(Stream stream, DataFrame frame, Callback callback)
          {
              System.err.println(frame);
              callback.succeeded();
          }
     };
    
     // Send the HEADERS frame to create a stream.
     FuturePromise<Stream> streamPromise = new FuturePromise<>();
     session.newStream(headersFrame, streamPromise, responseListener);
     Stream stream = streamPromise.get(5, TimeUnit.SECONDS);
    
     // Use the Stream object to send request content, if any, using a DATA frame.
     ByteBuffer content = ...;
     DataFrame requestContent = new DataFrame(stream.getId(), content, true);
     stream.data(requestContent, Callback.Adapter.INSTANCE);
    
     // When done, stop the client.
     client.stop();
     
    • Constructor Detail

      • HTTP2Client

        public HTTP2Client()
    • Method Detail

      • doStart

        protected void doStart()
                        throws java.lang.Exception
        Description copied from class: ContainerLifeCycle
        Starts the managed lifecycle beans in the order they were added.
        Overrides:
        doStart in class ContainerLifeCycle
        Throws:
        java.lang.Exception
      • getExecutor

        public java.util.concurrent.Executor getExecutor()
      • setExecutor

        public void setExecutor​(java.util.concurrent.Executor executor)
      • getScheduler

        public Scheduler getScheduler()
      • setScheduler

        public void setScheduler​(Scheduler scheduler)
      • setByteBufferPool

        public void setByteBufferPool​(ByteBufferPool bufferPool)
      • setClientConnectionFactory

        public void setClientConnectionFactory​(ClientConnectionFactory connectionFactory)
      • setFlowControlStrategyFactory

        public void setFlowControlStrategyFactory​(FlowControlStrategy.Factory flowControlStrategyFactory)
      • getSelectors

        @ManagedAttribute("The number of selectors")
        public int getSelectors()
      • setSelectors

        public void setSelectors​(int selectors)
      • getIdleTimeout

        @ManagedAttribute("The idle timeout in milliseconds")
        public long getIdleTimeout()
      • setIdleTimeout

        public void setIdleTimeout​(long idleTimeout)
      • getConnectTimeout

        @ManagedAttribute("The connect timeout in milliseconds")
        public long getConnectTimeout()
      • setConnectTimeout

        public void setConnectTimeout​(long connectTimeout)
      • isConnectBlocking

        @ManagedAttribute("Whether the connect() operation is blocking")
        public boolean isConnectBlocking()
      • setConnectBlocking

        public void setConnectBlocking​(boolean connectBlocking)
      • getBindAddress

        public java.net.SocketAddress getBindAddress()
      • setBindAddress

        public void setBindAddress​(java.net.SocketAddress bindAddress)
      • getInputBufferSize

        @ManagedAttribute("The size of the buffer used to read from the network")
        public int getInputBufferSize()
      • setInputBufferSize

        public void setInputBufferSize​(int inputBufferSize)
      • getProtocols

        @ManagedAttribute("The ALPN protocol list")
        public java.util.List<java.lang.String> getProtocols()
      • setProtocols

        public void setProtocols​(java.util.List<java.lang.String> protocols)
      • getInitialSessionRecvWindow

        @ManagedAttribute("The initial size of session\'s flow control receive window")
        public int getInitialSessionRecvWindow()
      • setInitialSessionRecvWindow

        public void setInitialSessionRecvWindow​(int initialSessionRecvWindow)
      • getInitialStreamRecvWindow

        @ManagedAttribute("The initial size of stream\'s flow control receive window")
        public int getInitialStreamRecvWindow()
      • setInitialStreamRecvWindow

        public void setInitialStreamRecvWindow​(int initialStreamRecvWindow)
      • getMaxFrameLength

        @ManagedAttribute("The max frame length in bytes")
        public int getMaxFrameLength()
      • setMaxFrameLength

        public void setMaxFrameLength​(int maxFrameLength)
      • getMaxConcurrentPushedStreams

        @ManagedAttribute("The max number of concurrent pushed streams")
        public int getMaxConcurrentPushedStreams()
      • setMaxConcurrentPushedStreams

        public void setMaxConcurrentPushedStreams​(int maxConcurrentPushedStreams)
      • getMaxSettingsKeys

        @ManagedAttribute("The max number of keys in all SETTINGS frames")
        public int getMaxSettingsKeys()
      • setMaxSettingsKeys

        public void setMaxSettingsKeys​(int maxSettingsKeys)
      • configure

        protected void configure​(java.nio.channels.SocketChannel channel)
                          throws java.io.IOException
        Throws:
        java.io.IOException