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

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

Introduction

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

Prototype

public void setMaxPerRoute(final HttpRoute route, final int max) 

Source Link

Usage

From source file:org.yamj.core.tools.web.PoolingHttpClient.java

private void setRoute(HttpUriRequest request) throws ClientProtocolException {
    HttpHost httpHost = determineTarget(request);
    String key = httpHost.toString();

    synchronized (routedHosts) {
        if (!routedHosts.contains(key)) {
            String group = ".*";
            for (String searchGroup : groupLimits.keySet()) {
                if (key.matches(searchGroup) && searchGroup.length() > group.length()) {
                    group = searchGroup;

                }//ww w . j a  v  a  2s . co m
            }
            int maxRequests = groupLimits.get(group);

            LOG.debug("IO download host: {}; rule: {}, maxRequests: {}", key, group, maxRequests);
            routedHosts.add(key);

            HttpRoute httpRoute = new HttpRoute(httpHost);

            ClientConnectionManager conMan = this.getConnectionManager();
            if (conMan instanceof PoolingClientConnectionManager) {
                PoolingClientConnectionManager poolMan = (PoolingClientConnectionManager) conMan;
                poolMan.setMaxPerRoute(httpRoute, maxRequests);
            }
        }
    }
}

From source file:org.yamj.api.common.http.AbstractPoolingHttpClient.java

public void setRoute(HttpRoute httpRoute, int maxRequests) {
    ClientConnectionManager conMan = this.getConnectionManager();
    if (conMan instanceof PoolingClientConnectionManager) {
        PoolingClientConnectionManager poolMan = (PoolingClientConnectionManager) conMan;
        poolMan.setMaxPerRoute(httpRoute, maxRequests);
    }//from  w w w  .j  a  va 2  s . c  o m
}

From source file:nextflow.fs.dx.api.DxHttpClient.java

/**
 * Creates the http client using a thread safe {@code PoolingClientConnectionManager}
 *
 * See http://hc.apache.org/httpcomponents-client-4.2.x/tutorial/html/connmgmt.html#d5e581
 *
 * @authors Paolo Di Tommaso <paolo.ditommaso@gmail.com>
 *
 * @return/*www. j  av a 2 s  .  co  m*/
 */
protected DxHttpClient() {
    log.debug("Creating DxHttpClient object");
    try {
        DxEnv env = DxEnv.getInstance();
        securityContext = env.getSecurityContext();
        apiserver = env.getApiserverPath();

        final String PROT = env.getApiserverProtocol();
        final String HOST = env.getApiserverHost();
        final int PORT = env.getApiserverPort();

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        SchemeSocketFactory factory = "https".equals(PROT) ? SSLSocketFactory.getSocketFactory()
                : PlainSocketFactory.getSocketFactory();
        schemeRegistry.register(new Scheme(PROT, PORT, factory));

        PoolingClientConnectionManager manager = new PoolingClientConnectionManager(schemeRegistry);
        manager.setMaxTotal(100);
        manager.setDefaultMaxPerRoute(10);

        manager.setMaxPerRoute(new HttpRoute(new HttpHost(HOST, PORT)), 50);

        httpclient = new DefaultHttpClient(manager);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

}

From source file:org.n52.oxf.util.web.PoolingConnectionManagerHttpClient.java

@Override
public ClientConnectionManager getConnectionManager() {
    PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
    // Increase max total connection to 200
    cm.setMaxTotal(200);/*w ww . j a va 2 s .  com*/
    // Increase default max connection per route to 20
    cm.setDefaultMaxPerRoute(20);
    // Increase max connections for localhost:80 to 50
    HttpHost localhost8080 = new HttpHost("localhost", 8080);
    cm.setMaxPerRoute(new HttpRoute(localhost8080), 50);
    HttpHost localhost = new HttpHost("localhost", 80);
    cm.setMaxPerRoute(new HttpRoute(localhost), 50);

    return cm;
}

From source file:org.ovirt.engine.sdk.web.ConnectionsPoolBuilder.java

/**
 * Creates PoolingClientConnectionManager
 *
 * @param url//ww  w.  j  a  v  a2  s  . c o  m
 * @param port
 *
 * @return {@link ClientConnectionManager}
 */
private ClientConnectionManager createPoolingClientConnectionManager(String url, int port) {
    SchemeRegistry schemeRegistry = createSchemeRegistry(url, port);

    PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
    cm.setMaxTotal(MAX_CONNECTIONS);
    cm.setDefaultMaxPerRoute(MAX_CONNECTIONS_PER_ROUTE);
    cm.setMaxPerRoute(new HttpRoute(new HttpHost(getHost(url), getPort(url, port))), MAX_CONNECTIONS_PER_HOST);

    return cm;
}

From source file:com.da.daum.DaumCafeBungImgList.java

private HttpClient getPoolHttpClient() {
    HttpClient httpclient;/*from  w  w  w .  j  a v  a  2s. com*/
    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);
    // Increase max total connection to 200
    cm.setMaxTotal(200);
    // Increase default max connection per route to 20
    cm.setDefaultMaxPerRoute(20);
    // Increase max connections for localhost:80 to 50
    HttpHost localhost = new HttpHost("locahost", 80);
    cm.setMaxPerRoute(new HttpRoute(localhost), 50);
    //DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient = new DefaultHttpClient(cm);
    return httpclient;
}

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

