Example usage for org.apache.http.conn.params ConnManagerParams setMaxTotalConnections

List of usage examples for org.apache.http.conn.params ConnManagerParams setMaxTotalConnections

Introduction

In this page you can find the example usage for org.apache.http.conn.params ConnManagerParams setMaxTotalConnections.

Prototype

public static void setMaxTotalConnections(final HttpParams params, final int maxTotalConnections) 

Source Link

Document

Sets the maximum number of connections allowed.

Usage

From source file:httpclient.client.ClientEvictExpiredConnections.java

public static void main(String[] args) throws Exception {
    // Create and initialize HTTP parameters
    HttpParams params = new BasicHttpParams();
    ConnManagerParams.setMaxTotalConnections(params, 100);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

    // Create and initialize scheme registry 
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    HttpClient httpclient = new DefaultHttpClient(cm, params);

    // create an array of URIs to perform GETs on
    String[] urisToGet = { "http://jakarta.apache.org/", "http://jakarta.apache.org/commons/",
            "http://jakarta.apache.org/commons/httpclient/",
            "http://svn.apache.org/viewvc/jakarta/httpcomponents/" };

    IdleConnectionEvictor connEvictor = new IdleConnectionEvictor(cm);
    connEvictor.start();// www .  j a v a 2 s  .  c om

    for (int i = 0; i < urisToGet.length; i++) {
        String requestURI = urisToGet[i];
        HttpGet req = new HttpGet(requestURI);

        System.out.println("executing request " + requestURI);

        HttpResponse rsp = httpclient.execute(req);
        HttpEntity entity = rsp.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(rsp.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
        }
        System.out.println("----------------------------------------");

        if (entity != null) {
            entity.consumeContent();
        }
    }

    // Sleep 10 sec and let the connection evictor do its job
    Thread.sleep(20000);

    // Shut down the evictor thread
    connEvictor.shutdown();
    connEvictor.join();

    // When HttpClient instance is no longer needed, 
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.getConnectionManager().shutdown();
}

From source file:com.hilatest.httpclient.apacheexample.ClientMultiThreadedExecution.java

public static void main(String[] args) throws Exception {
    // Create and initialize HTTP parameters
    HttpParams params = new BasicHttpParams();
    ConnManagerParams.setMaxTotalConnections(params, 100);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

    // Create and initialize scheme registry 
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

    // Create an HttpClient with the ThreadSafeClientConnManager.
    // This connection manager must be used if more than one thread will
    // be using the HttpClient.
    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    HttpClient httpClient = new DefaultHttpClient(cm, params);

    // create an array of URIs to perform GETs on
    String[] urisToGet = { "http://hc.apache.org/", "http://hc.apache.org/httpcomponents-core/",
            "http://hc.apache.org/httpcomponents-client/", "http://svn.apache.org/viewvc/httpcomponents/" };

    // create a thread for each URI
    GetThread[] threads = new GetThread[urisToGet.length];
    for (int i = 0; i < threads.length; i++) {
        HttpGet httpget = new HttpGet(urisToGet[i]);
        threads[i] = new GetThread(httpClient, httpget, i + 1);
    }/* w w w .  j a v a2  s.  com*/

    // start the threads
    for (int j = 0; j < threads.length; j++) {
        threads[j].start();
    }

    // join the threads
    for (int j = 0; j < threads.length; j++) {
        threads[j].join();
    }

    // When HttpClient instance is no longer needed, 
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpClient.getConnectionManager().shutdown();
}

From source file:com.deliciousdroid.client.HttpClientFactory.java

public static HttpClient getThreadSafeClient() {

    HttpParams params = new BasicHttpParams();
    ConnManagerParams.setMaxTotalConnections(params, 100);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

    HttpConnectionParams.setConnectionTimeout(params, REGISTRATION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, REGISTRATION_TIMEOUT);
    ConnManagerParams.setTimeout(params, REGISTRATION_TIMEOUT);

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

    ClientConnectionManager mgr = new ThreadSafeClientConnManager(params, schemeRegistry);
    HttpClient client = new DefaultHttpClient(mgr, params);

    return client;
}

From source file:cc.nickmo.easyhttp.EasyHttp.java

public EasyHttp(SchemeRegistry schemeRegistry) {
    if (httpConf == null)
        httpConf = new EasyHttpOptions();
    BasicHttpParams httpParams = new BasicHttpParams();

    ConnManagerParams.setTimeout(httpParams, httpConf.getTimeout());
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(httpConf.getMaxConnection()));
    ConnManagerParams.setMaxTotalConnections(httpParams, 10);
}

From source file:net.ecfirm.ec.ec1.net.EcNetHelper.java

public static DefaultHttpClient getThreadSafeClient() {
    DefaultHttpClient client = new DefaultHttpClient();
    ClientConnectionManager mgr = client.getConnectionManager();
    //////////from w w  w.  j  a va 2  s.c om
    client.getParams().setParameter("http.protocol.expect-continue", false);
    client.getParams().setParameter("http.connection.timeout", EcConst.NET_HTTP_CONN_TIMEOUT);
    //client.getParams().setParameter("http.socket.timeout", );
    ////////
    HttpParams params = client.getParams();
    ////////
    ConnManagerParams.setMaxTotalConnections(params, EcConst.NET_HTTP_CONN);
    ConnPerRouteBean connPerRoute = new ConnPerRouteBean(EcConst.NET_HTTP_CONN_PER_ROUTE);
    HttpHost localhost = new HttpHost("localhost", 80);
    connPerRoute.setMaxForRoute(new HttpRoute(localhost), EcConst.NET_HTTP_CONN_PER_ROUTE);
    ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
    mgr.getSchemeRegistry().register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    mgr.getSchemeRegistry().register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ////////
    client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()), params);
    return client;
}

