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

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

Introduction

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

Prototype

public void setDefaultMaxPerRoute(final int max) 

Source Link

Usage

From source file:com.unboundid.scim.sdk.examples.ClientExample.java

/**
 * Create an SSL-enabled Wink client config from the provided information.
 * The returned client config may be used to create a SCIM service object.
 * IMPORTANT: This should not be used in production because no validation
 * is performed on the server certificate returned by the SCIM service.
 *
 * @param userName    The HTTP Basic Auth user name.
 * @param password    The HTTP Basic Auth password.
 *
 * @return  An Apache Wink client config.
 *//*from w ww  . j a  va  2  s. c  o m*/
public static ClientConfig createHttpBasicClientConfig(final String userName, final String password) {
    SSLSocketFactory sslSocketFactory;
    try {
        final SSLContext sslContext = SSLContext.getInstance("TLS");

        // Do not use these settings in production.
        sslContext.init(null, new TrustManager[] { new BlindTrustManager() }, new SecureRandom());
        sslSocketFactory = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    } catch (KeyManagementException e) {
        throw new RuntimeException(e.getLocalizedMessage());
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getLocalizedMessage());
    }

    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()));
    schemeRegistry.register(new Scheme("https", 443, sslSocketFactory));

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

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

From source file:de.onyxbits.raccoon.gplay.PlayManager.java

/**
 * create a proxy client/*from ww  w . ja  v  a2s.com*/
 * 
 * @return either a client or null if none is configured
 * @throws KeyManagementException
 * @throws NumberFormatException
 *           if that port could not be parsed.
 * @throws NoSuchAlgorithmException
 */
private static HttpClient createProxyClient(PlayProfile profile)
        throws KeyManagementException, NoSuchAlgorithmException {
    if (profile.getProxyAddress() == null) {
        return null;
    }

    PoolingClientConnectionManager connManager = new PoolingClientConnectionManager(
            SchemeRegistryFactory.createDefault());
    connManager.setMaxTotal(100);
    connManager.setDefaultMaxPerRoute(30);

    DefaultHttpClient client = new DefaultHttpClient(connManager);
    client.getConnectionManager().getSchemeRegistry().register(Utils.getMockedScheme());
    HttpHost proxy = new HttpHost(profile.getProxyAddress(), profile.getProxyPort());
    client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    if (profile.getProxyUser() != null && profile.getProxyPassword() != null) {
        client.getCredentialsProvider().setCredentials(new AuthScope(proxy),
                new UsernamePasswordCredentials(profile.getProxyUser(), profile.getProxyPassword()));
    }
    return client;
}

From source file:com.base.httpclient.HttpJsonClient.java

/**
 * httpClient//from   w w w  .  j  a v  a  2s.  co m
 * @return
 */
public static DefaultHttpClient getHttpClient() {
    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);
    HttpParams httpParams = new BasicHttpParams();
    cm.setMaxTotal(10);//   
    cm.setDefaultMaxPerRoute(5);// ? 
    HttpConnectionParams.setConnectionTimeout(httpParams, 60000);//
    HttpConnectionParams.setSoTimeout(httpParams, 60000);//?
    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUseExpectContinue(httpParams, false);
    httpParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES);
    DefaultHttpClient httpClient = new DefaultHttpClient(cm, httpParams);
    //httpClient.setCookieStore(null);
    httpClient.getCookieStore().clear();
    httpClient.getCookieStore().getCookies().clear();
    //   httpClient.setHttpRequestRetryHandler(new HttpJsonClient().new HttpRequestRetry());//?
    return httpClient;
}

From source file:com.ah.be.common.PresenceUtil.java

