Example usage for org.apache.commons.httpclient.params HttpConnectionManagerParams setMaxTotalConnections

List of usage examples for org.apache.commons.httpclient.params HttpConnectionManagerParams setMaxTotalConnections

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.params HttpConnectionManagerParams setMaxTotalConnections.

Prototype

public void setMaxTotalConnections(int paramInt) 

Source Link

Usage

From source file:com.taobao.diamond.client.impl.DefaultDiamondPublisher.java

private void initHttpClient() {
    if (MockServer.isTestMode()) {
        return;/*from   www.  j av a  2  s .c o  m*/
    }
    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost(diamondConfigure.getDomainNameList().get(domainNamePos.get()),
            diamondConfigure.getPort());
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.closeIdleConnections(60 * 1000);

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setStaleCheckingEnabled(diamondConfigure.isConnectionStaleCheckingEnabled());
    params.setMaxConnectionsPerHost(hostConfiguration, diamondConfigure.getMaxHostConnections());
    params.setMaxTotalConnections(diamondConfigure.getMaxTotalConnections());
    params.setConnectionTimeout(diamondConfigure.getConnectionTimeout());
    params.setSoTimeout(requestTimeout);

    connectionManager.setParams(params);

    httpClient = new HttpClient(connectionManager);
    httpClient.setHostConfiguration(hostConfiguration);
}

From source file:it.geosolutions.httpproxy.HTTPProxy.java

/**
 * Initialize the <code>ProxyServlet</code>
 * /* w  w  w .  jav a 2  s. c o  m*/
 * @param servletConfig The Servlet configuration passed in by the servlet conatiner
 */
public void init(ServletConfig servletConfig) throws ServletException {
    super.init(servletConfig);

    ServletContext context = getServletContext();
    String proxyPropPath = context.getInitParameter("proxyPropPath");

    proxyConfig = new ProxyConfig(getServletContext(), proxyPropPath);

    connectionManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();

    params.setSoTimeout(proxyConfig.getSoTimeout());
    params.setConnectionTimeout(proxyConfig.getConnectionTimeout());
    params.setMaxTotalConnections(proxyConfig.getMaxTotalConnections());
    params.setDefaultMaxConnectionsPerHost(proxyConfig.getDefaultMaxConnectionsPerHost());

    //setSystemProxy(params);

    connectionManager.setParams(params);
    httpClient = new HttpClient(connectionManager);

    //
    // Check for system proxy usage
    //
    try {
        String proxyHost = System.getProperty("http.proxyHost");
        int proxyPort = 80;

        if (proxyHost != null && !proxyHost.isEmpty()) {
            try {
                proxyPort = (System.getProperty("http.proxyPort") != null
                        ? Integer.parseInt(System.getProperty("http.proxyPort"))
                        : proxyPort);

                httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);

            } catch (Exception ex) {
                LOGGER.warning("No proxy port found");
            }
        }

    } catch (Exception ex) {
        LOGGER.warning("Exception while setting the system proxy: " + ex.getLocalizedMessage());
    }

    // //////////////////////////////////////////
    // Setup the callbacks (in the future this
    // will be a pluggable lookup).
    // //////////////////////////////////////////

    callbacks = new ArrayList<ProxyCallback>();
    callbacks.add(new MimeTypeChecker(proxyConfig));
    callbacks.add(new HostNameChecker(proxyConfig));
    callbacks.add(new RequestTypeChecker(proxyConfig));
    callbacks.add(new MethodsChecker(proxyConfig));
    callbacks.add(new HostChecker(proxyConfig));
}

From source file:com.amazonaws.elasticmapreduce.AmazonElasticMapReduceClient.java

/**
 * Configure HttpClient with set of defaults as well as configuration
 * from AmazonElasticMapReduceConfig instance
 *
 *///w w w  .  ja v  a  2 s  . c o  m
