Search in sources :

Example 1 with HttpAsyncClientBuilder

use of org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder in project cxf by apache.

the class AsyncHTTPConduitFactory method setupNIOClient.

public synchronized void setupNIOClient(HTTPClientPolicy clientPolicy) {
    if (client != null) {
        return;
    }
    final IOReactorConfig config = IOReactorConfig.custom().setIoThreadCount(ioThreadCount).setSelectInterval(TimeValue.ofMilliseconds(selectInterval)).setSoLinger(TimeValue.ofMilliseconds(soLinger)).setSoTimeout(Timeout.ofMilliseconds(soTimeout)).setSoKeepAlive(soKeepalive).setTcpNoDelay(tcpNoDelay).build();
    final Registry<TlsStrategy> tlsStrategy = RegistryBuilder.<TlsStrategy>create().register("https", DefaultClientTlsStrategy.getSystemDefault()).build();
    connectionManager = new PoolingAsyncClientConnectionManager(tlsStrategy, PoolConcurrencyPolicy.STRICT, PoolReusePolicy.LIFO, TimeValue.ofMilliseconds(connectionTTL), DefaultSchemePortResolver.INSTANCE, SystemDefaultDnsResolver.INSTANCE);
    connectionManager.setDefaultMaxPerRoute(maxPerRoute);
    connectionManager.setMaxTotal(maxConnections);
    final RedirectStrategy redirectStrategy = new RedirectStrategy() {

        public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
            return false;
        }

        public URI getLocationURI(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
            return null;
        }
    };
    final HttpAsyncClientBuilder httpAsyncClientBuilder = HttpAsyncClients.custom().setConnectionManager(connectionManager).setRedirectStrategy(redirectStrategy).setDefaultCookieStore(new BasicCookieStore() {

        private static final long serialVersionUID = 1L;

        public void addCookie(Cookie cookie) {
        }
    });
    adaptClientBuilder(httpAsyncClientBuilder);
    client = httpAsyncClientBuilder.setIOReactorConfig(config).build();
    // Start the client thread
    client.start();
    // Always start the idle checker thread to validate pending requests and
    // use the ConnectionMaxIdle to close the idle connection
    new CloseIdleConnectionThread(connectionManager, client).start();
}
Also used : TlsStrategy(org.apache.hc.core5.http.nio.ssl.TlsStrategy) DefaultClientTlsStrategy(org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy) HttpRequest(org.apache.hc.core5.http.HttpRequest) Cookie(org.apache.hc.client5.http.cookie.Cookie) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) HttpAsyncClientBuilder(org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) BasicCookieStore(org.apache.hc.client5.http.cookie.BasicCookieStore) PoolingAsyncClientConnectionManager(org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager) RedirectStrategy(org.apache.hc.client5.http.protocol.RedirectStrategy)

Example 2 with HttpAsyncClientBuilder

use of org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder in project bender by Nextdoor.

the class AbstractHttp2TransportFactory method getClientBuilder.

protected HttpAsyncClientBuilder getClientBuilder(boolean useSSL, String url, Map<String, String> stringHeaders) {
    HttpAsyncClientBuilder cb = HttpAsyncClients.custom();
    PoolingAsyncClientConnectionManagerBuilder cmb = PoolingAsyncClientConnectionManagerBuilder.create();
    /*
     * Setup SSL
     */
    if (useSSL) {
        final ClientTlsStrategyBuilder tsb = ClientTlsStrategyBuilder.create().setSslContext(getSSLContext()).setTlsVersions(TLS.V_1_3, TLS.V_1_2).setHostnameVerifier(new HostnameVerifier() {

            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        });
        cmb = cmb.setTlsStrategy(tsb.build());
    }
    /*
     * Add default headers
     */
    ArrayList<BasicHeader> headers = new ArrayList<BasicHeader>(stringHeaders.size());
    stringHeaders.forEach((k, v) -> headers.add(new BasicHeader(k, v)));
    cb = cb.setDefaultHeaders(headers);
    /*
     * Pool concurrency settings
     */
    cmb = cmb.setMaxConnPerRoute(this.config.getThreads()).setMaxConnTotal(this.config.getThreads()).setPoolConcurrencyPolicy(PoolConcurrencyPolicy.STRICT);
    /*
     * Negotiate on HTTP version with the server
     */
    cb = cb.setVersionPolicy(HttpVersionPolicy.NEGOTIATE);
    return cb.setConnectionManager(cmb.build());
}
Also used : PoolingAsyncClientConnectionManagerBuilder(org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder) ClientTlsStrategyBuilder(org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder) SSLSession(javax.net.ssl.SSLSession) ArrayList(java.util.ArrayList) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) HttpAsyncClientBuilder(org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Aggregations

HttpAsyncClientBuilder (org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder)2 ArrayList (java.util.ArrayList)1 HostnameVerifier (javax.net.ssl.HostnameVerifier)1 SSLSession (javax.net.ssl.SSLSession)1 BasicCookieStore (org.apache.hc.client5.http.cookie.BasicCookieStore)1 Cookie (org.apache.hc.client5.http.cookie.Cookie)1 PoolingAsyncClientConnectionManager (org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager)1 PoolingAsyncClientConnectionManagerBuilder (org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder)1 RedirectStrategy (org.apache.hc.client5.http.protocol.RedirectStrategy)1 ClientTlsStrategyBuilder (org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder)1 DefaultClientTlsStrategy (org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy)1 HttpRequest (org.apache.hc.core5.http.HttpRequest)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)1 TlsStrategy (org.apache.hc.core5.http.nio.ssl.TlsStrategy)1 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)1 IOReactorConfig (org.apache.hc.core5.reactor.IOReactorConfig)1