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

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

Introduction

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

Prototype

public void setDefaultMaxPerRoute(final int max) 

Source Link

Usage

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);
                }//from   w  w  w .ja  v a 2s.  co m

            }
        }
    }

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

    return client;
}

From source file:biocode.fims.ezid.EzidService.java

/**
 * Generate an HTTP Client for communicating with web services that is
 * thread safe and can be used in the context of a multi-threaded application.
 *
 * @return DefaultHttpClient//ww  w.j  a  v a  2s  .c o m
 */
private static DefaultHttpClient createThreadSafeClient() {
    DefaultHttpClient client = new DefaultHttpClient();
    ClientConnectionManager mgr = client.getConnectionManager();
    HttpParams params = client.getParams();
    ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(mgr.getSchemeRegistry());
    connManager.setDefaultMaxPerRoute(CONNECTIONS_PER_ROUTE);
    client = new DefaultHttpClient(connManager, params);
    return client;
}

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 {/* www .jav  a  2  s  .c  om*/
            conMgr.setDefaultMaxPerRoute(100);
            conMgr.setMaxTotal(1000);
        }
    }
}

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  www . j  a v a2s .  co m
    // 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: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  w w  .  j a v a 2 s. com
            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);
    cm.setMaxTotal(20);/*from w w w . j  ava  2  s. c om*/
    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());
    }//from  w  w 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);/*from   w  w  w.j  a v  a2 s .  c o  m*/
    cm.setDefaultMaxPerRoute(200);

    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.netflix.http4.NFHttpClientTest.java

@Test
public void testNFHttpClient() throws Exception {
    NFHttpClient client = NFHttpClientFactory.getNFHttpClient("www.google.com", 80);
    ThreadSafeClientConnManager cm = (ThreadSafeClientConnManager) client.getConnectionManager();
    cm.setDefaultMaxPerRoute(10);
    HttpGet get = new HttpGet("www.google.com");
    ResponseHandler<Integer> respHandler = new ResponseHandler<Integer>() {
        public Integer handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            HttpEntity entity = response.getEntity();
            String contentStr = EntityUtils.toString(entity);
            return contentStr.length();
        }//ww  w  .ja  v  a2 s .c  o  m
    };
    long contentLen = client.execute(get, respHandler);
    assertTrue(contentLen > 0);
}

From source file:org.lilyproject.testclientfw.BaseRepositoryTestTool.java

public void setupSolr() throws MalformedURLException {
    if (useSolrCloud) {
        System.out.println("Using SolrCloud instance with ZooKeeper " + solrUrl);
        solrServer = new CloudSolrServer(solrUrl);
    } else {/*from w  ww  .  jav a  2  s . com*/
        System.out.println("Using Solr instance at " + solrUrl);

        ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager();
        connectionManager.setDefaultMaxPerRoute(5);
        connectionManager.setMaxTotal(50);
        HttpClient httpClient = new DefaultHttpClient(connectionManager);

        solrServer = new HttpSolrServer(solrUrl, httpClient);
    }
}