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:org.apache.stratos.integration.tests.rest.RestClient.java

public RestClient() {
    PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
    // Increase max total connection to 200
    cm.setMaxTotal(200);//from   w w w . java  2  s.  c  om
    // Increase default max connection per route to 50
    cm.setDefaultMaxPerRoute(50);

    httpClient = new DefaultHttpClient(cm);
    httpClient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpClient);
}

From source file:com.alibaba.dubbo.rpc.protocol.resteasy.RestProtocol.java

protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
    if (connectionMonitor == null) {
        connectionMonitor = new ConnectionMonitor();
    }/*from  w ww  . j  a v  a  2 s  .  c om*/

    // TODO more configs to add

    PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
    // 20 is the default maxTotal of current PoolingClientConnectionManager
    connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, 20));
    connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, 20));

    connectionMonitor.addConnectionManager(connectionManager);

    // BasicHttpContext localContext = new BasicHttpContext();

    DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);

    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")) {
                    return Long.parseLong(value) * 1000;
                }
            }
            // TODO constant
            return 30 * 1000;
        }
    });

    HttpParams params = httpClient.getParams();
    // TODO currently no xml config for Constants.CONNECT_TIMEOUT_KEY so we directly reuse Constants.TIMEOUT_KEY for now
    HttpConnectionParams.setConnectionTimeout(params,
            url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
    HttpConnectionParams.setSoTimeout(params,
            url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
    HttpConnectionParams.setTcpNoDelay(params, true);
    HttpConnectionParams.setSoKeepalive(params, true);

    ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient/* , localContext */);

    ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
    clients.add(client);

    client.register(RpcContextFilter.class);
    for (String clazz : Constants.COMMA_SPLIT_PATTERN.split(url.getParameter(Constants.EXTENSION_KEY, ""))) {
        if (!StringUtils.isEmpty(clazz)) {
            try {
                client.register(Thread.currentThread().getContextClassLoader().loadClass(clazz.trim()));
            } catch (ClassNotFoundException e) {
                throw new RpcException("Error loading JAX-RS extension class: " + clazz.trim(), e);
            }
        }
    }

    // dubbo ?
    String version = url.getParameter(Constants.VERSION_KEY);
    String versionPath = "";
    if (StringUtils.isNotEmpty(version)) {
        versionPath = version + "/";
    }
    // TODO protocol
    ResteasyWebTarget target = client
            .target("http://" + url.getHost() + ":" + url.getPort() + "/" + versionPath + getContextPath(url));
    return target.proxy(serviceType);
}

From source file:org.string_db.psicquic.ws.StringdbSolrBasedPsicquicRestService.java

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

    PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
    cm.setMaxTotal(maxTotalConnections);
    cm.setDefaultMaxPerRoute(defaultMaxConnectionsPerHost);

    HttpClient httpClient = new DefaultHttpClient(cm);

    String proxyHost = config.getProxyHost();
    String proxyPort = config.getProxyPort();

    if (isValueSet(proxyHost) && proxyHost.trim().length() > 0 && isValueSet(proxyPort)
            && proxyPort.trim().length() > 0) {
        try {/*w ww .j  a  va  2 s .co m*/
            HttpHost proxy = new HttpHost(proxyHost, Integer.parseInt(proxyPort));
            httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        } catch (Exception e) {
            logger.error("Impossible to create proxy host:" + proxyHost + ", port:" + proxyPort, e);
        }
    }

    return httpClient;
}

From source file:org.ovirt.engine.sdk.web.ConnectionsPoolBuilder.java

/**
 * Creates PoolingClientConnectionManager
 *
 * @param url//from  ww w  .  j a  v a2s .c o m
 * @param port
 *
 * @return {@link ClientConnectionManager}
 */
private ClientConnectionManager createPoolingClientConnectionManager(String url, int port) {
    SchemeRegistry schemeRegistry = createSchemeRegistry(url, port);

    PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
    cm.setMaxTotal(MAX_CONNECTIONS);
    cm.setDefaultMaxPerRoute(MAX_CONNECTIONS_PER_ROUTE);
    cm.setMaxPerRoute(new HttpRoute(new HttpHost(getHost(url), getPort(url, port))), MAX_CONNECTIONS_PER_HOST);

    return cm;
}

From source file:org.apache.marmotta.ldclient.services.ldclient.LDClient.java