private HttpClient configureHttpClient() {

    /* Set http client parameters */
    HttpClientParams httpClientParams = new HttpClientParams();
    httpClientParams.setParameter(HttpMethodParams.USER_AGENT, config.getUserAgent());
    httpClientParams.setParameter(HttpClientParams.RETRY_HANDLER, new HttpMethodRetryHandler() {

        public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
            if (executionCount > 3) {
                log.debug("Maximum Number of Retry attempts reached, will not retry");
                return false;
            }
            log.debug("Retrying request. Attempt " + executionCount);
            if (exception instanceof NoHttpResponseException) {
                log.debug("Retrying on NoHttpResponseException");
                return true;
            }
            if (exception instanceof InterruptedIOException) {
                log.debug("Will not retry on InterruptedIOException", exception);
                return false;
            }
            if (exception instanceof UnknownHostException) {
                log.debug("Will not retry on UnknownHostException", exception);
                return false;
            }
            if (!method.isRequestSent()) {
                log.debug("Retrying on failed sent request");
                return true;
            }
            return false;
        }
    });

    /* Set host configuration */
    HostConfiguration hostConfiguration = new HostConfiguration();

    /* Set connection manager parameters */
    HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
    connectionManagerParams.setConnectionTimeout(50000);
    connectionManagerParams.setSoTimeout(50000);
    connectionManagerParams.setStaleCheckingEnabled(true);
    connectionManagerParams.setTcpNoDelay(true);
    connectionManagerParams.setMaxTotalConnections(config.getMaxConnections());
    connectionManagerParams.setMaxConnectionsPerHost(hostConfiguration, config.getMaxConnections());

    /* Set connection manager */
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.setParams(connectionManagerParams);

    /* Set http client */
    httpClient = new HttpClient(httpClientParams, connectionManager);

    /* Set proxy if configured */
    if (config.isSetProxyHost() && config.isSetProxyPort()) {
        log.info("Configuring Proxy. Proxy Host: " + config.getProxyHost() + "Proxy Port: "
                + config.getProxyPort());
        hostConfiguration.setProxy(config.getProxyHost(), config.getProxyPort());
        if (config.isSetProxyUsername() && config.isSetProxyPassword()) {
            httpClient.getState().setProxyCredentials(
                    new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));

        }
    }

    httpClient.setHostConfiguration(hostConfiguration);
    return httpClient;
}

From source file:com.taobao.diamond.client.impl.DefaultDiamondSubscriber.java

protected void initHttpClient() {
    if (MockServer.isTestMode()) {
        return;/*from w w w . j  a va  2  s.c om*/
    }
    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost(diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
            diamondConfigure.getPort());

    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.closeIdleConnections(diamondConfigure.getPollingIntervalTime() * 4000);

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setStaleCheckingEnabled(diamondConfigure.isConnectionStaleCheckingEnabled());
    params.setMaxConnectionsPerHost(hostConfiguration, diamondConfigure.getMaxHostConnections());
    params.setMaxTotalConnections(diamondConfigure.getMaxTotalConnections());
    params.setConnectionTimeout(diamondConfigure.getConnectionTimeout());
    // 1,
    // boyan@taobao.com
    params.setSoTimeout(60 * 1000);

    connectionManager.setParams(params);
    httpClient = new HttpClient(connectionManager);
    httpClient.setHostConfiguration(hostConfiguration);
}

From source file:cn.leancloud.diamond.client.impl.DefaultDiamondSubscriber.java

protected void initHttpClient() {
    if (MockServer.isTestMode()) {
        return;//from   w  ww  .  jav a  2  s . c  om
    }
    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost(diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
            diamondConfigure.getPort());

    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.closeIdleConnections(diamondConfigure.getPollingIntervalTime() * 4000);

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setStaleCheckingEnabled(diamondConfigure.isConnectionStaleCheckingEnabled());
    params.setMaxConnectionsPerHost(hostConfiguration, diamondConfigure.getMaxHostConnections());
    params.setMaxTotalConnections(diamondConfigure.getMaxTotalConnections());
    params.setConnectionTimeout(diamondConfigure.getConnectionTimeout());
    // 1,
    // boyan@taobao.com
    params.setSoTimeout(60 * 1000);

    connectionManager.setParams(params);
    httpClient = new HttpClient(connectionManager);
    httpClient.setHostConfiguration(hostConfiguration);
}

From source file:com.liferay.portal.util.HttpImpl.java

