Search in sources :

Example 1 with AbstractBinPushConsumer

use of org.apache.hc.client5.http.async.methods.AbstractBinPushConsumer in project mercury by yellow013.

the class AsyncClientH2ServerPush method main.

public static void main(final String[] args) throws Exception {
    final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(5)).build();
    final H2Config h2Config = H2Config.custom().setPushEnabled(true).build();
    final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setIOReactorConfig(ioReactorConfig).setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2).setH2Config(h2Config).build();
    client.start();
    client.register("*", new Supplier<AsyncPushConsumer>() {

        @Override
        public AsyncPushConsumer get() {
            return new AbstractBinPushConsumer() {

                @Override
                protected void start(final HttpRequest promise, final HttpResponse response, final ContentType contentType) throws HttpException, IOException {
                    System.out.println(promise.getPath() + " (push)->" + new StatusLine(response));
                }

                @Override
                protected int capacityIncrement() {
                    return Integer.MAX_VALUE;
                }

                @Override
                protected void data(final ByteBuffer data, final boolean endOfStream) throws IOException {
                }

                @Override
                protected void completed() {
                }

                @Override
                public void failed(final Exception cause) {
                    System.out.println("(push)->" + cause);
                }

                @Override
                public void releaseResources() {
                }
            };
        }
    });
    final BasicHttpRequest request = BasicRequestBuilder.get("https://nghttp2.org/httpbin/").build();
    System.out.println("Executing request " + request);
    final Future<Void> future = client.execute(new BasicRequestProducer(request, null), new AbstractCharResponseConsumer<Void>() {

        @Override
        protected void start(final HttpResponse response, final ContentType contentType) throws HttpException, IOException {
            System.out.println(request + "->" + new StatusLine(response));
        }

        @Override
        protected int capacityIncrement() {
            return Integer.MAX_VALUE;
        }

        @Override
        protected void data(final CharBuffer data, final boolean endOfStream) throws IOException {
        }

        @Override
        protected Void buildResult() throws IOException {
            return null;
        }

        @Override
        public void failed(final Exception cause) {
            System.out.println(request + "->" + cause);
        }

        @Override
        public void releaseResources() {
        }
    }, null);
    future.get();
    System.out.println("Shutting down");
    client.close(CloseMode.GRACEFUL);
}
Also used : AsyncPushConsumer(org.apache.hc.core5.http.nio.AsyncPushConsumer) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) AbstractBinPushConsumer(org.apache.hc.client5.http.async.methods.AbstractBinPushConsumer) ContentType(org.apache.hc.core5.http.ContentType) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) CharBuffer(java.nio.CharBuffer) HttpResponse(org.apache.hc.core5.http.HttpResponse) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) StatusLine(org.apache.hc.core5.http.message.StatusLine) CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient) HttpException(org.apache.hc.core5.http.HttpException) H2Config(org.apache.hc.core5.http2.config.H2Config)

Aggregations

IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 CharBuffer (java.nio.CharBuffer)1 AbstractBinPushConsumer (org.apache.hc.client5.http.async.methods.AbstractBinPushConsumer)1 CloseableHttpAsyncClient (org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient)1 ContentType (org.apache.hc.core5.http.ContentType)1 HttpException (org.apache.hc.core5.http.HttpException)1 HttpRequest (org.apache.hc.core5.http.HttpRequest)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)1 StatusLine (org.apache.hc.core5.http.message.StatusLine)1 AsyncPushConsumer (org.apache.hc.core5.http.nio.AsyncPushConsumer)1 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)1 H2Config (org.apache.hc.core5.http2.config.H2Config)1 IOReactorConfig (org.apache.hc.core5.reactor.IOReactorConfig)1