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

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

Introduction

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

Prototype

public PoolingClientConnectionManager(final SchemeRegistry schemeRegistry, final long timeToLive,
            final TimeUnit tunit) 

Source Link

Usage

From source file:com.amazonaws.http.ConnectionManagerFactory.java

public static PoolingClientConnectionManager createPoolingClientConnManager(ClientConfiguration config,
        HttpParams httpClientParams) {/*from  w  ww . ja  va 2  s . co  m*/
    PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(
            SchemeRegistryFactory.createDefault(), config.getConnectionTTL(), TimeUnit.MILLISECONDS);
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnections());
    connectionManager.setMaxTotal(config.getMaxConnections());
    if (config.useReaper()) {
        IdleConnectionReaper.registerConnectionManager(connectionManager);
    }
    return connectionManager;
}

From source file:outfox.dict.contest.util.HttpToolKit.java

/**
 * @param maxConnectPerHost/*ww w  .j  a va  2s.c o m*/
 * @param maxConnection
 * @param connectTimeOut
 * @param socketTimeOut
 * @param cookiePolicy
 * @param isAutoRetry
 * @param redirect
 */
public HttpToolKit(int maxConnectPerHost, int maxConnection, int connectTimeOut, int socketTimeOut,
        String cookiePolicy, boolean isAutoRetry, boolean redirect) {
    Scheme https = new Scheme("https", 443, SSLSocketFactory.getSocketFactory());
    Scheme http = new Scheme("http", 80, PlainSocketFactory.getSocketFactory());
    SchemeRegistry sr = new SchemeRegistry();
    sr.register(https);
    sr.register(http);

    connectionManager = new PoolingClientConnectionManager(sr, socketTimeOut, TimeUnit.MILLISECONDS);
    connectionManager.setDefaultMaxPerRoute(maxConnectPerHost);
    connectionManager.setMaxTotal(maxConnection);
    HttpParams params = new BasicHttpParams();
    params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, connectTimeOut);
    params.setParameter(ClientPNames.COOKIE_POLICY, cookiePolicy);
    params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, redirect);
    params.setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, false);

    if (isAutoRetry) {
        client = new AutoRetryHttpClient(new DefaultHttpClient(connectionManager, params));
    } else {
        client = new DefaultHttpClient(connectionManager, params);
    }
}

From source file:com.aliyun.oss.common.comm.HttpClientFactory.java

public static PoolingClientConnectionManager createConnectionManager(ClientConfiguration config,
        HttpParams httpClientParams) {//ww  w . j  a v a2 s  . c  om
    PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(
            SchemeRegistryFactory.createDefault(), config.getConnectionTTL(), TimeUnit.MILLISECONDS);
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnections());
    connectionManager.setMaxTotal(config.getMaxConnections());
    if (config.isUseReaper()) {
        IdleConnectionReaper.registerConnectionManager(connectionManager);
    }
    return connectionManager;
}

From source file:monasca.common.middleware.HttpClientPoolFactory.java

HttpClientPoolFactory(String host, int port, boolean useHttps, int timeout, boolean clientAuth, String keyStore,
        String keyPass, String trustStore, String trustPass, String adminToken, int maxActive,
        long timeBetweenEvictionRunsMillis, long minEvictableIdleTimeMillis) {
    // Setup auth URL
    String protocol = useHttps ? "https://" : "http://";
    String urlStr = protocol + host + ":" + port;
    uri = URI.create(urlStr);/*from w ww  .  j a  va2s .  c  o m*/

    // Setup connection pool
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    if (protocol.startsWith("https")) {
        SSLSocketFactory sslf = sslFactory(keyStore, keyPass, trustStore, trustPass, clientAuth);
        schemeRegistry.register(new Scheme("https", port, sslf));
    } else {
        schemeRegistry.register(new Scheme("http", port, PlainSocketFactory.getSocketFactory()));
    }
    connMgr = new PoolingClientConnectionManager(schemeRegistry, minEvictableIdleTimeMillis,
            TimeUnit.MILLISECONDS);

    connMgr.setMaxTotal(maxActive);
    connMgr.setDefaultMaxPerRoute(maxActive);

    // Http connection timeout
    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);

    // Create a single client
    client = new DefaultHttpClient(connMgr, params);

    // Create and start the connection pool cleaner
    cleaner = new HttpPoolCleaner(connMgr, timeBetweenEvictionRunsMillis, minEvictableIdleTimeMillis);
    new Thread(cleaner).start();

}

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

/**
 * Initialize the connection pool/*w w  w .j  av a 2 s .  com*/
 */
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:org.codelibs.empros.agent.operation.rest.RestApiOperation.java

public RestApiOperation() {
    url = PropertiesUtil.getAsString(EMPROSAPI_PROPERTIES, "emprosUrl", null);

    if (StringUtil.isBlank(url)) {
        throw new EmprosSystemException("emprosUrl is empty.");
    }/*  w  w  w.j a  v  a2  s .c  om*/

    eventCapacity = PropertiesUtil.getAsInt(EMPROSAPI_PROPERTIES, "eventCapacity", 100);
    requestInterval = PropertiesUtil.getAsInt(EMPROSAPI_PROPERTIES, "requestInterval", 100);
    maxRetryCount = PropertiesUtil.getAsInt(EMPROSAPI_PROPERTIES, "maxRetryCount", 5);
    apiMonitorInterval = PropertiesUtil.getAsLong(EMPROSAPI_PROPERTIES, "apiMonitorInterval", 1 * 60 * 1000);

    final long connectionCheckInterval = PropertiesUtil.getAsLong(EMPROSAPI_PROPERTIES,
            "connectionCheckInterval", 5000);
    final long idleConnectionTimeout = PropertiesUtil.getAsLong(EMPROSAPI_PROPERTIES, "idleConnectionTimeout",
            60 * 1000);

    final SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    final ClientConnectionManager clientConnectionManager = new PoolingClientConnectionManager(schemeRegistry,
            5, TimeUnit.MINUTES);

    httpClient = new DefaultHttpClient(clientConnectionManager);
    HttpParams httpParams = httpClient.getParams();

    // TODO auth
    HttpConnectionParams.setConnectionTimeout(httpParams, 10 * 1000);
    HttpConnectionParams.setSoTimeout(httpParams, 10 * 1000);

    connectionMonitor = new ConnectionMonitor(clientConnectionManager, connectionCheckInterval,
            idleConnectionTimeout);
    connectionMonitor.setDaemon(true);
    connectionMonitor.start();

    apiMonitor = new ApiMonitor();
    apiMonitorTimer = new Timer();
    apiMonitorTimer.schedule(apiMonitor, 0, apiMonitorInterval);
}

