Example usage for org.apache.http.impl.nio.client HttpAsyncClientBuilder setConnectionManager

List of usage examples for org.apache.http.impl.nio.client HttpAsyncClientBuilder setConnectionManager

Introduction

In this page you can find the example usage for org.apache.http.impl.nio.client HttpAsyncClientBuilder setConnectionManager.

Prototype

public final HttpAsyncClientBuilder setConnectionManager(final NHttpClientConnectionManager connManager) 

Source Link

Document

Assigns NHttpClientConnectionManager instance.

Usage

From source file:org.apache.zeppelin.notebook.repo.zeppelinhub.rest.HttpProxyClient.java

private CloseableHttpAsyncClient getAsyncProxyHttpClient(URI proxyUri) {
    LOG.info("Creating async proxy http client");
    PoolingNHttpClientConnectionManager cm = getAsyncConnectionManager();
    HttpHost proxy = new HttpHost(proxyUri.getHost(), proxyUri.getPort());

    HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom();
    if (cm != null) {
        clientBuilder = clientBuilder.setConnectionManager(cm);
    }//www.j ava2 s.co  m

    if (proxy != null) {
        clientBuilder = clientBuilder.setProxy(proxy);
    }
    clientBuilder = setRedirects(clientBuilder);
    return clientBuilder.build();
}

From source file:co.paralleluniverse.fibers.dropwizard.FiberHttpClientBuilder.java

/**
 * Builds the {@link HttpClient}./*from  w w  w .  j av a2 s . c o  m*/
 *
 * @return an {@link HttpClient}
 */
public HttpClient build(String name) {
    RequestConfig createHttpParams = createHttpParams();
    final NHttpClientConnectionManager manager = createConnectionManager(registry, name);
    HttpAsyncClientBuilder clientBuilder = new InstrumentedNHttpClientBuilder(metricRegistry, name);
    clientBuilder.setConnectionManager(manager);
    clientBuilder.setDefaultRequestConfig(createHttpParams);
    setStrategiesForClient(clientBuilder);
    CloseableHttpAsyncClient client = clientBuilder.build();
    client.start();
    return new FiberHttpClient(client, getRetryHandler());
}

From source file:org.apache.gobblin.http.ApacheHttpAsyncClient.java

public ApacheHttpAsyncClient(HttpAsyncClientBuilder builder, Config config,
        SharedResourcesBroker<GobblinScopeTypes> broker) {
    super(broker, HttpUtils.createApacheHttpClientLimiterKey(config));
    config = config.withFallback(FALLBACK);

    RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT)
            .setSocketTimeout(config.getInt(REQUEST_TIME_OUT_MS_KEY))
            .setConnectTimeout(config.getInt(CONNECTION_TIME_OUT_MS_KEY))
            .setConnectionRequestTimeout(config.getInt(CONNECTION_TIME_OUT_MS_KEY)).build();

    try {//from  w w  w .ja va2  s.  c o  m
        builder.disableCookieManagement().useSystemProperties().setDefaultRequestConfig(requestConfig);
        builder.setConnectionManager(getNHttpConnManager(config));
        client = builder.build();
        client.start();
    } catch (IOException e) {
        throw new RuntimeException("ApacheHttpAsyncClient cannot be initialized");
    }
}

From source file:ee.ria.xroad.proxy.testsuite.MessageTestCase.java

protected CloseableHttpAsyncClient getClient() throws Exception {
    HttpAsyncClientBuilder builder = HttpAsyncClients.custom();

    IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
            .setIoThreadCount(Runtime.getRuntime().availableProcessors()).setConnectTimeout(getClientTimeout())
            .setSoTimeout(30000).build();

    ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);

    PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);

    connManager.setMaxTotal(1);//from   ww w .  ja  v a2s .c o  m
    connManager.setDefaultMaxPerRoute(1);

    builder.setConnectionManager(connManager);

    return builder.build();
}

From source file:com.liferay.petra.json.web.service.client.BaseJSONWebServiceClientImpl.java