From source file:org.codegist.crest.io.http.HttpClientFactory.java

public static HttpClient create(CRestConfig crestConfig, Class<?> source) {
    HttpClient httpClient = crestConfig.get(source.getName() + HTTP_CLIENT);
    if (httpClient != null) {
        return httpClient;
    }/*from  w  w  w  .  j a  va  2  s  .com*/

    int concurrencyLevel = crestConfig.getConcurrencyLevel();
    if (concurrencyLevel > 1) {
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(concurrencyLevel));
        ConnManagerParams.setMaxTotalConnections(params, concurrencyLevel);

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), HTTP_PORT));
        schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), HTTPS_PORT));

        ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
        httpClient = new DefaultHttpClient(cm, params);
    } else {
        httpClient = new DefaultHttpClient();
    }
    ((DefaultHttpClient) httpClient).setRoutePlanner(new ProxySelectorRoutePlanner(
            httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault()));
    return httpClient;
}

From source file:it.av.youeat.web.pubsubhubbub.Publisher.java

/**
 * Constructor/*from   www.  ja v  a  2s . c o  m*/
 */
public Publisher() {
    HttpParams params = new BasicHttpParams();
    ConnManagerParams.setMaxTotalConnections(params, 200);
    ConnPerRouteBean connPerRoute = new ConnPerRouteBean(20);
    connPerRoute.setDefaultMaxPerRoute(50);
    ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    httpClient = new DefaultHttpClient(cm, params);

    httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {

        public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
            HeaderElementIterator it = new BasicHeaderElementIterator(
                    response.headerIterator(HTTP.CONN_KEEP_ALIVE));
            while (it.hasNext()) {
                HeaderElement he = it.nextElement();
                String param = he.getName();
                String value = he.getValue();
                if (value != null && param.equalsIgnoreCase("timeout")) {
                    try {
                        return Long.parseLong(value) * 1000;
                    } catch (NumberFormatException ignore) {
                    }
                }
            }
            // default keepalive is 60 seconds. This is higher than usual
            // since the number of hubs it should be talking to should be
            // small
            return 30 * 1000;
        }
    });
}

From source file:ro.zg.netcell.connectors.HttpConnectionManager.java

private void initHttpClient() {
    ConfigurationData cfgData = dataSourceDefinition.getConfigData();

    HttpParams params = new BasicHttpParams();
    ConnManagerParams.setMaxTotalConnections(params, Integer
            .parseInt(cfgData.getParameterValue(DataSourceConfigParameters.MAX_TOTAL_CONNECTIONS).toString()));
    ConnPerRouteBean connPerRoute = new ConnPerRouteBean(10);
    HttpHost localhost = new HttpHost("locahost", 80);
    connPerRoute.setMaxForRoute(new HttpRoute(localhost), 50);
    ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
    ConnManagerParams.setTimeout(params, Long
            .parseLong(cfgData.getParameterValue(DataSourceConfigParameters.CONNECTION_TIMEOUT).toString()));

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

    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);

    /* set config params */
    ConfigurationData configData = dataSourceDefinition.getConfigData();
    Map<String, UserInputParameter> userInputParams = configData.getUserInputParams();
    for (UserInputParameter uip : userInputParams.values()) {
        params.setParameter(uip.getInnerName(), uip.getValue());
    }//from   www  .  j  av a2s. c o m

    HttpConnectionParams.setSoTimeout(params, 25000);
    httpClient = new DefaultHttpClient(cm, params);

}

From source file:com.google.appinventor.components.runtime.util.WebServiceUtil.java

/**
 * Returns the one <code>WebServiceUtil</code> instance
 * @return the one <code>WebServiceUtil</code> instance
 *///from   ww w.j ava2  s .  c  o  m
public static WebServiceUtil getInstance() {
    // This needs to be here instead of in the constructor because
    // it uses classes that are in the AndroidSDK and thus would
    // cause Stub! errors when running the component descriptor.
    synchronized (httpClientSynchronizer) {
        if (httpClient == null) {
            SchemeRegistry schemeRegistry = new SchemeRegistry();
            schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
            BasicHttpParams params = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
            HttpConnectionParams.setSoTimeout(params, 20 * 1000);
            ConnManagerParams.setMaxTotalConnections(params, 20);
            ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);
            WebServiceUtil.httpClient = new DefaultHttpClient(manager, params);
        }
    }
    return INSTANCE;
}

From source file:Main.java

public static DefaultHttpClient getThreadSafeClient() {
    if (httpclient != null)
        return (DefaultHttpClient) httpclient;
    HttpParams params = new BasicHttpParams();
    ConnManagerParams.setMaxTotalConnections(params, 100);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

    // Create and initialize scheme registry 
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    httpclient = new DefaultHttpClient(cm, params);

    return (DefaultHttpClient) httpclient;
}