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:com.ngdata.hbaseindexer.mr.HBaseMapReduceIndexerTool.java

private Set<SolrServer> createSolrServers(Map<String, String> indexConnectionParams)
        throws MalformedURLException {
    String solrMode = getSolrMode(indexConnectionParams);
    if (solrMode.equals("cloud")) {
        String indexZkHost = indexConnectionParams.get(SolrConnectionParams.ZOOKEEPER);
        String collectionName = indexConnectionParams.get(SolrConnectionParams.COLLECTION);
        CloudSolrServer solrServer = new CloudSolrServer(indexZkHost);
        solrServer.setDefaultCollection(collectionName);
        return Collections.singleton((SolrServer) solrServer);
    } else if (solrMode.equals("classic")) {
        PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
        connectionManager.setDefaultMaxPerRoute(getSolrMaxConnectionsPerRoute(indexConnectionParams));
        connectionManager.setMaxTotal(getSolrMaxConnectionsTotal(indexConnectionParams));

        HttpClient httpClient = new DefaultHttpClient(connectionManager);
        return new HashSet<SolrServer>(createHttpSolrServers(indexConnectionParams, httpClient));
    } else {/* ww w . ja  va  2s . c  o  m*/
        throw new RuntimeException(
                "Only 'cloud' and 'classic' are valid values for solr.mode, but got " + solrMode);
    }

}

From source file:com.kurento.kmf.content.internal.StreamingProxy.java

/**
 * After constructor method; it created the HTTP client using configuration
 * parameters {@link ContentApiConfiguration}.
 * /*from  ww  w .  ja  v  a 2  s.  co  m*/
 * @see ContentApiConfiguration
 */
@PostConstruct
public void afterPropertiesSet() {
    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, configuration.getProxyConnectionTimeout());
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, configuration.getProxySocketTimeout());

    // Thread-safe configuration (using PoolingClientConnectionManager)
    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(configuration.getProxyMaxConnections());
    cm.setDefaultMaxPerRoute(configuration.getProxyMaxConnectionsPerRoute());

    httpClient = new DefaultHttpClient(cm, params);
}

From source file:org.apache.usergrid.security.sso.UsergridExternalProvider.java

private Client getJerseyClient() {

    if (jerseyClient == null) {

        synchronized (this) {

            // create HTTPClient and with configured connection pool

            int poolSize = 100; // connections
            final String poolSizeStr = properties.getProperty(CENTRAL_CONNECTION_POOL_SIZE);
            if (poolSizeStr != null) {
                poolSize = Integer.parseInt(poolSizeStr);
            }/*from ww w.  ja v  a  2  s.co m*/

            PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
            connectionManager.setMaxTotal(poolSize);

            int timeout = 20000; // ms
            final String timeoutStr = properties.getProperty(CENTRAL_CONNECTION_TIMEOUT);
            if (timeoutStr != null) {
                timeout = Integer.parseInt(timeoutStr);
            }

            int readTimeout = 20000; // ms
            final String readTimeoutStr = properties.getProperty(CENTRAL_READ_TIMEOUT);
            if (readTimeoutStr != null) {
                readTimeout = Integer.parseInt(readTimeoutStr);
            }

            ClientConfig clientConfig = new ClientConfig();
            clientConfig.register(new JacksonFeature());
            clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
            clientConfig.connectorProvider(new ApacheConnectorProvider());

            jerseyClient = ClientBuilder.newClient(clientConfig);
            jerseyClient.property(ClientProperties.CONNECT_TIMEOUT, timeout);
            jerseyClient.property(ClientProperties.READ_TIMEOUT, readTimeout);
        }
    }

    return jerseyClient;

}

From source file:com.liferay.portal.search.solr.http.BasePoolingHttpClientFactory.java

protected void applyProperties(PoolingClientConnectionManager poolingClientConnectionManager) {

    if (_defaultMaxConnectionsPerRoute != null) {
        poolingClientConnectionManager.setDefaultMaxPerRoute(_defaultMaxConnectionsPerRoute.intValue());
    }/*from   w  ww  .ja va2s . co m*/

    if (_maxTotalConnections != null) {
        poolingClientConnectionManager.setMaxTotal(_maxTotalConnections.intValue());
    }
}

From source file:org.sentilo.common.rest.impl.RESTClientImpl.java

@Override
public void afterPropertiesSet() throws Exception {
    if (httpClient == null) {
        final PoolingClientConnectionManager pccm = new PoolingClientConnectionManager();
        // Increase max total connection to 400
        pccm.setMaxTotal(400);
        // Increase default max connection per route to 50
        pccm.setDefaultMaxPerRoute(50);//from   w  w w .j  av a2 s.  c o m

        httpClient = new DefaultHttpClient(pccm);
        // Set the timeouts for read the response and create a connection
        setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS);
        setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
    }

    if (interceptors != null && httpClient instanceof DefaultHttpClient) {
        for (final HttpRequestInterceptor interceptor : interceptors) {
            ((DefaultHttpClient) httpClient).addRequestInterceptor(interceptor);
        }
    }

    if (credentials != null && httpClient instanceof DefaultHttpClient) {
        ((DefaultHttpClient) httpClient).getCredentialsProvider().setCredentials(authScope, credentials);

    }
}

From source file:gov.nrel.bacnet.consumer.DatabusSender.java