public HttpImpl() {

    // Mimic behavior found in
    // http://java.sun.com/j2se/1.5.0/docs/guide/net/properties.html

    if (Validator.isNotNull(_NON_PROXY_HOSTS)) {
        String nonProxyHostsRegEx = _NON_PROXY_HOSTS;

        nonProxyHostsRegEx = nonProxyHostsRegEx.replaceAll("\\.", "\\\\.");
        nonProxyHostsRegEx = nonProxyHostsRegEx.replaceAll("\\*", ".*?");
        nonProxyHostsRegEx = nonProxyHostsRegEx.replaceAll("\\|", ")|(");

        nonProxyHostsRegEx = "(" + nonProxyHostsRegEx + ")";

        _nonProxyHostsPattern = Pattern.compile(nonProxyHostsRegEx);
    }/*from   ww w  .  j  a  va2  s  .c  om*/

    MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();

    HttpConnectionManagerParams httpConnectionManagerParams = httpConnectionManager.getParams();

    httpConnectionManagerParams.setConnectionTimeout(_TIMEOUT);
    httpConnectionManagerParams.setDefaultMaxConnectionsPerHost(new Integer(_MAX_CONNECTIONS_PER_HOST));
    httpConnectionManagerParams.setMaxTotalConnections(new Integer(_MAX_TOTAL_CONNECTIONS));
    httpConnectionManagerParams.setSoTimeout(_TIMEOUT);

    _httpClient.setHttpConnectionManager(httpConnectionManager);
    _proxyHttpClient.setHttpConnectionManager(httpConnectionManager);

    if (hasProxyConfig() && Validator.isNotNull(_PROXY_USERNAME)) {
        List<String> authPrefs = new ArrayList<String>();

        if (_PROXY_AUTH_TYPE.equals("username-password")) {
            _proxyCredentials = new UsernamePasswordCredentials(_PROXY_USERNAME, _PROXY_PASSWORD);

            authPrefs.add(AuthPolicy.BASIC);
            authPrefs.add(AuthPolicy.DIGEST);
            authPrefs.add(AuthPolicy.NTLM);
        } else if (_PROXY_AUTH_TYPE.equals("ntlm")) {
            _proxyCredentials = new NTCredentials(_PROXY_USERNAME, _PROXY_PASSWORD, _PROXY_NTLM_HOST,
                    _PROXY_NTLM_DOMAIN);

            authPrefs.add(AuthPolicy.NTLM);
            authPrefs.add(AuthPolicy.BASIC);
            authPrefs.add(AuthPolicy.DIGEST);
        }

        HttpClientParams httpClientParams = _proxyHttpClient.getParams();

        httpClientParams.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
    }
}

From source file:com.amazonaws.a2s.AmazonA2SClient.java

/**
 * Configure HttpClient with set of defaults as well as configuration
 * from AmazonA2SConfig instance//from w  w  w  . j  av  a  2s  .c  o m
 *
 */
private HttpClient configureHttpClient() {

    /* Set http client parameters */
    HttpClientParams httpClientParams = new HttpClientParams();
    httpClientParams.setParameter(HttpMethodParams.USER_AGENT, config.getUserAgent());
    httpClientParams.setParameter(HttpClientParams.RETRY_HANDLER, new HttpMethodRetryHandler() {
        public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
            if (executionCount > 3) {
                log.debug("Maximum Number of Retry attempts reached, will not retry");
                return false;
            }
            log.debug("Retrying request. Attempt " + executionCount);
            if (exception instanceof NoHttpResponseException) {
                log.debug("Retrying on NoHttpResponseException");
                return true;
            }
            if (!method.isRequestSent()) {
                log.debug("Retrying on failed sent request");
                return true;
            }
            return false;
        }
    });

    /* Set host configuration */
    HostConfiguration hostConfiguration = new HostConfiguration();

    /* Set connection manager parameters */
    HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
    connectionManagerParams.setConnectionTimeout(50000);
    connectionManagerParams.setSoTimeout(50000);
    connectionManagerParams.setStaleCheckingEnabled(true);
    connectionManagerParams.setTcpNoDelay(true);
    connectionManagerParams.setMaxTotalConnections(3);
    connectionManagerParams.setMaxConnectionsPerHost(hostConfiguration, 3);

    /* Set connection manager */
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.setParams(connectionManagerParams);

    /* Set http client */
    httpClient = new HttpClient(httpClientParams, connectionManager);

    /* Set proxy if configured */
    if (config.isSetProxyHost() && config.isSetProxyPort()) {
        log.info("Configuring Proxy. Proxy Host: " + config.getProxyHost() + "Proxy Port: "
                + config.getProxyPort());
        hostConfiguration.setProxy(config.getProxyHost(), config.getProxyPort());

    }

    httpClient.setHostConfiguration(hostConfiguration);
    return httpClient;
}

