Example usage for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager setDefaultMaxPerRoute

List of usage examples for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager setDefaultMaxPerRoute

Introduction

In this page you can find the example usage for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager setDefaultMaxPerRoute.

Prototype

public void setDefaultMaxPerRoute(final int max) 

Source Link

Usage

From source file:com.msopentech.thali.utilities.universal.HttpKeyHttpClient.java

protected ClientConnectionManager javaCreateClientConnectionManager() {
    // Note that HttpKeySocksProxyClientConnOperator only supports SOCKS connections
    // which is why we return a standard ThreadSafeClientConnManager when there is no
    // proxy and return a version with createConnectionOperator overridden only when
    // the connection is for SOCKS.

    ThreadSafeClientConnManager connManager;

    // It turns out that there is a bug in the Apache code we are using that if you use the argument
    // constructor below it will ignore anything you set in terms of httpParams and so we only get
    // 2 connections per destination which doesn't work for the realy who is sending lots of connections
    // to the local TDH. The work around is to use the single argument constructor below. Unfortunately this
    // constructor doesn't work in Android who has an even older version of the code, hence why we have to
    // methods./* www .  j  ava2  s.  c  om*/
    if (proxy == null) {
        connManager = new ThreadSafeClientConnManager(schemeRegistry);
    } else {
        connManager = new ThreadSafeClientConnManager(schemeRegistry) {
            @Override
            protected ClientConnectionOperator createConnectionOperator(SchemeRegistry schreg) {
                return new HttpKeySocksProxyClientConnOperator(schreg, proxy);
            }
        };
    }

    connManager.setDefaultMaxPerRoute(maxConnections);
    connManager.setMaxTotal(maxConnections);
    return connManager;
}

From source file:org.wso2.carbon.appfactory.jenkins.build.RestBasedJenkinsCIConnector.java

/**
 * Private constructor for singleton class
 *
 * @throws AppFactoryException when reading from appfactory.xml
 *///from  ww w . j  a  v  a 2s .co  m
private RestBasedJenkinsCIConnector() throws AppFactoryException {

    this.authenticate = Boolean.parseBoolean(AppFactoryUtil.getAppfactoryConfiguration()
            .getFirstProperty(JenkinsCIConstants.AUTHENTICATE_CONFIG_SELECTOR));

    this.username = AppFactoryUtil.getAppfactoryConfiguration()
            .getFirstProperty(JenkinsCIConstants.JENKINS_SERVER_ADMIN_USERNAME);

    this.apiKeyOrPassword = AppFactoryUtil.getAppfactoryConfiguration()
            .getFirstProperty(JenkinsCIConstants.JENKINS_SERVER_ADMIN_PASSWORD);

    this.allowAllHostNameVerifier = Boolean.parseBoolean(AppFactoryUtil.getAppfactoryConfiguration()
            .getFirstProperty(JenkinsCIConstants.ALLOW_ALL_HOSTNAME_VERIFIER));

    this.defaultMaxConnectionsPerRoute = Integer.parseInt(AppFactoryUtil.getAppfactoryConfiguration()
            .getFirstProperty(JenkinsCIConstants.DEFAULT_MAX_CONNECTIONS_PER_ROUTE));

    this.maxTotalConnections = Integer.parseInt(AppFactoryUtil.getAppfactoryConfiguration()
            .getFirstProperty(JenkinsCIConstants.MAX_TOTAL_CONNECTIONS));

    if (log.isDebugEnabled()) {
        log.debug(String.format("Authenticate : %s", this.authenticate));
        log.debug(String.format("Jenkins user name : %s", this.username));
    }

    ThreadSafeClientConnManager threadSafeClientConnManager = new ThreadSafeClientConnManager();
    threadSafeClientConnManager.setDefaultMaxPerRoute(this.defaultMaxConnectionsPerRoute);
    threadSafeClientConnManager.setMaxTotal(this.maxTotalConnections);
    this.httpClient = new DefaultHttpClient(threadSafeClientConnManager);
}

From source file:com.marklogic.client.impl.JerseyServices.java