public DatabusSender(String username, String key, ExecutorService recorderSvc, String host, int port,
        boolean isSecure) {
    log.info("username=" + username + " key=" + key + " port=" + port);
    this.port = port;

    PoolingClientConnectionManager mgr = new PoolingClientConnectionManager();
    mgr.setDefaultMaxPerRoute(30);/*from   ww w  .  jav  a 2s .  com*/
    mgr.setMaxTotal(30);
    this.username = username;
    this.key = key;
    this.host = host;
    this.port = port;
    this.mode = "https";

    if (!isSecure)
        mode = "http";
    this.hostUrl = mode + "://" + host + ":" + port;
    if (isSecure) {
        httpclient = createSecureOne(mgr);
    } else {
        httpclient = new DefaultHttpClient(mgr);
    }
    HttpParams params = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 60000);
    HttpConnectionParams.setSoTimeout(params, 60000);
    log.info("hostUrl=" + hostUrl);
}

From source file:dk.deck.resolver.ArtifactResolverRemote.java

public ArtifactResolverRemote(List<String> repositories, String username, String password) {
    this.repositories = Collections.unmodifiableList(repositories);
    PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
    connectionManager.setMaxTotal(MAX_HTTP_THREADS);
    connectionManager.setDefaultMaxPerRoute(MAX_HTTP_THREADS);
    httpclient = new DefaultHttpClient(connectionManager);

    List<String> authpref = new ArrayList<String>();
    authpref.add(AuthPolicy.BASIC);//from   www.ja v  a2 s .co  m
    authpref.add(AuthPolicy.DIGEST);
    httpclient.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authpref);
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
    httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);

}

From source file:org.graphity.core.util.jena.HttpOp.java

/** Create an HttpClient that performs connection pooling.  This can be used
 * with {@link #setDefaultHttpClient} or provided in the HttpOp calls.
 *//*from  www .j a va2  s.  co  m*/
public static HttpClient createCachingHttpClient() {
    return new SystemDefaultHttpClient() {
        /** See SystemDefaultHttpClient (4.2).  This version always sets the connection cache */
        @Override
        protected ClientConnectionManager createClientConnectionManager() {
            PoolingClientConnectionManager connmgr = new PoolingClientConnectionManager(
                    SchemeRegistryFactory.createSystemDefault());
            String s = System.getProperty("http.maxConnections", "5");
            int max = Integer.parseInt(s);
            connmgr.setDefaultMaxPerRoute(max);
            connmgr.setMaxTotal(2 * max);
            return connmgr;
        }
    };
}

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

protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
    if (connectionMonitor == null) {
        connectionMonitor = new ConnectionMonitor();
    }//from   w w w . ja v a  2  s  .  co  m

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

    // TODO protocol
    ResteasyWebTarget target = client
            .target("http://" + url.getHost() + ":" + url.getPort() + "/" + getContextPath(url));
    return target.proxy(serviceType);
}

From source file:com.liferay.ide.core.remote.RemoteConnection.java

private HttpClient getHttpClient() {
    if (this.httpClient == null) {
        DefaultHttpClient newDefaultHttpClient = null;

        if (getUsername() != null || getPassword() != null) {
            try {
                final IProxyService proxyService = LiferayCore.getProxyService();

                URI uri = new URI("http://" + getHost() + ":" + getHttpPort()); //$NON-NLS-1$ //$NON-NLS-2$
                IProxyData[] proxyDataForHost = proxyService.select(uri);

                for (IProxyData data : proxyDataForHost) {
                    if (data.getHost() != null && data.getPort() > 0) {
                        SchemeRegistry schemeRegistry = new SchemeRegistry();
                        schemeRegistry.register(
                                new Scheme("http", data.getPort(), PlainSocketFactory.getSocketFactory())); //$NON-NLS-1$

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

                        DefaultHttpClient newHttpClient = new DefaultHttpClient(cm);
                        HttpHost proxy = new HttpHost(data.getHost(), data.getPort());

                        newHttpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

                        newDefaultHttpClient = newHttpClient;
                        break;
                    }/* w w  w  .j  a va 2 s . com*/
                }

                if (newDefaultHttpClient == null) {
                    uri = new URI("SOCKS://" + getHost() + ":" + getHttpPort()); //$NON-NLS-1$ //$NON-NLS-2$
                    proxyDataForHost = proxyService.select(uri);

                    for (IProxyData data : proxyDataForHost) {
                        if (data.getHost() != null) {
                            DefaultHttpClient newHttpClient = new DefaultHttpClient();
                            newHttpClient.getParams().setParameter("socks.host", data.getHost()); //$NON-NLS-1$
                            newHttpClient.getParams().setParameter("socks.port", data.getPort()); //$NON-NLS-1$
                            newHttpClient.getConnectionManager().getSchemeRegistry().register(
                                    new Scheme("socks", data.getPort(), PlainSocketFactory.getSocketFactory())); //$NON-NLS-1$

                            newDefaultHttpClient = newHttpClient;
                            break;
                        }
                    }
                }
            } catch (URISyntaxException e) {
                LiferayCore.logError("Unable to read proxy data", e); //$NON-NLS-1$
            }

            if (newDefaultHttpClient == null) {
                newDefaultHttpClient = new DefaultHttpClient();
            }

            this.httpClient = newDefaultHttpClient;
        } else {
            this.httpClient = new DefaultHttpClient();
        }
    }

    return this.httpClient;
}