From source file:com.twelve.capital.external.feed.util.HttpImpl.java

public HttpImpl() {

    // Override the default protocol socket factory because it uses
    // reflection for JDK 1.4 compatibility, which we do not need. It also
    // attemps to create a new socket in a different thread so that we
    // cannot track which class loader initiated the call.

    Protocol protocol = new Protocol("http", new FastProtocolSocketFactory(), 80);

    Protocol.registerProtocol("http", protocol);

    // Mimic behavior found in
    // http://java.sun.com/j2se/1.5.0/docs/guide/net/properties.html

    if (Validator.isNotNull(_NON_PROXY_HOSTS)) {
        String nonProxyHostsRegEx = _NON_PROXY_HOSTS;

        nonProxyHostsRegEx = nonProxyHostsRegEx.replaceAll("\\.", "\\\\.");
        nonProxyHostsRegEx = nonProxyHostsRegEx.replaceAll("\\*", ".*?");
        nonProxyHostsRegEx = nonProxyHostsRegEx.replaceAll("\\|", ")|(");

        nonProxyHostsRegEx = "(" + nonProxyHostsRegEx + ")";

        _nonProxyHostsPattern = Pattern.compile(nonProxyHostsRegEx);
    }// w w  w  .java  2  s.  com

    MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();

    HttpConnectionManagerParams httpConnectionManagerParams = httpConnectionManager.getParams();

    httpConnectionManagerParams.setConnectionTimeout(_TIMEOUT);
    httpConnectionManagerParams.setDefaultMaxConnectionsPerHost(new Integer(_MAX_CONNECTIONS_PER_HOST));
    httpConnectionManagerParams.setMaxTotalConnections(new Integer(_MAX_TOTAL_CONNECTIONS));
    httpConnectionManagerParams.setSoTimeout(_TIMEOUT);

    _httpClient.setHttpConnectionManager(httpConnectionManager);
    _proxyHttpClient.setHttpConnectionManager(httpConnectionManager);

    if (!hasProxyConfig() || Validator.isNull(_PROXY_USERNAME)) {
        return;
    }

    List<String> authPrefs = new ArrayList<String>();

    if (_PROXY_AUTH_TYPE.equals("username-password")) {
        _proxyCredentials = new UsernamePasswordCredentials(_PROXY_USERNAME, _PROXY_PASSWORD);

        authPrefs.add(AuthPolicy.BASIC);
        authPrefs.add(AuthPolicy.DIGEST);
        authPrefs.add(AuthPolicy.NTLM);
    } else if (_PROXY_AUTH_TYPE.equals("ntlm")) {
        _proxyCredentials = new NTCredentials(_PROXY_USERNAME, _PROXY_PASSWORD, _PROXY_NTLM_HOST,
                _PROXY_NTLM_DOMAIN);

        authPrefs.add(AuthPolicy.NTLM);
        authPrefs.add(AuthPolicy.BASIC);
        authPrefs.add(AuthPolicy.DIGEST);
    }

    HttpClientParams httpClientParams = _proxyHttpClient.getParams();

    httpClientParams.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
}

From source file:com.datos.vfs.provider.http.HttpClientFactory.java

/**
 * Creates a new connection to the server.
 * @param builder The HttpFileSystemConfigBuilder.
 * @param scheme The protocol.//from  w  w w  .  j  a v a2 s.  c  om
 * @param hostname The hostname.
 * @param port The port number.
 * @param username The username.
 * @param password The password
 * @param fileSystemOptions The file system options.
 * @return a new HttpClient connection.
 * @throws FileSystemException if an error occurs.
 * @since 2.0
 */
