Example usage for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager setMaxTotal

List of usage examples for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager setMaxTotal

Introduction

In this page you can find the example usage for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager setMaxTotal.

Prototype

public void setMaxTotal(final int max) 

Source Link

Document

since 4.1

Usage

From source file:cn.keke.travelmix.HttpClientHelper.java

public static HttpClient getNewHttpClient() {
    try {/*from ww w  . j av a2 s.  c o m*/
        SSLSocketFactory sf = new EasySSLSocketFactory();

        // TODO test, if SyncBasicHttpParams is needed
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
        HttpProtocolParams.setUseExpectContinue(params, false);
        HttpProtocolParams.setHttpElementCharset(params, HTTP.UTF_8);
        HttpConnectionParams.setConnectionTimeout(params, 10000);
        HttpConnectionParams.setSocketBufferSize(params, 8192);
        HttpConnectionParams.setLinger(params, 1);
        HttpConnectionParams.setStaleCheckingEnabled(params, false);
        HttpConnectionParams.setSoReuseaddr(params, true);
        HttpConnectionParams.setTcpNoDelay(params, true);
        HttpClientParams.setCookiePolicy(params, CookiePolicy.IGNORE_COOKIES);
        HttpClientParams.setAuthenticating(params, false);
        HttpClientParams.setRedirecting(params, false);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        registry.register(new Scheme("https", 443, sf));

        ThreadSafeClientConnManager ccm = new ThreadSafeClientConnManager(registry, 20, TimeUnit.MINUTES);
        ccm.setMaxTotal(100);
        ccm.setDefaultMaxPerRoute(20);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        LOG.warn("Failed to create custom http client. Default http client is created", e);
        return new DefaultHttpClient();
    }
}

From source file:uk.co.techblue.docusign.client.DocuSignClient.java

private static HttpClient getHttpClient() {
    if (client == null) {
        synchronized (DocuSignClient.class) {
            if (client == null) {
                ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();

                int maxPerRoute = httpClientConfiguration.getDefaultMaxPerRoute();
                cm.setDefaultMaxPerRoute(maxPerRoute);
                cm.setMaxTotal(maxPerRoute);
                client = new DefaultHttpClient(cm);

                int timeout = httpClientConfiguration.getTimeout();
                String proxyHost = httpClientConfiguration.getProxyHost();

                HttpParams params = client.getParams();
                // Allowable time between packets
                HttpConnectionParams.setSoTimeout(params, timeout);
                // Allowable time to get a connection
                HttpConnectionParams.setConnectionTimeout(params, timeout);

                // Configure proxy info if necessary and defined
                if (proxyHost != null && !proxyHost.equals("")) {
                    // Configure the host and port
                    int port = httpClientConfiguration.getProxyPort();
                    HttpHost proxy = new HttpHost(proxyHost, port);

                    params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
                }/*ww w. j a  va  2  s .  c  o  m*/

            }
        }
    }

    if (logger.isDebugEnabled()) {
        logger.info("connections: "
                + ((ThreadSafeClientConnManager) client.getConnectionManager()).getConnectionsInPool());
    }

    return client;
}

From source file:org.sonatype.nexus.proxy.storage.remote.httpclient.HttpClientUtil.java

private static ThreadSafeClientConnManager createConnectionManager() {
    final ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager();

    int connectionPoolSize = SystemPropertiesHelper.getInteger(CONNECTION_POOL_SIZE_KEY, UNDEFINED_POOL_SIZE);
    if (connectionPoolSize != UNDEFINED_POOL_SIZE) {
        connManager.setMaxTotal(connectionPoolSize);
    }//from   w  w w .  ja  v  a 2 s .c om
    // NOTE: connPool is _per_ repo, hence all of those will connect to same host (unless mirrors are used)
    // so, we are violating intentionally the RFC and we let the whole pool size to chase same host
    connManager.setDefaultMaxPerRoute(connManager.getMaxTotal());

    return connManager;
}