From source file:com.flipkart.poseidon.handlers.http.impl.HttpConnectionPool.java

/** Constructor
 * @param host Host Name//from   w  ww. j a v  a 2s. c  o m
 * @param port Port Name
 * @param secure
 * @param connectionTimeout
 * @param operationTimeout
 * @param maxConnections
 * @param processQueueSize
 * @param timeToLiveInSecs
 */
protected HttpConnectionPool(final String name, String host, Integer port, Boolean secure,
        Integer connectionTimeout, Integer operationTimeout, Integer maxConnections, Integer processQueueSize,
        Integer timeToLiveInSecs) {
    this.name = name;
    this.host = host;
    this.port = port;
    this.secure = secure;
    this.headers = new HashMap<String, String>();
    this.processQueue = new Semaphore(processQueueSize + maxConnections);
    if (timeToLiveInSecs != null) {
        this.timeToLiveInSecs = timeToLiveInSecs;
    }
    this.requestGzipEnabled = false;
    this.responseGzipEnabled = false;

    // create scheme
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    if (this.secure) {
        schemeRegistry.register(new Scheme("https", port, SSLSocketFactory.getSocketFactory()));
    } else {
        schemeRegistry.register(new Scheme("http", port, PlainSocketFactory.getSocketFactory()));
    }

    // create connection manager
    PoolingClientConnectionManager cm;
    if (this.timeToLiveInSecs > 0) {
        cm = new PoolingClientConnectionManager(schemeRegistry, this.timeToLiveInSecs, 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);

    // policies (cookie)
    this.client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES);

    // adding gzip support for http client
    addGzipHeaderInRequestResponse();

}

From source file:org.alfresco.http.SharedHttpClientProvider.java

/**
 * See <a href="http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html">Connection management</a>.
 * /* w  w  w .  j ava  2 s .  co m*/
 * @param maxNumberOfConnections        the maximum number of Http connections in the pool
 * @param connectionTimeoutMs           the time to wait for a connection from the pool before failure
 * @param socketTimeoutMs               the time to wait for data activity on a connection before failure
 * @param socketTtlMs                   the time for a socket to remain alive before being forcibly closed (0 for infinite)
 */
public SharedHttpClientProvider(int maxNumberOfConnections, int connectionTimeoutMs, int socketTimeoutMs,
        int socketTtlMs) {
    SSLSocketFactory sslSf = null;
    try {
        TrustStrategy sslTs = new TrustAnyTrustStrategy();
        sslSf = new SSLSocketFactory(sslTs, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    } catch (Throwable e) {
        throw new RuntimeException("Unable to construct HttpClientProvider.", e);
    }

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

    httpClientCM = new PoolingClientConnectionManager(schemeRegistry, (long) socketTtlMs,
            TimeUnit.MILLISECONDS);
    // Increase max total connections
    httpClientCM.setMaxTotal(maxNumberOfConnections);
    // Ensure that we don't throttle on a per-scheme basis (BENCH-45)
    httpClientCM.setDefaultMaxPerRoute(maxNumberOfConnections);

    httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeoutMs);
    HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutMs);
    HttpConnectionParams.setTcpNoDelay(httpParams, true);
    HttpConnectionParams.setStaleCheckingEnabled(httpParams, true);
    HttpConnectionParams.setSoKeepalive(httpParams, true);

}

From source file:com.dgwave.osrs.OsrsClient.java

/**
 * Creates a connection manager from configuration - handles test mode
 * @return ClientConnectionManager//ww  w .j a v  a 2  s  .c o m
 * @throws OsrsException
 */
private ClientConnectionManager getConnectionManager() throws OsrsException {

    // Create and initialize scheme registry
    SchemeRegistry schemeRegistry = new SchemeRegistry();

    boolean testMode = "test".equals(OsrsConfig.getValue("osrs.environment"));
    int port = Integer.parseInt(OsrsConfig.getValue("osrs.port"));
    int sslPort = Integer.parseInt(OsrsConfig.getValue("osrs.sslPort"));
    schemeRegistry.register(new Scheme("http", port, PlainSocketFactory.getSocketFactory()));

    Scheme sslScheme;
    if (testMode) {
        try {
            sslScheme = new Scheme("https", sslPort, new SSLSocketFactory(getTestTrustStore()));
        } catch (Exception e) {
            throw new OsrsException("Error creating test SSL scheme", e);
        }
    } else {
        sslScheme = new Scheme("https", sslPort, SSLSocketFactory.getSocketFactory());
    }
    schemeRegistry.register(sslScheme);

    // Create an HttpClient with the PoolingClientConnManager (thread-safe).
    // This connection manager must be used if more than one thread will
    // be using the HttpClient.
    ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry, 60, TimeUnit.SECONDS);
    return cm;
}