public static HttpClient getHttpClientInstance(int maxConnections) {
    try {/*  www.  j av  a 2  s  .c o m*/
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(null, new TrustManager[] { new ClientTrustManager() }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        schemeRegistry.register(new Scheme("https", 443, ssf));
        PoolingClientConnectionManager connMgr = new PoolingClientConnectionManager(schemeRegistry);
        connMgr.setMaxTotal(maxConnections);
        connMgr.setDefaultMaxPerRoute(maxConnections);

        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, SOCKET_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT);
        HttpClient httpClient = new DefaultHttpClient(connMgr, params);
        return httpClient;
    } catch (Exception e) {
        log.error("getHttpClientInstance error.", e);
        return null;
    }
}

From source file:com.google.apphosting.vmruntime.VmApiProxyDelegate.java

private static ClientConnectionManager createConnectionManager() {
    PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
    connectionManager.setMaxTotal(VmApiProxyEnvironment.MAX_CONCURRENT_API_CALLS);
    connectionManager.setDefaultMaxPerRoute(VmApiProxyEnvironment.MAX_CONCURRENT_API_CALLS);
    return connectionManager;
}

From source file:net.yacy.cora.federate.solr.instance.RemoteInstance.java

/**
 * Initialize the maximum connections for the given pool
 * /*from  w  w  w  .j a v a  2s .c om*/
 * @param pool
 *            a pooling connection manager. Must not be null.
 * @param maxConnections.
 *            The new maximum connections values. Must be greater than 0.
 * @throws IllegalArgumentException
 *             when pool is null or when maxConnections is lower than 1
 */
public static void initPoolMaxConnections(final org.apache.http.impl.conn.PoolingClientConnectionManager pool,
        int maxConnections) {
    if (pool == null) {
        throw new IllegalArgumentException("pool parameter must not be null");
    }
    if (maxConnections <= 0) {
        throw new IllegalArgumentException("maxConnections parameter must be greater than zero");
    }
    pool.setMaxTotal(maxConnections);

    /* max connections per host */
    pool.setDefaultMaxPerRoute((int) (2 * Memory.cores()));
}

From source file:com.rackspacecloud.blueflood.http.HttpClientVendor.java

private ClientConnectionManager buildConnectionManager(int concurrency) {
    final PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
    connectionManager.setDefaultMaxPerRoute(concurrency);
    connectionManager.setMaxTotal(concurrency);
    return connectionManager;
}

From source file:com.microsoft.applicationinsights.internal.channel.common.ApacheSender42.java

public ApacheSender42() {
    PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
    cm.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS);
    cm.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);

    httpClient = new DefaultHttpClient(cm);

    HttpParams params = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, REQUEST_TIMEOUT_IN_MILLIS);
    HttpConnectionParams.setSoTimeout(params, REQUEST_TIMEOUT_IN_MILLIS);

    InternalLogger.INSTANCE.info("Using Apache HttpClient 4.2");
}

From source file:com.seyren.core.service.checker.GraphiteTargetChecker.java

private ClientConnectionManager createConnectionManager() {
    PoolingClientConnectionManager manager = new PoolingClientConnectionManager();
    manager.setDefaultMaxPerRoute(MAX_CONNECTIONS_PER_ROUTE);
    return manager;
}

From source file:at.ac.tuwien.big.testsuite.impl.util.HttpClientProducer.java

@Produces
@ApplicationScoped//from  w  ww .  j a va  2  s.  c  o  m
HttpClient produceHttpClient() {
    PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
    cm.setMaxTotal(100);
    cm.setDefaultMaxPerRoute(20);
    DefaultHttpClient client = new DefaultHttpClient(cm);

    client.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        @Override
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            if (executionCount >= MAX_RETRY_COUNT) {
                // Do not retry if over max retry count
                return false;
            }
            if (exception instanceof InterruptedIOException) {
                // Timeout
                return true;
            }
            if (exception instanceof UnknownHostException) {
                // Unknown host
                return true;
            }
            if (exception instanceof ConnectException) {
                // Connection refused
                return true;
            }
            if (exception instanceof SSLException) {
                // SSL handshake exception
                return true;
            }
            HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
            boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
            if (idempotent) {
                // Retry if the request is considered idempotent 
                return true;
            }
            return false;
        }
    });

    return new DecompressingHttpClient(client);
}