From source file:org.eclipse.mylyn.commons.http.HttpUtil.java

private static void configureHttpClientConnectionManager(AbstractHttpClient client) {

    ClientConnectionManager connectionManager = client.getConnectionManager();

    HttpConnectionParams.setConnectionTimeout(client.getParams(), CONNNECT_TIMEOUT);
    HttpConnectionParams.setSoTimeout(client.getParams(), SOCKET_TIMEOUT);

    if (connectionManager instanceof ThreadSafeClientConnManager) {
        ThreadSafeClientConnManager conMgr = (ThreadSafeClientConnManager) connectionManager;
        // FIXME fix connection leaks
        if (TEST_MODE) {
            conMgr.setDefaultMaxPerRoute(2);
        } else {//w w w. j av  a2 s  . com
            conMgr.setDefaultMaxPerRoute(100);
            conMgr.setMaxTotal(1000);
        }
    }
}

From source file:edu.scripps.fl.pubchem.web.session.HttpClientBase.java

public HttpClientBase() {
    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
    cm.setMaxTotal(100);
    client = new DefaultHttpClient(cm);
}

From source file:com.hp.mercury.ci.jenkins.plugins.OOBuildStep.java

private static void initializeHttpClient(DescriptorImpl descriptor) {

    final int maxConnectionsPerRoute = 100;
    final int maxConnectionsTotal = 100;

    ThreadSafeClientConnManager threadSafeClientConnManager = new ThreadSafeClientConnManager();
    threadSafeClientConnManager.setDefaultMaxPerRoute(maxConnectionsPerRoute);
    threadSafeClientConnManager.setMaxTotal(maxConnectionsTotal);

    httpClient = new DefaultHttpClient(threadSafeClientConnManager);
    if (descriptor.isIgnoreSsl()) {
        threadSafeClientConnManager.getSchemeRegistry()
                .register(new Scheme("https", 443, new FakeSocketFactory()));
    } else if (descriptor.getKeystorePath() != null) {
        try {//from w  ww . j ava 2 s. co  m
            SSLSocketFactory sslSocketFactory = sslSocketFactoryFromCertificateFile(
                    descriptor.getKeystorePath(), decrypt(descriptor.getKeystorePassword()).toCharArray());
            sslSocketFactory.setHostnameVerifier(new BrowserCompatHostnameVerifier());
            // For less strict rules in dev mode you can try
            //sslSocketFactory.setHostnameVerifier(new AllowAllHostnameVerifier());
            threadSafeClientConnManager.getSchemeRegistry()
                    .register(new Scheme("https", 443, sslSocketFactory));
        } catch (NoSuchAlgorithmException e) {
            LOG.error("Could not register https scheme: ", e);
        } catch (KeyManagementException e) {
            LOG.error("Could not register https scheme: ", e);
        } catch (KeyStoreException e) {
            LOG.error("Could not register https scheme: ", e);
        } catch (UnrecoverableKeyException e) {
            LOG.error("Could not register https scheme: ", e);
        } catch (IOException e) {
            LOG.error("Could not load keystore file: ", e);
        } catch (CertificateException e) {
            LOG.error("Could not load keystore file: ", e);
        }
    }

    final HttpParams params = httpClient.getParams();
    final int timeoutInSeconds = descriptor.getTimeout() * 1000;
    HttpConnectionParams.setConnectionTimeout(params, timeoutInSeconds);
    HttpConnectionParams.setSoTimeout(params, timeoutInSeconds);
    HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), false);

    for (OOServer s : descriptor.getOoServers(true).values()) {

        URL url = null;
        try {
            url = new URL(s.getUrl());
        } catch (MalformedURLException mue) {
            //can't happen, we pre-validate the URLS during configuration and set active to false if bad.
        }

        //check why it doesn't use the credentials provider
        httpClient.getCredentialsProvider().setCredentials(
                new AuthScope(url.getHost(), url.getPort(), AuthScope.ANY_REALM, "basic"),
                new UsernamePasswordCredentials(s.getUsername(), decrypt(s.getPassword())));
    }

}

