org.apache.hc.client5.http.impl.async

## Class HttpAsyncClientBuilder

- java.lang.Object
- - org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder

- Direct Known Subclasses:
    CachingHttpAsyncClientBuilder

      public class HttpAsyncClientBuilder
      extends Object

  Builder for `CloseableHttpAsyncClient` instances that can negotiate the most optimal HTTP protocol version during the `TLS` handshake with `ALPN` extension if supported by the Java runtime.

  Concurrent message exchanges executed by `CloseableHttpAsyncClient` instances created with this builder will get automatically assigned to separate connections leased from the connection pool.

  When a particular component is not explicitly set this class will use its default implementation. System properties will be taken into account when configuring the default implementations when `useSystemProperties()` method is called prior to calling `build()`.

  - http.proxyHost
  - http.proxyPort
  - https.proxyHost
  - https.proxyPort
  - http.nonProxyHosts
  - http.keepAlive
  - http.agent

  Please note that some settings used by this class can be mutually exclusive and may not apply when building `CloseableHttpAsyncClient` instances.

  Since:
    5.0

- - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `HttpAsyncClientBuilder()`

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `addCloseable(Closeable closeable)` Adds to the list of `Closeable` resources to be managed by the client.
    `HttpAsyncClientBuilder` | `addExecInterceptorAfter(String existing, String name, AsyncExecChainHandler interceptor)` Adds this execution interceptor after interceptor with the given name.
    `HttpAsyncClientBuilder` | `addExecInterceptorBefore(String existing, String name, AsyncExecChainHandler interceptor)` Adds this execution interceptor before an existing interceptor.
    `HttpAsyncClientBuilder` | `addExecInterceptorFirst(String name, AsyncExecChainHandler interceptor)` Add an interceptor to the head of the processing list.
    `HttpAsyncClientBuilder` | `addExecInterceptorLast(String name, AsyncExecChainHandler interceptor)` Add an interceptor to the tail of the processing list.
    `HttpAsyncClientBuilder` | `addRequestInterceptorFirst(org.apache.hc.core5.http.HttpRequestInterceptor interceptor)` Adds this protocol interceptor to the head of the protocol processing list.
    `HttpAsyncClientBuilder` | `addRequestInterceptorLast(org.apache.hc.core5.http.HttpRequestInterceptor interceptor)` Adds this protocol interceptor to the tail of the protocol processing list.
    `HttpAsyncClientBuilder` | `addResponseInterceptorFirst(org.apache.hc.core5.http.HttpResponseInterceptor interceptor)` Adds this protocol interceptor to the head of the protocol processing list.
    `HttpAsyncClientBuilder` | `addResponseInterceptorLast(org.apache.hc.core5.http.HttpResponseInterceptor interceptor)` Adds this protocol interceptor to the tail of the protocol processing list.
    `CloseableHttpAsyncClient` | `build()`
    `protected Function<org.apache.hc.core5.http.protocol.HttpContext,HttpClientContext>` | `contextAdaptor()`
    `static HttpAsyncClientBuilder` | `create()`
    `protected void` | `customizeExecChain(org.apache.hc.core5.http.config.NamedElementChain<AsyncExecChainHandler> execChainDefinition)` Request exec chain customization and extension.
    `HttpAsyncClientBuilder` | `disableAuthCaching()` Disables authentication scheme caching.
    `HttpAsyncClientBuilder` | `disableAutomaticRetries()` Disables automatic request recovery and re-execution.
    `HttpAsyncClientBuilder` | `disableConnectionState()` Disables connection state tracking.
    `HttpAsyncClientBuilder` | `disableCookieManagement()` Disables state (cookie) management.
    `HttpAsyncClientBuilder` | `disableRedirectHandling()` Disables automatic redirect handling.
    `HttpAsyncClientBuilder` | `evictExpiredConnections()` Makes this instance of HttpClient proactively evict expired connections from the connection pool using a background thread.
    `HttpAsyncClientBuilder` | `evictIdleConnections(org.apache.hc.core5.util.TimeValue maxIdleTime)` Makes this instance of HttpClient proactively evict idle connections from the connection pool using a background thread.
    `HttpAsyncClientBuilder` | `replaceExecInterceptor(String existing, AsyncExecChainHandler interceptor)` Replace an existing interceptor with the given name with new interceptor.
    `HttpAsyncClientBuilder` | `setCharCodingConfig(org.apache.hc.core5.http.config.CharCodingConfig charCodingConfig)` Sets `CharCodingConfig` configuration.
    `HttpAsyncClientBuilder` | `setConnectionManager(AsyncClientConnectionManager connManager)` Sets `AsyncClientConnectionManager` instance.
    `HttpAsyncClientBuilder` | `setConnectionManagerShared(boolean shared)` Defines the connection manager is to be shared by multiple client instances.
    `HttpAsyncClientBuilder` | `setConnectionReuseStrategy(org.apache.hc.core5.http.ConnectionReuseStrategy reuseStrategy)` Sets `ConnectionReuseStrategy` instance.
    `HttpAsyncClientBuilder` | `setDefaultAuthSchemeRegistry(org.apache.hc.core5.http.config.Lookup<AuthSchemeFactory> authSchemeRegistry)` Sets default `AuthScheme` registry which will be used for request execution if not explicitly set in the client execution context.
    `HttpAsyncClientBuilder` | `setDefaultCookieSpecRegistry(org.apache.hc.core5.http.config.Lookup<CookieSpecFactory> cookieSpecRegistry)` Sets default `CookieSpec` registry which will be used for request execution if not explicitly set in the client execution context.
    `HttpAsyncClientBuilder` | `setDefaultCookieStore(CookieStore cookieStore)` Sets default `CookieStore` instance which will be used for request execution if not explicitly set in the client execution context.
    `HttpAsyncClientBuilder` | `setDefaultCredentialsProvider(CredentialsProvider credentialsProvider)` Sets default `CredentialsProvider` instance which will be used for request execution if not explicitly set in the client execution context.
    `HttpAsyncClientBuilder` | `setDefaultHeaders(Collection<? extends org.apache.hc.core5.http.Header> defaultHeaders)` Sets default request header values.
    `HttpAsyncClientBuilder` | `setDefaultRequestConfig(RequestConfig config)` Sets default `RequestConfig` instance which will be used for request execution if not explicitly set in the client execution context.
    `HttpAsyncClientBuilder` | `setH2Config(org.apache.hc.core5.http2.config.H2Config h2Config)` Sets `H2Config` configuration.
    `HttpAsyncClientBuilder` | `setHttp1Config(org.apache.hc.core5.http.config.Http1Config h1Config)` Sets `Http1Config` configuration.
    `HttpAsyncClientBuilder` | `setIOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig ioReactorConfig)` Sets `IOReactorConfig` configuration.
    `HttpAsyncClientBuilder` | `setIoReactorExceptionCallback(org.apache.hc.core5.function.Callback<Exception> ioReactorExceptionCallback)` Sets the callback that will be invoked when the client's IOReactor encounters an uncaught exception.
    `HttpAsyncClientBuilder` | `setIoSessionDecorator(org.apache.hc.core5.function.Decorator<org.apache.hc.core5.reactor.IOSession> ioSessionDecorator)` Sets the `IOSession` `Decorator` that will be use with the client's IOReactor.
    `HttpAsyncClientBuilder` | `setIOSessionListener(org.apache.hc.core5.reactor.IOSessionListener ioSessionListener)` Sets `IOSessionListener` listener.
    `HttpAsyncClientBuilder` | `setKeepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy)` Sets `ConnectionKeepAliveStrategy` instance.
    `HttpAsyncClientBuilder` | `setProxy(org.apache.hc.core5.http.HttpHost proxy)` Sets default proxy value.
    `HttpAsyncClientBuilder` | `setProxyAuthenticationStrategy(AuthenticationStrategy proxyAuthStrategy)` Sets `AuthenticationStrategy` instance for proxy authentication.
    `HttpAsyncClientBuilder` | `setProxySelector(ProxySelector proxySelector)` Sets the `ProxySelector` that will be used to select the proxies to be used for establishing HTTP connections.
    `HttpAsyncClientBuilder` | `setRedirectStrategy(RedirectStrategy redirectStrategy)` Sets `RedirectStrategy` instance.
    `HttpAsyncClientBuilder` | `setRetryStrategy(HttpRequestRetryStrategy retryStrategy)` Sets `HttpRequestRetryStrategy` instance.
    `HttpAsyncClientBuilder` | `setRoutePlanner(HttpRoutePlanner routePlanner)` Sets `HttpRoutePlanner` instance.
    `HttpAsyncClientBuilder` | `setSchemePortResolver(SchemePortResolver schemePortResolver)` Sets `SchemePortResolver` instance.
    `HttpAsyncClientBuilder` | `setTargetAuthenticationStrategy(AuthenticationStrategy targetAuthStrategy)` Sets `AuthenticationStrategy` instance for target host authentication.
    `HttpAsyncClientBuilder` | `setThreadFactory(ThreadFactory threadFactory)` Sets `ThreadFactory` instance.
    `HttpAsyncClientBuilder` | `setUserAgent(String userAgent)` Sets `User-Agent` value.
    `HttpAsyncClientBuilder` | `setUserTokenHandler(UserTokenHandler userTokenHandler)` Sets `UserTokenHandler` instance.
    `HttpAsyncClientBuilder` | `setVersionPolicy(org.apache.hc.core5.http2.HttpVersionPolicy versionPolicy)` Deprecated. Use `TlsConfig` and connection manager methods
    `HttpAsyncClientBuilder` | `useSystemProperties()` Use system properties when creating and configuring default implementations.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### HttpAsyncClientBuilder

          protected HttpAsyncClientBuilder()

  - ### Method Detail

    - #### create

          public static HttpAsyncClientBuilder create()

    - #### setVersionPolicy

          @Deprecated
          public final HttpAsyncClientBuilder setVersionPolicy(org.apache.hc.core5.http2.HttpVersionPolicy versionPolicy)

      Deprecated. Use `TlsConfig` and connection manager methods
      Sets HTTP protocol version policy.
      Returns:
        this instance.

    - #### setHttp1Config

          public final HttpAsyncClientBuilder setHttp1Config(org.apache.hc.core5.http.config.Http1Config h1Config)

      Sets `Http1Config` configuration.
      Returns:
        this instance.

    - #### setH2Config

          public final HttpAsyncClientBuilder setH2Config(org.apache.hc.core5.http2.config.H2Config h2Config)

      Sets `H2Config` configuration.
      Returns:
        this instance.

    - #### setConnectionManager

          public final HttpAsyncClientBuilder setConnectionManager(AsyncClientConnectionManager connManager)

      Sets `AsyncClientConnectionManager` instance.
      Returns:
        this instance.

    - #### setConnectionManagerShared

          public final HttpAsyncClientBuilder setConnectionManagerShared(boolean shared)

      Defines the connection manager is to be shared by multiple client instances.

      If the connection manager is shared its life-cycle is expected to be managed by the caller and it will not be shut down if the client is closed.

      Parameters:
        `shared` - defines whether or not the connection manager can be shared by multiple clients.
      Returns:
        this instance.

    - #### setIOReactorConfig

          public final HttpAsyncClientBuilder setIOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig ioReactorConfig)

      Sets `IOReactorConfig` configuration.
      Returns:
        this instance.

    - #### setIOSessionListener

          public final HttpAsyncClientBuilder setIOSessionListener(org.apache.hc.core5.reactor.IOSessionListener ioSessionListener)

      Sets `IOSessionListener` listener.
      Returns:
        this instance.
      Since:
        5.2

    - #### setIoReactorExceptionCallback

          public final HttpAsyncClientBuilder setIoReactorExceptionCallback(org.apache.hc.core5.function.Callback<Exception> ioReactorExceptionCallback)

      Sets the callback that will be invoked when the client's IOReactor encounters an uncaught exception.
      Returns:
        this instance.
      Since:
        5.1

    - #### setCharCodingConfig

          public final HttpAsyncClientBuilder setCharCodingConfig(org.apache.hc.core5.http.config.CharCodingConfig charCodingConfig)

      Sets `CharCodingConfig` configuration.
      Returns:
        this instance.

    - #### setConnectionReuseStrategy

          public final HttpAsyncClientBuilder setConnectionReuseStrategy(org.apache.hc.core5.http.ConnectionReuseStrategy reuseStrategy)

      Sets `ConnectionReuseStrategy` instance.

      Please note this strategy applies to HTTP/1.0 and HTTP/1.1 connections only

      Returns:
        this instance.

    - #### setKeepAliveStrategy

          public final HttpAsyncClientBuilder setKeepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy)

      Sets `ConnectionKeepAliveStrategy` instance.
      Returns:
        this instance.

    - #### setUserTokenHandler

          public final HttpAsyncClientBuilder setUserTokenHandler(UserTokenHandler userTokenHandler)

      Sets `UserTokenHandler` instance.

      Please note this value can be overridden by the `disableConnectionState()` method.

      Returns:
        this instance.

    - #### setTargetAuthenticationStrategy

          public final HttpAsyncClientBuilder setTargetAuthenticationStrategy(AuthenticationStrategy targetAuthStrategy)

      Sets `AuthenticationStrategy` instance for target host authentication.
      Returns:
        this instance.

    - #### setProxyAuthenticationStrategy

          public final HttpAsyncClientBuilder setProxyAuthenticationStrategy(AuthenticationStrategy proxyAuthStrategy)

      Sets `AuthenticationStrategy` instance for proxy authentication.
      Returns:
        this instance.

    - #### setIoSessionDecorator

          public final HttpAsyncClientBuilder setIoSessionDecorator(org.apache.hc.core5.function.Decorator<org.apache.hc.core5.reactor.IOSession> ioSessionDecorator)

      Sets the `IOSession` `Decorator` that will be use with the client's IOReactor.
      Returns:
        this instance.
      Since:
        5.2

    - #### addResponseInterceptorFirst

          public final HttpAsyncClientBuilder addResponseInterceptorFirst(org.apache.hc.core5.http.HttpResponseInterceptor interceptor)

      Adds this protocol interceptor to the head of the protocol processing list.
      Returns:
        this instance.

    - #### addResponseInterceptorLast

          public final HttpAsyncClientBuilder addResponseInterceptorLast(org.apache.hc.core5.http.HttpResponseInterceptor interceptor)

      Adds this protocol interceptor to the tail of the protocol processing list.
      Returns:
        this instance.

    - #### addExecInterceptorBefore

          public final HttpAsyncClientBuilder addExecInterceptorBefore(String existing,
                                                                       String name,
                                                                       AsyncExecChainHandler interceptor)

      Adds this execution interceptor before an existing interceptor.
      Returns:
        this instance.

    - #### addExecInterceptorAfter

          public final HttpAsyncClientBuilder addExecInterceptorAfter(String existing,
                                                                      String name,
                                                                      AsyncExecChainHandler interceptor)

      Adds this execution interceptor after interceptor with the given name.
      Returns:
        this instance.

    - #### replaceExecInterceptor

          public final HttpAsyncClientBuilder replaceExecInterceptor(String existing,
                                                                     AsyncExecChainHandler interceptor)

      Replace an existing interceptor with the given name with new interceptor.
      Returns:
        this instance.

    - #### addExecInterceptorFirst

          public final HttpAsyncClientBuilder addExecInterceptorFirst(String name,
                                                                      AsyncExecChainHandler interceptor)

      Add an interceptor to the head of the processing list.
      Returns:
        this instance.

    - #### addExecInterceptorLast

          public final HttpAsyncClientBuilder addExecInterceptorLast(String name,
                                                                     AsyncExecChainHandler interceptor)

      Add an interceptor to the tail of the processing list.
      Returns:
        this instance.

    - #### addRequestInterceptorFirst

          public final HttpAsyncClientBuilder addRequestInterceptorFirst(org.apache.hc.core5.http.HttpRequestInterceptor interceptor)

      Adds this protocol interceptor to the head of the protocol processing list.
      Returns:
        this instance.

    - #### addRequestInterceptorLast

          public final HttpAsyncClientBuilder addRequestInterceptorLast(org.apache.hc.core5.http.HttpRequestInterceptor interceptor)

      Adds this protocol interceptor to the tail of the protocol processing list.
      Returns:
        this instance.

    - #### setRetryStrategy

          public final HttpAsyncClientBuilder setRetryStrategy(HttpRequestRetryStrategy retryStrategy)

      Sets `HttpRequestRetryStrategy` instance.

      Please note this value can be overridden by the `disableAutomaticRetries()` method.

      Returns:
        this instance.

    - #### setRedirectStrategy

          public HttpAsyncClientBuilder setRedirectStrategy(RedirectStrategy redirectStrategy)

      Sets `RedirectStrategy` instance.

      Please note this value can be overridden by the `disableRedirectHandling()` method.

      Returns:
        this instance.

    - #### setSchemePortResolver

          public final HttpAsyncClientBuilder setSchemePortResolver(SchemePortResolver schemePortResolver)

      Sets `SchemePortResolver` instance.
      Returns:
        this instance.

    - #### setThreadFactory

          public final HttpAsyncClientBuilder setThreadFactory(ThreadFactory threadFactory)

      Sets `ThreadFactory` instance.
      Returns:
        this instance.

    - #### setUserAgent

          public final HttpAsyncClientBuilder setUserAgent(String userAgent)

      Sets `User-Agent` value.
      Returns:
        this instance.

    - #### setDefaultHeaders

          public final HttpAsyncClientBuilder setDefaultHeaders(Collection<? extends org.apache.hc.core5.http.Header> defaultHeaders)

      Sets default request header values.
      Returns:
        this instance.

    - #### setProxySelector

          public final HttpAsyncClientBuilder setProxySelector(ProxySelector proxySelector)

      Sets the `ProxySelector` that will be used to select the proxies to be used for establishing HTTP connections. If a non-null proxy selector is set, it will take precedence over the proxy settings configured in the client.
      Parameters:
        `proxySelector` - the `ProxySelector` to be used, or null to use the default system proxy selector.
      Returns:
        this instance.

    - #### setProxy

          public final HttpAsyncClientBuilder setProxy(org.apache.hc.core5.http.HttpHost proxy)

      Sets default proxy value.

      Please note this value can be overridden by the `setRoutePlanner( HttpRoutePlanner)` method.

      Returns:
        this instance.

    - #### setRoutePlanner

          public final HttpAsyncClientBuilder setRoutePlanner(HttpRoutePlanner routePlanner)

      Sets `HttpRoutePlanner` instance.
      Returns:
        this instance.

    - #### setDefaultCredentialsProvider

          public final HttpAsyncClientBuilder setDefaultCredentialsProvider(CredentialsProvider credentialsProvider)

      Sets default `CredentialsProvider` instance which will be used for request execution if not explicitly set in the client execution context.
      Returns:
        this instance.

    - #### setDefaultAuthSchemeRegistry

          public final HttpAsyncClientBuilder setDefaultAuthSchemeRegistry(org.apache.hc.core5.http.config.Lookup<AuthSchemeFactory> authSchemeRegistry)

      Sets default `AuthScheme` registry which will be used for request execution if not explicitly set in the client execution context.
      Returns:
        this instance.

    - #### setDefaultCookieSpecRegistry

          public final HttpAsyncClientBuilder setDefaultCookieSpecRegistry(org.apache.hc.core5.http.config.Lookup<CookieSpecFactory> cookieSpecRegistry)

      Sets default `CookieSpec` registry which will be used for request execution if not explicitly set in the client execution context.
      Returns:
        this instance.

    - #### setDefaultCookieStore

          public final HttpAsyncClientBuilder setDefaultCookieStore(CookieStore cookieStore)

      Sets default `CookieStore` instance which will be used for request execution if not explicitly set in the client execution context.
      Returns:
        this instance.

    - #### setDefaultRequestConfig

          public final HttpAsyncClientBuilder setDefaultRequestConfig(RequestConfig config)

      Sets default `RequestConfig` instance which will be used for request execution if not explicitly set in the client execution context.
      Returns:
        this instance.

    - #### useSystemProperties

          public final HttpAsyncClientBuilder useSystemProperties()

      Use system properties when creating and configuring default implementations.
      Returns:
        this instance.

    - #### disableConnectionState

          public final HttpAsyncClientBuilder disableConnectionState()

      Disables connection state tracking.
      Returns:
        this instance.

    - #### disableRedirectHandling

          public final HttpAsyncClientBuilder disableRedirectHandling()

      Disables automatic redirect handling.
      Returns:
        this instance.

    - #### disableAutomaticRetries

          public final HttpAsyncClientBuilder disableAutomaticRetries()

      Disables automatic request recovery and re-execution.
      Returns:
        this instance.

    - #### disableCookieManagement

          public final HttpAsyncClientBuilder disableCookieManagement()

      Disables state (cookie) management.
      Returns:
        this instance.

    - #### disableAuthCaching

          public final HttpAsyncClientBuilder disableAuthCaching()

      Disables authentication scheme caching.
      Returns:
        this instance.

    - #### evictExpiredConnections

          public final HttpAsyncClientBuilder evictExpiredConnections()

      Makes this instance of HttpClient proactively evict expired connections from the connection pool using a background thread.

      One MUST explicitly close HttpClient with `Closeable.close()` in order to stop and release the background thread.

      Please note this method has no effect if the instance of HttpClient is configured to use a shared connection manager.

      Returns:
        this instance.
      See Also:
        `setConnectionManagerShared(boolean)`, `ConnPoolControl.closeExpired()`

    - #### evictIdleConnections

          public final HttpAsyncClientBuilder evictIdleConnections(org.apache.hc.core5.util.TimeValue maxIdleTime)

      Makes this instance of HttpClient proactively evict idle connections from the connection pool using a background thread.

      One MUST explicitly close HttpClient with `Closeable.close()` in order to stop and release the background thread.

      Please note this method has no effect if the instance of HttpClient is configured to use a shared connection manager.

      Parameters:
        `maxIdleTime` - maximum time persistent connections can stay idle while kept alive in the connection pool. Connections whose inactivity period exceeds this value will get closed and evicted from the pool.
      Returns:
        this instance.
      See Also:
        `setConnectionManagerShared(boolean)`, `ConnPoolControl.closeIdle(TimeValue)`

    - #### customizeExecChain

          @Internal
          protected void customizeExecChain(org.apache.hc.core5.http.config.NamedElementChain<AsyncExecChainHandler> execChainDefinition)

      Request exec chain customization and extension.

      For internal use.

    - #### addCloseable

          @Internal
          protected void addCloseable(Closeable closeable)

      Adds to the list of `Closeable` resources to be managed by the client.

      For internal use.

    - #### contextAdaptor

          @Internal
          protected Function<org.apache.hc.core5.http.protocol.HttpContext,HttpClientContext> contextAdaptor()

    - #### build

          public CloseableHttpAsyncClient build()

Copyright © 1999–2021 The Apache Software Foundation. All rights reserved.