public static HttpClient createConnection(final HttpFileSystemConfigBuilder builder, final String scheme,
        final String hostname, final int port, final String username, final String password,
        final FileSystemOptions fileSystemOptions) throws FileSystemException {
    HttpClient client;
    try {
        final HttpConnectionManager mgr = new MultiThreadedHttpConnectionManager();
        final HttpConnectionManagerParams connectionMgrParams = mgr.getParams();

        client = new HttpClient(mgr);

        final HostConfiguration config = new HostConfiguration();
        config.setHost(hostname, port, scheme);

        if (fileSystemOptions != null) {
            final String proxyHost = builder.getProxyHost(fileSystemOptions);
            final int proxyPort = builder.getProxyPort(fileSystemOptions);

            if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) {
                config.setProxy(proxyHost, proxyPort);
            }

            final UserAuthenticator proxyAuth = builder.getProxyAuthenticator(fileSystemOptions);
            if (proxyAuth != null) {
                final UserAuthenticationData authData = UserAuthenticatorUtils.authenticate(proxyAuth,
                        new UserAuthenticationData.Type[] { UserAuthenticationData.USERNAME,
                                UserAuthenticationData.PASSWORD });

                if (authData != null) {
                    final UsernamePasswordCredentials proxyCreds = new UsernamePasswordCredentials(
                            UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
                                    UserAuthenticationData.USERNAME, null)),
                            UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
                                    UserAuthenticationData.PASSWORD, null)));

                    final AuthScope scope = new AuthScope(proxyHost, AuthScope.ANY_PORT);
                    client.getState().setProxyCredentials(scope, proxyCreds);
                }

                if (builder.isPreemptiveAuth(fileSystemOptions)) {
                    final HttpClientParams httpClientParams = new HttpClientParams();
                    httpClientParams.setAuthenticationPreemptive(true);
                    client.setParams(httpClientParams);
                }
            }

            final Cookie[] cookies = builder.getCookies(fileSystemOptions);
            if (cookies != null) {
                client.getState().addCookies(cookies);
            }
        }
        /**
         * ConnectionManager set methods must be called after the host & port and proxy host & port
         * are set in the HostConfiguration. They are all used as part of the key when HttpConnectionManagerParams
         * tries to locate the host configuration.
         */
        connectionMgrParams.setMaxConnectionsPerHost(config,
                builder.getMaxConnectionsPerHost(fileSystemOptions));
        connectionMgrParams.setMaxTotalConnections(builder.getMaxTotalConnections(fileSystemOptions));

        connectionMgrParams.setConnectionTimeout(builder.getConnectionTimeout(fileSystemOptions));
        connectionMgrParams.setSoTimeout(builder.getSoTimeout(fileSystemOptions));

        client.setHostConfiguration(config);

        if (username != null) {
            final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
            final AuthScope scope = new AuthScope(hostname, AuthScope.ANY_PORT);
            client.getState().setCredentials(scope, creds);
        }
    } catch (final Exception exc) {
        throw new FileSystemException("vfs.provider.http/connect.error", exc, hostname);
    }

    return client;
}

From source file:nu.validator.xml.PrudentHttpEntityResolver.java

/**
 * Sets the timeouts of the HTTP client.
 * //from   w  w  w.  ja  v  a  2 s  .c  o m
 * @param connectionTimeout
 *            timeout until connection established in milliseconds. Zero
 *            means no timeout.
 * @param socketTimeout
 *            timeout for waiting for data in milliseconds. Zero means no
 *            timeout.
 */
public static void setParams(int connectionTimeout, int socketTimeout, int maxRequests) {
    HttpConnectionManagerParams hcmp = client.getHttpConnectionManager().getParams();
    hcmp.setConnectionTimeout(connectionTimeout);
    hcmp.setSoTimeout(socketTimeout);
    hcmp.setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, maxRequests);
    hcmp.setMaxTotalConnections(200); // XXX take this from a property
    PrudentHttpEntityResolver.maxRequests = maxRequests;
    HttpClientParams hcp = client.getParams();
    hcp.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
    hcp.setIntParameter(HttpClientParams.MAX_REDIRECTS, 20); // Gecko
    // default
}