private void connect(String host, int port, String database, String user, String password,
        Authentication authenType, SSLContext context, X509HostnameVerifier verifier) {
    if (logger.isDebugEnabled())
        logger.debug("Connecting to {} at {} as {}", new Object[] { host, port, user });

    if (host == null)
        throw new IllegalArgumentException("No host provided");

    if (authenType == null) {
        if (context != null) {
            authenType = Authentication.BASIC;
        }/*from   ww  w  .ja v  a2 s .  c o m*/
    }

    if (authenType != null) {
        if (user == null)
            throw new IllegalArgumentException("No user provided");
        if (password == null)
            throw new IllegalArgumentException("No password provided");
    }

    if (connection != null)
        connection = null;
    if (client != null) {
        client.destroy();
        client = null;
    }

    this.database = database;

    String baseUri = ((context == null) ? "http" : "https") + "://" + host + ":" + port + "/v1/";

    Properties props = System.getProperties();

    if (props.containsKey(MAX_DELAY_PROP)) {
        String maxDelayStr = props.getProperty(MAX_DELAY_PROP);
        if (maxDelayStr != null && maxDelayStr.length() > 0) {
            int max = Integer.parseInt(maxDelayStr);
            if (max > 0) {
                maxDelay = max * 1000;
            }
        }
    }
    if (props.containsKey(MIN_RETRY_PROP)) {
        String minRetryStr = props.getProperty(MIN_RETRY_PROP);
        if (minRetryStr != null && minRetryStr.length() > 0) {
            int min = Integer.parseInt(minRetryStr);
            if (min > 0) {
                minRetry = min;
            }
        }
    }

    // TODO: integrated control of HTTP Client and Jersey Client logging
    if (!props.containsKey("org.apache.commons.logging.Log")) {
        System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
    }
    if (!props.containsKey("org.apache.commons.logging.simplelog.log.org.apache.http")) {
        System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "warn");
    }
    if (!props.containsKey("org.apache.commons.logging.simplelog.log.org.apache.http.wire")) {
        System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "warn");
    }

    Scheme scheme = null;
    if (context == null) {
        SchemeSocketFactory socketFactory = PlainSocketFactory.getSocketFactory();
        scheme = new Scheme("http", port, socketFactory);
    } else {
        SSLSocketFactory socketFactory = new SSLSocketFactory(context, verifier);
        scheme = new Scheme("https", port, socketFactory);
    }
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(scheme);

    int maxRouteConnections = 100;
    int maxTotalConnections = 2 * maxRouteConnections;

    /*
     * 4.2 PoolingClientConnectionManager connMgr = new
     * PoolingClientConnectionManager(schemeRegistry);
     * connMgr.setMaxTotal(maxTotalConnections);
     * connMgr.setDefaultMaxPerRoute(maxRouteConnections);
     * connMgr.setMaxPerRoute( new HttpRoute(new HttpHost(baseUri)),
     *     maxRouteConnections);
     */
    // start 4.1
    ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager(schemeRegistry);
    connMgr.setMaxTotal(maxTotalConnections);
    connMgr.setDefaultMaxPerRoute(maxRouteConnections);
    connMgr.setMaxForRoute(new HttpRoute(new HttpHost(baseUri)), maxRouteConnections);
    // end 4.1

    // CredentialsProvider credentialsProvider = new
    // BasicCredentialsProvider();
    // credentialsProvider.setCredentials(new AuthScope(host, port),
    // new UsernamePasswordCredentials(user, password));

    HttpParams httpParams = new BasicHttpParams();

    if (authenType != null) {
        List<String> authpref = new ArrayList<String>();

        if (authenType == Authentication.BASIC)
            authpref.add(AuthPolicy.BASIC);
        else if (authenType == Authentication.DIGEST)
            authpref.add(AuthPolicy.DIGEST);
        else
            throw new MarkLogicInternalException(
                    "Internal error - unknown authentication type: " + authenType.name());

        httpParams.setParameter(AuthPNames.PROXY_AUTH_PREF, authpref);
    }

    httpParams.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);

    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);

    // HttpConnectionParams.setStaleCheckingEnabled(httpParams, false);

    // long-term alternative to isFirstRequest alive
    // HttpProtocolParams.setUseExpectContinue(httpParams, false);
    // httpParams.setIntParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE, 1000);

    DefaultApacheHttpClient4Config config = new DefaultApacheHttpClient4Config();
    Map<String, Object> configProps = config.getProperties();
    configProps.put(ApacheHttpClient4Config.PROPERTY_PREEMPTIVE_BASIC_AUTHENTICATION, false);
    configProps.put(ApacheHttpClient4Config.PROPERTY_DISABLE_COOKIES, true);
    configProps.put(ApacheHttpClient4Config.PROPERTY_CONNECTION_MANAGER, connMgr);
    // ignored?
    configProps.put(ApacheHttpClient4Config.PROPERTY_FOLLOW_REDIRECTS, false);
    // configProps.put(ApacheHttpClient4Config.PROPERTY_CREDENTIALS_PROVIDER,
    // credentialsProvider);
    configProps.put(ApacheHttpClient4Config.PROPERTY_HTTP_PARAMS, httpParams);
    // switches from buffered to streamed in Jersey Client
    configProps.put(ApacheHttpClient4Config.PROPERTY_CHUNKED_ENCODING_SIZE, 32 * 1024);

    client = ApacheHttpClient4.create(config);

    // System.setProperty("javax.net.debug", "all"); // all or ssl

    if (authenType == null) {
        checkFirstRequest = false;
    } else if (authenType == Authentication.BASIC) {
        checkFirstRequest = false;

        client.addFilter(new HTTPBasicAuthFilter(user, password));
    } else if (authenType == Authentication.DIGEST) {
        checkFirstRequest = true;

        // workaround for JerseyClient bug 1445
        client.addFilter(new DigestChallengeFilter());

        client.addFilter(new HTTPDigestAuthFilter(user, password));
    } else {
        throw new MarkLogicInternalException(
                "Internal error - unknown authentication type: " + authenType.name());
    }

    // client.addFilter(new LoggingFilter(System.err));

    connection = client.resource(baseUri);
}