public LDClient(ClientConfiguration config) {
    log.info("Initialising Linked Data Client Service ...");

    this.config = config;

    endpoints = new ArrayList<>();
    for (Endpoint endpoint : defaultEndpoints) {
        endpoints.add(endpoint);/*from   w ww .j  a v  a  2 s .c  o m*/
    }
    endpoints.addAll(config.getEndpoints());

    Collections.sort(endpoints);
    if (log.isInfoEnabled()) {
        for (Endpoint endpoint : endpoints) {
            log.info("- LDClient Endpoint: {}", endpoint.getName());
        }
    }

    providers = new ArrayList<>();
    for (DataProvider provider : defaultProviders) {
        providers.add(provider);
    }
    providers.addAll(config.getProviders());
    if (log.isInfoEnabled()) {
        for (DataProvider provider : providers) {
            log.info("- LDClient Provider: {}", provider.getName());
        }
    }

    retrievalSemaphore = new Semaphore(config.getMaxParallelRequests());

    if (config.getHttpClient() != null) {
        log.debug("Using HttpClient provided in the configuration");
        this.client = config.getHttpClient();
    } else {
        log.debug("Creating default HttpClient based on the configuration");

        HttpParams httpParams = new BasicHttpParams();
        httpParams.setParameter(CoreProtocolPNames.USER_AGENT, "Apache Marmotta LDClient");

        httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeout());
        httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout());

        httpParams.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true);
        httpParams.setIntParameter(ClientPNames.MAX_REDIRECTS, 3);

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

        try {
            SSLContext sslcontext = SSLContext.getInstance("TLS");
            sslcontext.init(null, null, null);
            SSLSocketFactory sf = new SSLSocketFactory(sslcontext, SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);

            schemeRegistry.register(new Scheme("https", 443, sf));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }

        PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
        cm.setMaxTotal(20);
        cm.setDefaultMaxPerRoute(10);

        DefaultHttpClient client = new DefaultHttpClient(cm, httpParams);
        client.setRedirectStrategy(new LMFRedirectStrategy());
        client.setHttpRequestRetryHandler(new LMFHttpRequestRetryHandler());
        idleConnectionMonitorThread = new IdleConnectionMonitorThread(client.getConnectionManager());
        idleConnectionMonitorThread.start();

        this.client = client;
    }
}

From source file:com.ngdata.hbaseindexer.mr.HBaseIndexerMapper.java

private DirectSolrClassicInputDocumentWriter createClassicSolrWriter(Context context,
        Map<String, String> indexConnectionParams) throws IOException {
    PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
    connectionManager.setDefaultMaxPerRoute(getSolrMaxConnectionsPerRoute(indexConnectionParams));
    connectionManager.setMaxTotal(getSolrMaxConnectionsTotal(indexConnectionParams));

    HttpClient httpClient = new DefaultHttpClient(connectionManager);
    List<SolrServer> solrServers = createHttpSolrServers(indexConnectionParams, httpClient);

    return new DirectSolrClassicInputDocumentWriter(context.getConfiguration().get(INDEX_NAME_CONF_KEY),
            solrServers);/*from   w  w  w  . ja va  2s  . c  o  m*/
}

From source file:com.da.daum.DaumCafeBungImgList.java

private HttpClient getPoolHttpClient() {
    HttpClient httpclient;/*from www.j  av  a  2 s .  com*/
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
    // Increase max total connection to 200
    cm.setMaxTotal(200);
    // Increase default max connection per route to 20
    cm.setDefaultMaxPerRoute(20);
    // Increase max connections for localhost:80 to 50
    HttpHost localhost = new HttpHost("locahost", 80);
    cm.setMaxPerRoute(new HttpRoute(localhost), 50);
    //DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient = new DefaultHttpClient(cm);
    return httpclient;
}

From source file:com.flipkart.phantom.http.impl.HttpConnectionPool.java

/**
 * Initialize the connection pool//from  ww  w  .ja  v  a  2  s.c  o m
 */
public void initConnectionPool() {

    // max concurrent requests = max connections + request queue size
    this.processQueue = new Semaphore(requestQueueSize + maxConnections);

    // create scheme
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    // registry both http and https schemes
    schemeRegistry.register(new Scheme("http", port, PlainSocketFactory.getSocketFactory()));
    schemeRegistry.register(new Scheme("https", port, PlainSocketFactory.getSocketFactory()));

    PoolingClientConnectionManager cm;
    // create connection manager
    if (getTimeToLiveInSecs() > 0) {
        cm = new PoolingClientConnectionManager(schemeRegistry, getTimeToLiveInSecs(), TimeUnit.SECONDS);
    } else {
        cm = new PoolingClientConnectionManager(schemeRegistry);
    }

    // Max pool size
    cm.setMaxTotal(maxConnections);

    // Increase default max connection per route to 20
    cm.setDefaultMaxPerRoute(maxConnections);

    // Increase max connections for host:port
    HttpHost httpHost = new HttpHost(host, port);
    cm.setMaxPerRoute(new HttpRoute(httpHost), maxConnections);

    // set timeouts
    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeout);
    HttpConnectionParams.setSoTimeout(httpParams, operationTimeout);

    // create client pool
    this.client = new DefaultHttpClient(cm, httpParams);
}

From source file:com.sematext.ag.sink.AbstractHttpSink.java

/**
 * (non-Javadoc)// w  ww .java2  s .  c  o  m
 * 
 * @see com.sematext.ag.sink.Sink#init(com.sematext.ag.PlayerConfig)
 */
@Override
public void init(PlayerConfig config) throws InitializationFailedException {
    super.init(config);
    PoolingClientConnectionManager connectionManager = (PoolingClientConnectionManager) HTTP_CLIENT_INSTANCE
            .getConnectionManager();
    if (config.get(MAXIMUM_CONNECTIONS_PER_ROUTE_KEY) != null
            && !config.get(MAXIMUM_CONNECTIONS_PER_ROUTE_KEY).isEmpty()) {
        connectionManager
                .setDefaultMaxPerRoute(Integer.parseInt(config.get(MAXIMUM_CONNECTIONS_PER_ROUTE_KEY)));
    }
    if (config.get(MAXIMUM_CONNECTIONS_TOTAL_KEY) != null
            && !config.get(MAXIMUM_CONNECTIONS_TOTAL_KEY).isEmpty()) {
        connectionManager.setMaxTotal(Integer.parseInt(config.get(MAXIMUM_CONNECTIONS_TOTAL_KEY)));
    }
}