Search in sources :

Example 6 with PoolingNHttpClientConnectionManager

use of org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager in project tutorials by eugenp.

the class HttpAsyncClientLiveTest method whenUseMultipleHttpAsyncClient_thenCorrect.

@Test
public void whenUseMultipleHttpAsyncClient_thenCorrect() throws Exception {
    final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
    final PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(ioReactor);
    final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setConnectionManager(cm).build();
    client.start();
    final String[] toGet = { "http://www.google.com/", "http://www.apache.org/", "http://www.bing.com/" };
    final GetThread[] threads = new GetThread[toGet.length];
    for (int i = 0; i < threads.length; i++) {
        final HttpGet request = new HttpGet(toGet[i]);
        threads[i] = new GetThread(client, request);
    }
    for (final GetThread thread : threads) {
        thread.start();
    }
    for (final GetThread thread : threads) {
        thread.join();
    }
}
Also used : ConnectingIOReactor(org.apache.http.nio.reactor.ConnectingIOReactor) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) PoolingNHttpClientConnectionManager(org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager) Test(org.junit.Test)

Example 7 with PoolingNHttpClientConnectionManager

use of org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager in project SSM by Intel-bigdata.

the class HttpProxyClient method getAsyncConnectionManager.

private PoolingNHttpClientConnectionManager getAsyncConnectionManager() {
    ConnectingIOReactor ioReactor = null;
    PoolingNHttpClientConnectionManager cm = null;
    try {
        ioReactor = new DefaultConnectingIOReactor();
        // ssl setup
        SSLContext sslcontext = SSLContexts.createSystemDefault();
        X509HostnameVerifier hostnameVerifier = new BrowserCompatHostnameVerifier();
        @SuppressWarnings("deprecation") Registry<SchemeIOSessionStrategy> sessionStrategyRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create().register("http", NoopIOSessionStrategy.INSTANCE).register("https", new SSLIOSessionStrategy(sslcontext, hostnameVerifier)).build();
        cm = new PoolingNHttpClientConnectionManager(ioReactor, sessionStrategyRegistry);
    } catch (IOReactorException e) {
        LOG.error("Couldn't initialize multi-threaded async client ", e);
        return null;
    }
    return cm;
}
Also used : ConnectingIOReactor(org.apache.http.nio.reactor.ConnectingIOReactor) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) IOReactorException(org.apache.http.nio.reactor.IOReactorException) X509HostnameVerifier(org.apache.http.conn.ssl.X509HostnameVerifier) SchemeIOSessionStrategy(org.apache.http.nio.conn.SchemeIOSessionStrategy) BrowserCompatHostnameVerifier(org.apache.http.conn.ssl.BrowserCompatHostnameVerifier) SSLIOSessionStrategy(org.apache.http.nio.conn.ssl.SSLIOSessionStrategy) SSLContext(javax.net.ssl.SSLContext) PoolingNHttpClientConnectionManager(org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager)

Example 8 with PoolingNHttpClientConnectionManager

use of org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager in project SSM by Intel-bigdata.

the class HttpProxyClient method getAsyncProxyHttpClient.

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);
    }
    if (proxy != null) {
        clientBuilder = clientBuilder.setProxy(proxy);
    }
    clientBuilder = setRedirects(clientBuilder);
    return clientBuilder.build();
}
Also used : HttpHost(org.apache.http.HttpHost) PoolingNHttpClientConnectionManager(org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder)

Example 9 with PoolingNHttpClientConnectionManager

use of org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager in project incubator-gobblin by apache.

the class ApacheHttpAsyncClient method getNHttpConnManager.

private NHttpClientConnectionManager getNHttpConnManager(Config config) throws IOException {
    NHttpClientConnectionManager httpConnManager;
    String connMgrStr = config.getString(HTTP_CONN_MANAGER);
    switch(ApacheHttpClient.ConnManager.valueOf(connMgrStr.toUpperCase())) {
        case POOLING:
            ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
            PoolingNHttpClientConnectionManager poolingConnMgr = new PoolingNHttpClientConnectionManager(ioReactor);
            poolingConnMgr.setMaxTotal(config.getInt(POOLING_CONN_MANAGER_MAX_TOTAL_CONN));
            poolingConnMgr.setDefaultMaxPerRoute(config.getInt(POOLING_CONN_MANAGER_MAX_PER_CONN));
            httpConnManager = poolingConnMgr;
            break;
        default:
            throw new IllegalArgumentException(connMgrStr + " is not supported");
    }
    LOG.info("Using " + httpConnManager.getClass().getSimpleName());
    return httpConnManager;
}
Also used : ConnectingIOReactor(org.apache.http.nio.reactor.ConnectingIOReactor) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) PoolingNHttpClientConnectionManager(org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager) NHttpClientConnectionManager(org.apache.http.nio.conn.NHttpClientConnectionManager) PoolingNHttpClientConnectionManager(org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager)

Aggregations

PoolingNHttpClientConnectionManager (org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager)9 DefaultConnectingIOReactor (org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor)8 ConnectingIOReactor (org.apache.http.nio.reactor.ConnectingIOReactor)7 IOReactorConfig (org.apache.http.impl.nio.reactor.IOReactorConfig)4 SchemeIOSessionStrategy (org.apache.http.nio.conn.SchemeIOSessionStrategy)4 SSLContext (javax.net.ssl.SSLContext)3 RequestConfig (org.apache.http.client.config.RequestConfig)3 ConnectionConfig (org.apache.http.config.ConnectionConfig)3 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)3 SSLIOSessionStrategy (org.apache.http.nio.conn.ssl.SSLIOSessionStrategy)3 HttpHost (org.apache.http.HttpHost)2 HttpRequest (org.apache.http.HttpRequest)2 HttpResponse (org.apache.http.HttpResponse)2 HttpAsyncClientBuilder (org.apache.http.impl.nio.client.HttpAsyncClientBuilder)2 ManagedNHttpClientConnectionFactory (org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionFactory)2 InetAddress (java.net.InetAddress)1 URL (java.net.URL)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 HostnameVerifier (javax.net.ssl.HostnameVerifier)1