Example usage for org.apache.http.impl.conn PoolingClientConnectionManager setMaxTotal

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

Introduction

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

Prototype

public void setMaxTotal(final int max) 

Source Link

Usage

From source file:bundestagswahl.benchmark.BWBenchmark.java

public static void main(String[] args) throws Exception {

    resultTime = new double[6];

    Scanner scanner = new Scanner(System.in);
    System.out.print("Please enter URL: ");
    serverUrl = scanner.nextLine();//from ww  w . j  ava2s  . c om
    System.out.print("Please enter number of terminals: ");
    numberTerminals = scanner.nextInt();
    System.out.print("Please enter number of requests: ");
    numberRequests = scanner.nextInt();
    System.out.print("Please enter delay between two requests in seconds: ");
    requestDelay = scanner.nextDouble();
    scanner.close();

    PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
    cm.setMaxTotal(numberTerminals);
    HttpClient httpclient = new DefaultHttpClient(cm);
    httpclient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 300000)
            .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 300000)
            .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true);
    try {
        final CountDownLatch latch = new CountDownLatch(numberTerminals);
        for (int i = 0; i < numberTerminals; i++) {
            BenchmarkTerminal terminal = new BenchmarkTerminal(httpclient, latch, serverUrl, numberRequests,
                    requestDelay);
            terminal.start();
        }
        latch.await();
        httpclient.getConnectionManager().shutdown();
    } finally {

    }

    printResultTimes();
    System.out.println(" ");
    System.out.println("Done");
}

From source file:com.dlmu.heipacker.crawler.client.ClientMultiThreadedExecution.java

public static void main(String[] args) throws Exception {
    // Create an HttpClient with the ThreadSafeClientConnManager.
    // This connection manager must be used if more than one thread will
    // be using the HttpClient.
    PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
    cm.setMaxTotal(100);

    HttpClient httpclient = new DefaultHttpClient(cm);
    try {/* www.  j a  v  a2  s.  com*/
        // create an array of URIs to perform GETs on
        String[] urisToGet = { "http://hc.apache.org/", "http://hc.apache.org/httpcomponents-core-ga/",
                "http://hc.apache.org/httpcomponents-client-ga/",
                "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);
        }

        // 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();
        }

    } finally {
        // 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.dlmu.heipacker.crawler.client.ClientEvictExpiredConnections.java

public static void main(String[] args) throws Exception {
    PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
    cm.setMaxTotal(100);

    HttpClient httpclient = new DefaultHttpClient(cm);
    try {//from   w  ww  .j ava2 s.co  m
        // 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();

        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("----------------------------------------");

            EntityUtils.consume(entity);
        }

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

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

    } finally {
        // 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:org.n52.server.util.SosAdapterFactory.java

private static HttpClient createHttpClient(SOSMetadata metadata) {
    PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
    cm.setMaxTotal(metadata.getHttpConnectionPoolSize());

    int timeout = metadata.getTimeout();
    SimpleHttpClient simpleClient = new SimpleHttpClient(timeout, timeout, cm);
    return new GzipEnabledHttpClient(new ProxyAwareHttpClient(simpleClient));
}

From source file:cn.clxy.upload.ApacheHCUploader.java

/**
 * The timeout should be adjusted by network condition.
 * @return//from  w w w.  j  av  a2s. c o m
 */
private static HttpClient createClient() {

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

    PoolingClientConnectionManager ccm = new PoolingClientConnectionManager(schReg);
    ccm.setMaxTotal(Config.maxUpload);

    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 10 * 1000);
    HttpConnectionParams.setSoTimeout(params, Config.timeOut);

    return new DefaultHttpClient(ccm, params);
}

From source file:cn.clxy.codes.upload.ApacheHCUploader.java

/**
 * The timeout should be adjusted by network condition.
 * @return/*from  www  .  ja  v  a2s. co m*/
 */
private static HttpClient createClient() {

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

    PoolingClientConnectionManager ccm = new PoolingClientConnectionManager(schReg);
    ccm.setMaxTotal(Config.MAX_UPLOAD);

    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 10 * 1000);
    HttpConnectionParams.setSoTimeout(params, Config.PART_UPLOAD_TIMEOUT);

    return new DefaultHttpClient(ccm, params);
}

From source file:com.sinacloud.scs.http.ConnectionManagerFactory.java

public static PoolingClientConnectionManager createPoolingClientConnManager(ClientConfiguration config,
        HttpParams httpClientParams) {//from   w ww  .j a  v a2s  .  c o  m
    PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnections());
    connectionManager.setMaxTotal(config.getMaxConnections());
    if (config.useReaper()) {
        IdleConnectionReaper.registerConnectionManager(connectionManager);
    }
    return connectionManager;
}

From source file:org.ow2.proactive_grid_cloud_portal.common.server.HttpUtils.java

public static DefaultHttpClient createDefaultExecutor() {
    PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
    cm.setDefaultMaxPerRoute(50);/* w ww  . ja va 2 s .c  o  m*/
    cm.setMaxTotal(50);
    DefaultHttpClient httpClient = new DefaultHttpClient(cm);

    try {
        SSLSocketFactory socketFactory = new SSLSocketFactory(new RelaxedTrustStrategy(),
                SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        Scheme https = new Scheme("https", 443, socketFactory);
        httpClient.getConnectionManager().getSchemeRegistry().register(https);
    } catch (Exception ignored) {
    }

    return httpClient;
}

From source file:com.nebkat.junglist.bot.http.ConnectionManager.java

public static HttpClient getHttpClient() {
    if (sHttpClient == null) {
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        schemeRegistry.register(new Scheme("https", 443, new EasySSLSocketFactory()));

        HttpParams params = new BasicHttpParams();
        params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

        PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(schemeRegistry);
        connectionManager.setDefaultMaxPerRoute(30);
        connectionManager.setMaxTotal(30);

        sHttpClient = new DefaultHttpClient(connectionManager, params);

        ((DefaultHttpClient) sHttpClient).addResponseInterceptor((response, context) -> {
            if (response.containsHeader("Location")) {
                context.setAttribute(REDIRECTED, true);
            }//ww w.j  av  a 2s. c o  m
        });
    }
    return sHttpClient;
}

From source file:org.nuxeo.scim.server.tests.ScimServerTest.java

protected static ClientConfig createHttpBasicClientConfig(final String userName, final String password) {

    final HttpParams params = new BasicHttpParams();
    DefaultHttpClient.setDefaultHttpParams(params);
    params.setBooleanParameter(CoreConnectionPNames.SO_REUSEADDR, true);
    params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, true);
    params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true);

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

    final PoolingClientConnectionManager mgr = new PoolingClientConnectionManager(schemeRegistry);
    mgr.setMaxTotal(200);
    mgr.setDefaultMaxPerRoute(20);//  ww  w  .  j  a v a2  s  . c om

    final DefaultHttpClient httpClient = new DefaultHttpClient(mgr, params);

    final Credentials credentials = new UsernamePasswordCredentials(userName, password);
    httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials);
    httpClient.addRequestInterceptor(new PreemptiveAuthInterceptor(), 0);

    ClientConfig clientConfig = new ApacheHttpClientConfig(httpClient);
    clientConfig.setBypassHostnameVerification(true);

    return clientConfig;
}