Search in sources :

Example 6 with Supplier

use of org.apache.hc.core5.function.Supplier 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)

Example 7 with Supplier

use of org.apache.hc.core5.function.Supplier in project httpcomponents-core by apache.

the class H2MultiplexingRequesterBootstrap method create.

public H2MultiplexingRequester create() {
    final RequestHandlerRegistry<Supplier<AsyncPushConsumer>> registry = new RequestHandlerRegistry<>(uriPatternType);
    for (final HandlerEntry<Supplier<AsyncPushConsumer>> entry : pushConsumerList) {
        registry.register(entry.hostname, entry.uriPattern, entry.handler);
    }
    final ClientH2StreamMultiplexerFactory http2StreamHandlerFactory = new ClientH2StreamMultiplexerFactory(httpProcessor != null ? httpProcessor : H2Processors.client(), new DefaultAsyncPushConsumerFactory(registry), h2Config != null ? h2Config : H2Config.DEFAULT, charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT, streamListener);
    return new H2MultiplexingRequester(ioReactorConfig, (ioSession, attachment) -> new ClientH2PrefaceHandler(ioSession, http2StreamHandlerFactory, strictALPNHandshake), ioSessionDecorator, exceptionCallback, sessionListener, DefaultAddressResolver.INSTANCE, tlsStrategy != null ? tlsStrategy : new H2ClientTlsStrategy());
}
Also used : RequestHandlerRegistry(org.apache.hc.core5.http.protocol.RequestHandlerRegistry) DefaultAsyncPushConsumerFactory(org.apache.hc.core5.http2.nio.support.DefaultAsyncPushConsumerFactory) ClientH2PrefaceHandler(org.apache.hc.core5.http2.impl.nio.ClientH2PrefaceHandler) ClientH2StreamMultiplexerFactory(org.apache.hc.core5.http2.impl.nio.ClientH2StreamMultiplexerFactory) H2ClientTlsStrategy(org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy) Supplier(org.apache.hc.core5.function.Supplier)

Aggregations

Supplier (org.apache.hc.core5.function.Supplier)4 AsyncServerExchangeHandler (org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)4 RequestHandlerRegistry (org.apache.hc.core5.http.protocol.RequestHandlerRegistry)4 IOException (java.io.IOException)3 HttpException (org.apache.hc.core5.http.HttpException)3 HttpRequest (org.apache.hc.core5.http.HttpRequest)3 HttpResponse (org.apache.hc.core5.http.HttpResponse)3 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)3 AsyncServerRequestHandler (org.apache.hc.core5.http.nio.AsyncServerRequestHandler)3 TlsStrategy (org.apache.hc.core5.http.nio.ssl.TlsStrategy)3 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)3 IOEventHandlerFactory (org.apache.hc.core5.reactor.IOEventHandlerFactory)3 IOReactorConfig (org.apache.hc.core5.reactor.IOReactorConfig)3 IOSession (org.apache.hc.core5.reactor.IOSession)3 InterruptedIOException (java.io.InterruptedIOException)2 InetSocketAddress (java.net.InetSocketAddress)2 URISyntaxException (java.net.URISyntaxException)2 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2