public void afterPropertiesSet() throws IOReactorException {
    HttpAsyncClientBuilder httpAsyncClientBuilder = HttpAsyncClients.custom();

    httpAsyncClientBuilder = httpAsyncClientBuilder.useSystemProperties();

    NHttpClientConnectionManager nHttpClientConnectionManager = getPoolingNHttpClientConnectionManager();

    httpAsyncClientBuilder.setConnectionManager(nHttpClientConnectionManager);

    httpAsyncClientBuilder.setDefaultCredentialsProvider(_getCredentialsProvider());

    setProxyHost(httpAsyncClientBuilder);

    try {/*from w w w.  j av a  2 s .co m*/
        _closeableHttpAsyncClient = httpAsyncClientBuilder.build();

        _closeableHttpAsyncClient.start();

        _idleConnectionMonitorThread = new IdleConnectionMonitorThread(nHttpClientConnectionManager);

        _idleConnectionMonitorThread.start();

        if (_logger.isDebugEnabled()) {
            StringBuilder sb = new StringBuilder();

            sb.append("Configured client for ");
            sb.append(_protocol);
            sb.append("://");
            sb.append(_hostName);
            sb.append(":");
            sb.append(_hostPort);

            _logger.debug(sb.toString());
        }
    } catch (Exception e) {
        _logger.error("Unable to configure client", e);
    }
}

From source file:net.tirasa.wink.client.asynchttpclient.ApacheHttpAsyncClientConnectionHandler.java

private synchronized CloseableHttpAsyncClient openConnection(ClientRequest request)
        throws NoSuchAlgorithmException, KeyManagementException, IOException {

    if (this.httpclient != null) {
        return this.httpclient;
    }//w  w w  .ja  va  2s.c o m

    HttpAsyncClientBuilder clientBuilder = HttpAsyncClientBuilder.create();

    // cast is safe because we're on the client
    ApacheHttpAsyncClientConfig config = (ApacheHttpAsyncClientConfig) request
            .getAttribute(WinkConfiguration.class);

    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
            .setConnectTimeout(config.getConnectTimeout()).setSocketTimeout(config.getReadTimeout());
    if (config.isFollowRedirects()) {
        requestConfigBuilder.setRedirectsEnabled(true).setCircularRedirectsAllowed(true);
    }

    // setup proxy
    if (config.getProxyHost() != null) {
        requestConfigBuilder.setProxy(new HttpHost(config.getProxyHost(), config.getProxyPort()));
    }

    clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());

    Registry<SchemeIOSessionFactory> connManagerRegistry;
    if (config.getBypassHostnameVerification()) {
        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, null, null);

        connManagerRegistry = RegistryBuilder.<SchemeIOSessionFactory>create()
                .register("http", PlainIOSessionFactory.INSTANCE)
                .register("https", new SSLIOSessionFactory(sslcontext, new X509HostnameVerifier() {

                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }

                    @Override
                    public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
                    }

                    @Override
                    public void verify(String host, X509Certificate cert) throws SSLException {
                    }

                    @Override
                    public void verify(String host, SSLSocket ssl) throws IOException {
                    }
                })).build();
    } else {
        connManagerRegistry = RegistryBuilder.<SchemeIOSessionFactory>create()
                .register("http", PlainIOSessionFactory.INSTANCE)
                .register("https", SSLIOSessionFactory.getDefaultStrategy()).build();
    }

    PoolingNHttpClientConnectionManager httpConnectionManager = new PoolingNHttpClientConnectionManager(
            new DefaultConnectingIOReactor(IOReactorConfig.DEFAULT), connManagerRegistry);
    if (config.getMaxPooledConnections() > 0) {
        httpConnectionManager.setMaxTotal(config.getMaxPooledConnections());
        httpConnectionManager.setDefaultMaxPerRoute(config.getMaxPooledConnections());

    }
    clientBuilder.setConnectionManager(httpConnectionManager);

    this.httpclient = clientBuilder.build();
    this.httpclient.start();

    return this.httpclient;
}