public Cancellable execute()

in httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalH2AsyncClient.java [122:258]


    public Cancellable execute(
            final AsyncClientExchangeHandler exchangeHandler,
            final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
            final HttpContext context) {
        Args.notNull(exchangeHandler, "Message exchange handler");
        final ComplexCancellable cancellable = new ComplexCancellable();
        try {
            if (!isRunning()) {
                throw new CancellationException("Request execution cancelled");
            }
            final HttpClientContext clientContext = HttpClientContext.castOrCreate(context);
            exchangeHandler.produceRequest((request, entityDetails, context1) -> {
                RequestConfig requestConfig = null;
                if (request instanceof Configurable) {
                    requestConfig = ((Configurable) request).getConfig();
                }
                if (requestConfig != null) {
                    clientContext.setRequestConfig(requestConfig);
                } else {
                    requestConfig = clientContext.getRequestConfigOrDefault();
                }
                @SuppressWarnings("deprecation")
                final Timeout connectTimeout = requestConfig.getConnectTimeout();
                final HttpHost target = new HttpHost(request.getScheme(), request.getAuthority());

                final Future<IOSession> sessionFuture = connPool.getSession(new HttpRoute(target), connectTimeout,
                    new FutureCallback<IOSession>() {

                    @Override
                    public void completed(final IOSession session) {
                        final AsyncClientExchangeHandler internalExchangeHandler = new AsyncClientExchangeHandler() {

                            @Override
                            public void releaseResources() {
                                exchangeHandler.releaseResources();
                            }

                            @Override
                            public void failed(final Exception cause) {
                                exchangeHandler.failed(cause);
                            }

                            @Override
                            public void cancel() {
                                failed(new RequestFailedException("Request aborted"));
                            }

                            @Override
                            public void produceRequest(
                                    final RequestChannel channel,
                                    final HttpContext context1) throws HttpException, IOException {
                                channel.sendRequest(request, entityDetails, context1);
                            }

                            @Override
                            public int available() {
                                return exchangeHandler.available();
                            }

                            @Override
                            public void produce(final DataStreamChannel channel) throws IOException {
                                exchangeHandler.produce(channel);
                            }

                            @Override
                            public void consumeInformation(
                                    final HttpResponse response,
                                    final HttpContext context1) throws HttpException, IOException {
                                exchangeHandler.consumeInformation(response, context1);
                            }

                            @Override
                            public void consumeResponse(
                                    final HttpResponse response,
                                    final EntityDetails entityDetails,
                                    final HttpContext context1) throws HttpException, IOException {
                                exchangeHandler.consumeResponse(response, entityDetails, context1);
                            }

                            @Override
                            public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
                                exchangeHandler.updateCapacity(capacityChannel);
                            }

                            @Override
                            public void consume(final ByteBuffer src) throws IOException {
                                exchangeHandler.consume(src);
                            }

                            @Override
                            public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
                                exchangeHandler.streamEnd(trailers);
                            }

                        };
                        if (LOG.isDebugEnabled()) {
                            final String exchangeId = ExecSupport.getNextExchangeId();
                            clientContext.setExchangeId(exchangeId);
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("{} executing message exchange {}", exchangeId, ConnPoolSupport.getId(session));
                            }
                            session.enqueue(
                                    new RequestExecutionCommand(
                                            new LoggingAsyncClientExchangeHandler(LOG, exchangeId, internalExchangeHandler),
                                            pushHandlerFactory,
                                            cancellable,
                                            clientContext),
                                    Command.Priority.NORMAL);
                        } else {
                            session.enqueue(
                                    new RequestExecutionCommand(
                                            internalExchangeHandler,
                                            pushHandlerFactory,
                                            cancellable,
                                            clientContext),
                                    Command.Priority.NORMAL);
                        }
                    }

                    @Override
                    public void failed(final Exception ex) {
                        exchangeHandler.failed(ex);
                    }

                    @Override
                    public void cancelled() {
                        exchangeHandler.cancel();
                    }

                });
                cancellable.setDependency(() -> sessionFuture.cancel(true));
            }, context);
        } catch (final HttpException | IOException | IllegalStateException ex) {
            exchangeHandler.failed(ex);
        }
        return cancellable;
    }