/**
 * Initialize the connection pool// w w w  .  j  a  v  a 2 s  .  c om
 */
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:com.flipkart.poseidon.handlers.http.impl.HttpConnectionPool.java

/** Constructor
 * @param host Host Name/* w w w .  j  av a  2 s  .  c  om*/
 * @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.craftercms.profile.impl.ProfileRestClientService.java

private DefaultHttpClient getHttpClient(int connectionTimeOut, int sockeTimeOut) {
    try {//w  ww.ja  v  a  2s. c o  m

        HttpParams httpParams = new BasicHttpParams();

        setParams(httpParams, connectionTimeOut, sockeTimeOut);

        SSLSocketFactory sf = new SSLSocketFactory(new TrustStrategy() {
            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                return true;
            }
        }, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", port, PlainSocketFactory.getSocketFactory()));
        registry.register(new Scheme("https", sslPort, sf));

        PoolingClientConnectionManager ccm = new PoolingClientConnectionManager(registry);
        HttpHost localhost = new HttpHost(host, port);
        ccm.setMaxPerRoute(new HttpRoute(localhost), maxPerRoute);
        ccm.setMaxTotal(maxTotal);
        ccm.setDefaultMaxPerRoute(defaultMaxPerRoute);
        return new DefaultHttpClient(ccm, httpParams);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return new DefaultHttpClient();
    }
}

From source file:rapture.common.client.BaseHttpApi.java

private static HttpClient 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);
    // Increase max total connection to 200
    cm.setMaxTotal(200);/*from   ww  w  . j  a  v  a 2  s .c o m*/
    // Increase default max connection per route to 20
    cm.setDefaultMaxPerRoute(20);
    // Increase max connections for localhost:80 to 50
    HttpHost localhost = new HttpHost("locahost", 80);
    cm.setMaxPerRoute(new HttpRoute(localhost), 50);

    DefaultHttpClient httpClient = new DefaultHttpClient(cm);
    // Use a proxy if it is defined - we need to pass this on to the
    // HttpClient
    if (System.getProperties().containsKey("http.proxyHost")) {
        String host = System.getProperty("http.proxyHost");
        String port = System.getProperty("http.proxyPort", "8080");
        HttpHost proxy = new HttpHost(host, Integer.parseInt(port));
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }
    httpClient.addRequestInterceptor(new HttpRequestInterceptor() {

        @Override
        public void process(HttpRequest request, HttpContext arg1) throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip");
            }
        }

    });

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        @Override
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            if (log.isTraceEnabled()) {
                log.trace("Response Headers:");
                for (Header h : response.getAllHeaders()) {
                    log.trace(h.getName() + " : " + h.getValue());
                }
            }
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                Header ceheader = entity.getContentEncoding();
                if (ceheader != null) {
                    HeaderElement[] codecs = ceheader.getElements();
                    for (int i = 0; i < codecs.length; i++) {
                        if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                            response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                            return;
                        }
                    }
                }
            }
        }

    });

    return httpClient;
}