From source file:org.obiba.opal.rest.client.magma.OpalClientConnectionManagerFactory.java

@Override
public ClientConnectionManager newInstance(HttpParams params, SchemeRegistry schemeRegistry) {
    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
    cm.setDefaultMaxPerRoute(20);/*w w  w.ja va 2s.c o m*/
    cm.setMaxTotal(20);
    return cm;
}

From source file:com.subgraph.vega.internal.http.requests.HttpRequestEngineFactory.java

private void configureClient(HttpClient client, IHttpRequestEngineConfig config) {
    final ClientConnectionManager connectionManager = client.getConnectionManager();
    if (connectionManager instanceof ThreadSafeClientConnManager) {
        ThreadSafeClientConnManager ccm = (ThreadSafeClientConnManager) connectionManager;
        ccm.setMaxTotal(config.getMaxConnections());
        ccm.setDefaultMaxPerRoute(config.getMaxConnectionsPerRoute());
    }/*ww  w  .j  a v a 2  s .  c o  m*/
}

From source file:com.baidu.oped.apm.profiler.errorTest.ConcurrentCall.java

@Test
public void test() throws IOException, InterruptedException {
    ((ThreadPoolExecutor) executorService).prestartAllCoreThreads();

    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
    cm.setMaxTotal(200);
    cm.setDefaultMaxPerRoute(200);/*  ww  w .j av  a2s  . c  o m*/

    final HttpClient client = new DefaultHttpClient(cm);
    int call = 400;
    final CountDownLatch latch = new CountDownLatch(call);
    for (int i = 0; i < call; i++) {
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    String url = getUrl();
                    logger.info("execute {}", url);
                    final HttpGet httpGet = new HttpGet(url);
                    final HttpResponse execute = client.execute(httpGet);
                    execute.getEntity().getContent().close();

                } catch (ClientProtocolException e) {
                    e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
                } catch (IOException e) {
                    e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
                } finally {
                    latch.countDown();
                }
            }
        });
    }
    latch.await();
    executorService.shutdown();
    cm.shutdown();

}

From source file:com.basho.riak.client.raw.http.HTTPClusterClient.java

@Override
protected RawClient[] fromConfig(ClusterConfig<HTTPClientConfig> clusterConfig) throws IOException {
    List<RawClient> clients = new ArrayList<RawClient>();
    int maxTotal = clusterConfig.getTotalMaximumConnections();

    // IE limitless
    if (maxTotal == ClusterConfig.UNLIMITED_CONNECTIONS) {
        // independent pools, independent clients
        HTTPRiakClientFactory fac = HTTPRiakClientFactory.getInstance();
        for (HTTPClientConfig node : clusterConfig.getClients()) {
            clients.add(fac.newClient(node));
        }/*w ww . j a v  a2 s.c  o  m*/
    } else {
        // create a ThreadSafeClientConnManager to be shared by all the
        // RiakClient instances
        // in the cluster
        // add a route per host and a max per route
        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
        cm.setMaxTotal(maxTotal);

        for (HTTPClientConfig node : clusterConfig.getClients()) {
            if (node.getMaxConnections() != null) {
                cm.setMaxForRoute(makeRoute(node.getUrl()), node.getMaxConnections());
            }
            DefaultHttpClient httpClient = new DefaultHttpClient(cm);

            if (node.getRetryHandler() != null) {
                httpClient.setHttpRequestRetryHandler(node.getRetryHandler());
            }

            RiakConfig riakConfig = new RiakConfig(node.getUrl());
            riakConfig.setMapReducePath(node.getMapreducePath());
            riakConfig.setTimeout(node.getTimeout());
            riakConfig.setHttpClient(httpClient);

            clients.add(new HTTPClientAdapter(new RiakClient(riakConfig)));
        }
    }
    return clients.toArray(new RawClient[clients.size()]);
}