From source file:org.jenkinsmvn.jenkins.api.JenkinsClientFactory.java

public static AbstractHttpClient createHttpClient(int defaultMaxPerRoute, int maxTotal) {
    ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager();
    manager.setDefaultMaxPerRoute(defaultMaxPerRoute);
    manager.setMaxTotal(maxTotal);//from  ww w. ja v  a 2  s.co m

    return new DefaultHttpClient(manager);
}

From source file:org.springframework.http.client.HttpComponentsClientHttpRequestFactory.java

/**
 * Create a new instance of the {@code HttpComponentsClientHttpRequestFactory} with a default {@link HttpClient} that
 * uses a default {@link org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager}
 *//*www.j  av a  2s  . c o m*/
public HttpComponentsClientHttpRequestFactory() {
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

    ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(schemeRegistry);
    connectionManager.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS);
    connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);

    httpClient = new DefaultHttpClient(connectionManager);
    this.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
}

From source file:voldemort.store.readonly.swapper.StoreSwapper.java

public static void main(String[] args) throws Exception {
    OptionParser parser = new OptionParser();
    parser.accepts("help", "print usage information");
    parser.accepts("cluster", "[REQUIRED] the voldemort cluster.xml file ").withRequiredArg()
            .describedAs("cluster.xml");
    parser.accepts("name", "[REQUIRED] the name of the store to swap").withRequiredArg()
            .describedAs("store-name");
    parser.accepts("servlet-path", "the path for the read-only management servlet").withRequiredArg()
            .describedAs("path");
    parser.accepts("file", "[REQUIRED] uri of a directory containing the new store files").withRequiredArg()
            .describedAs("uri");
    parser.accepts("timeout", "http timeout for the fetch in ms").withRequiredArg().describedAs("timeout ms")
            .ofType(Integer.class);
    parser.accepts("rollback", "Rollback store to older version");
    parser.accepts("admin", "Use admin services. Default = false");
    parser.accepts("push-version", "[REQUIRED] Version of push to fetch / rollback-to").withRequiredArg()
            .ofType(Long.class);

    OptionSet options = parser.parse(args);
    if (options.has("help")) {
        parser.printHelpOn(System.out);
        System.exit(0);/*from w  ww . j  ava  2 s.c  o m*/
    }

    Set<String> missing = CmdUtils.missing(options, "cluster", "name", "file", "push-version");
    if (missing.size() > 0) {
        if (!(missing.equals(ImmutableSet.of("file")) && (options.has("rollback")))) {
            System.err.println("Missing required arguments: " + Joiner.on(", ").join(missing));
            parser.printHelpOn(System.err);
            System.exit(1);
        }
    }

    String clusterXml = (String) options.valueOf("cluster");
    String storeName = (String) options.valueOf("name");
    String mgmtPath = CmdUtils.valueOf(options, "servlet-path", "read-only/mgmt");
    String filePath = (String) options.valueOf("file");
    int timeoutMs = CmdUtils.valueOf(options, "timeout",
            (int) (3 * Time.SECONDS_PER_HOUR * Time.MS_PER_SECOND));
    boolean useAdminServices = options.has("admin");
    boolean rollbackStore = options.has("rollback");
    Long pushVersion = (Long) options.valueOf("push-version");

    String clusterStr = FileUtils.readFileToString(new File(clusterXml));
    Cluster cluster = new ClusterMapper().readCluster(new StringReader(clusterStr));
    ExecutorService executor = Executors.newFixedThreadPool(cluster.getNumberOfNodes());
    StoreSwapper swapper = null;
    AdminClient adminClient = null;

    DefaultHttpClient httpClient = null;
    if (useAdminServices) {
        adminClient = new AdminClient(cluster, new AdminClientConfig(), new ClientConfig());
        swapper = new AdminStoreSwapper(cluster, executor, adminClient, timeoutMs);
    } else {
        int numConnections = cluster.getNumberOfNodes() + 3;
        ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager();
        httpClient = new DefaultHttpClient(connectionManager);

        HttpParams clientParams = httpClient.getParams();

        connectionManager.setMaxTotal(numConnections);
        connectionManager.setDefaultMaxPerRoute(numConnections);
        HttpConnectionParams.setSoTimeout(clientParams, timeoutMs);

        swapper = new HttpStoreSwapper(cluster, executor, httpClient, mgmtPath);
    }

    try {
        long start = System.currentTimeMillis();
        if (rollbackStore) {
            swapper.invokeRollback(storeName, pushVersion.longValue());
        } else {
            swapper.swapStoreData(storeName, filePath, pushVersion.longValue());
        }
        long end = System.currentTimeMillis();
        logger.info("Succeeded on all nodes in " + ((end - start) / Time.MS_PER_SECOND) + " seconds.");
    } finally {
        if (useAdminServices && adminClient != null)
            adminClient.close();
        executor.shutdownNow();
        executor.awaitTermination(1, TimeUnit.SECONDS);
        VoldemortIOUtils.closeQuietly(httpClient);
    }
    System.exit(0);
}