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:org.apache.marmotta.platform.core.services.http.HttpClientServiceImpl.java

@PostConstruct
protected void initialize() {
    try {// w w  w  . j  a v  a2 s . c  o m
        lock.writeLock().lock();

        httpParams = new BasicHttpParams();
        String userAgentString = "Apache Marmotta/"
                + configurationService.getStringConfiguration("kiwi.version") + " (running at "
                + configurationService.getServerUri() + ")" + " lmf-core/"
                + configurationService.getStringConfiguration("kiwi.version");
        userAgentString = configurationService.getStringConfiguration("core.http.user_agent", userAgentString);

        httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT,
                configurationService.getIntConfiguration("core.http.so_timeout", 60000));
        httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
                configurationService.getIntConfiguration("core.http.connection_timeout", 10000));

        httpParams.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true);
        httpParams.setIntParameter(ClientPNames.MAX_REDIRECTS, 3);

        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);
        cm.setMaxTotal(configurationService.getIntConfiguration(CoreOptions.HTTP_MAX_CONNECTIONS, 20));
        cm.setDefaultMaxPerRoute(
                configurationService.getIntConfiguration(CoreOptions.HTTP_MAX_CONNECTIONS_PER_ROUTE, 10));

        final DefaultHttpClient hc = new DefaultHttpClient(cm, httpParams);
        hc.setRedirectStrategy(new LMFRedirectStrategy());
        hc.setHttpRequestRetryHandler(new LMFHttpRequestRetryHandler());
        hc.removeRequestInterceptorByClass(org.apache.http.protocol.RequestUserAgent.class);
        hc.addRequestInterceptor(new LMFRequestUserAgent(userAgentString));

        if (configurationService.getBooleanConfiguration(CoreOptions.HTTP_CLIENT_CACHE_ENABLE, true)) {
            CacheConfig cacheConfig = new CacheConfig();
            // FIXME: Hardcoded constants - is this useful?
            cacheConfig.setMaxCacheEntries(1000);
            cacheConfig.setMaxObjectSize(81920);

            final HttpCacheStorage cacheStore = new MapHttpCacheStorage(httpCache);

            this.httpClient = new MonitoredHttpClient(new CachingHttpClient(hc, cacheStore, cacheConfig));
        } else {
            this.httpClient = new MonitoredHttpClient(hc);
        }
        bytesSent.set(0);
        bytesReceived.set(0);
        requestsExecuted.set(0);

        idleConnectionMonitorThread = new IdleConnectionMonitorThread(httpClient.getConnectionManager());
        idleConnectionMonitorThread.start();

        StatisticsProvider stats = new StatisticsProvider(cm);
        statisticsService.registerModule(HttpClientService.class.getSimpleName(), stats);
    } finally {
        lock.writeLock().unlock();
    }
}

From source file:API.amazon.mws.orders.MarketplaceWebServiceOrdersClient.java

/**
 * Configure HttpClient with set of defaults as well as configuration from
 * MarketplaceWebServiceProductsConfig instance
 * //from   w  w  w  .j a  v a2 s .c om
 */
private HttpClient configureHttpClient(String applicationName, String applicationVersion) {

    // respect a user-provided User-Agent header as-is, but if none is provided
    // then generate one satisfying the MWS User-Agent requirements
    if (config.getUserAgent() == null) {
        config.setUserAgent(quoteAppName(applicationName), quoteAppVersion(applicationVersion),
                quoteAttributeValue("Java/" + System.getProperty("java.version") + "/"
                        + System.getProperty("java.class.version") + "/" + System.getProperty("java.vendor")),

                quoteAttributeName("Platform"),
                quoteAttributeValue("" + System.getProperty("os.name") + "/" + System.getProperty("os.arch")
                        + "/" + System.getProperty("os.version")),

                quoteAttributeName("MWSClientVersion"), quoteAttributeValue(clientVersion));
    }

    defaultHeaders.add(new BasicHeader("X-Amazon-User-Agent", config.getUserAgent()));

    /* Set http client parameters */
    BasicHttpParams httpParams = new BasicHttpParams();

    httpParams.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgent());

    /* Set connection parameters */
    HttpConnectionParams.setConnectionTimeout(httpParams, 50000);
    HttpConnectionParams.setSoTimeout(httpParams, 50000);
    HttpConnectionParams.setStaleCheckingEnabled(httpParams, true);
    HttpConnectionParams.setTcpNoDelay(httpParams, true);

    /* Set connection manager */
    PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
    connectionManager.setMaxTotal(config.getMaxConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnections());

    /* Set http client */
    httpClient = new DefaultHttpClient(connectionManager, httpParams);
    httpContext = new BasicHttpContext();

    /* Set proxy if configured */
    if (config.isSetProxyHost() && config.isSetProxyPort()) {
        log.info("Configuring Proxy. Proxy Host: " + config.getProxyHost() + "Proxy Port: "
                + config.getProxyPort());
        final HttpHost hostConfiguration = new HttpHost(config.getProxyHost(), config.getProxyPort(),
                (usesHttps(config.getServiceURL()) ? "https" : "http"));
        httpContext = new BasicHttpContext();
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, hostConfiguration);

        if (config.isSetProxyUsername() && config.isSetProxyPassword()) {
            credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));
            httpContext.setAttribute(ClientContext.CREDS_PROVIDER, credentialsProvider);

        }

    }

    return httpClient;
}

From source file:com.ngdata.hbaseindexer.supervisor.IndexerSupervisor.java

private void startIndexer(IndexerDefinition indexerDef) {
    IndexerHandle handle = null;/*from   w  ww .  j a v  a 2  s . co  m*/
    SolrServer solr = null;

    String indexerProcessId = null;
    try {

        // If this is an update and the indexer failed to start, it's still in the registry as a
        // failed process
        if (indexerProcessIds.containsKey(indexerDef.getName())) {
            indexerProcessRegistry.unregisterIndexerProcess(indexerProcessIds.remove(indexerDef.getName()));
        }

        indexerProcessId = indexerProcessRegistry.registerIndexerProcess(indexerDef.getName(), hostName);
        indexerProcessIds.put(indexerDef.getName(), indexerProcessId);

        // Create and register the indexer
        IndexerComponentFactory factory = IndexerComponentFactoryUtil.getComponentFactory(
                indexerDef.getIndexerComponentFactory(),
                new ByteArrayInputStream(indexerDef.getConfiguration()), indexerDef.getConnectionParams());
        IndexerConf indexerConf = factory.createIndexerConf();

        ResultToSolrMapper mapper = factory.createMapper(indexerDef.getName());

        Sharder sharder = null;
        SolrInputDocumentWriter solrWriter;
        PoolingClientConnectionManager connectionManager = null;

        if (indexerDef.getConnectionType() == null || indexerDef.getConnectionType().equals("solr")) {
            Map<String, String> connectionParams = indexerDef.getConnectionParams();
            String solrMode = SolrConnectionParamUtil.getSolrMode(connectionParams);
            if (solrMode.equals("cloud")) {
                solrWriter = new DirectSolrInputDocumentWriter(indexerDef.getName(),
                        createCloudSolrServer(connectionParams));
            } else if (solrMode.equals("classic")) {
                connectionManager = new PoolingClientConnectionManager();
                connectionManager.setDefaultMaxPerRoute(getSolrMaxConnectionsPerRoute(connectionParams));
                connectionManager.setMaxTotal(getSolrMaxConnectionsTotal(connectionParams));

                httpClient = new DefaultHttpClient(connectionManager);
                List<SolrServer> solrServers = createHttpSolrServers(connectionParams, httpClient);
                solrWriter = new DirectSolrClassicInputDocumentWriter(indexerDef.getName(), solrServers);
                sharder = createSharder(connectionParams, solrServers.size());
            } else {
                throw new RuntimeException(
                        "Only 'cloud' and 'classic' are valid values for solr.mode, but got " + solrMode);
            }
        } else {
            throw new RuntimeException("Invalid connection type: " + indexerDef.getConnectionType()
                    + ". Only 'solr' is supported");
        }

        Indexer indexer = Indexer.createIndexer(indexerDef.getName(), indexerConf, indexerConf.getTable(),
                mapper, htablePool, sharder, solrWriter);
        IndexingEventListener eventListener = new IndexingEventListener(indexer, indexerConf.getTable());

        int threads = hbaseConf.getInt("hbaseindexer.indexer.threads", 10);
        SepConsumer sepConsumer = new SepConsumer(indexerDef.getSubscriptionId(),
                indexerDef.getSubscriptionTimestamp(), eventListener, threads, hostName, zk, hbaseConf, null);

        handle = new IndexerHandle(indexerDef, indexer, sepConsumer, solr, connectionManager);
        handle.start();

        indexers.put(indexerDef.getName(), handle);
        indexerRegistry.register(indexerDef.getName(), indexer);

        log.info("Started indexer for " + indexerDef.getName());
    } catch (Throwable t) {
        if (t instanceof InterruptedException) {
            Thread.currentThread().interrupt();
        }

        log.error("Problem starting indexer " + indexerDef.getName(), t);

        try {
            if (indexerProcessId != null) {
                indexerProcessRegistry.setErrorStatus(indexerProcessId, t);
            }
        } catch (Exception e) {
            log.error("Error setting error status on indexer process " + indexerProcessId, e);
        }

        if (handle != null) {
            // stop any listeners that might have been started
            try {
                handle.stop();
            } catch (Throwable t2) {
                if (t2 instanceof InterruptedException) {
                    Thread.currentThread().interrupt();
                }
                log.error(
                        "Problem stopping consumers for failed-to-start indexer '" + indexerDef.getName() + "'",
                        t2);
            }
        } else {
            // Might be the handle was not yet created, but the solr connection was
            Closer.close(solr);
        }
    }
}

From source file:API.amazon.mws.products.MarketplaceWebServiceProductsClient.java

/**
 * Configure HttpClient with set of defaults as well as configuration from
 * MarketplaceWebServiceProductsConfig instance
 * //w w  w.j  a  v a 2s  . c  om
 */

private HttpClient configureHttpClient(String applicationName, String applicationVersion) {

    // respect a user-provided User-Agent header as-is, but if none is provided
    // then generate one satisfying the MWS User-Agent requirements
    if (config.getUserAgent() == null) {
        config.setUserAgent(quoteAppName(applicationName), quoteAppVersion(applicationVersion),
                quoteAttributeValue("Java/" + System.getProperty("java.version") + "/"
                        + System.getProperty("java.class.version") + "/" + System.getProperty("java.vendor")),

                quoteAttributeName("Platform"),
                quoteAttributeValue("" + System.getProperty("os.name") + "/" + System.getProperty("os.arch")
                        + "/" + System.getProperty("os.version")),

                quoteAttributeName("MWSClientVersion"), quoteAttributeValue(clientVersion));
    }

    defaultHeaders.add(new BasicHeader("X-Amazon-User-Agent", config.getUserAgent()));

    /* Set http client parameters */
    BasicHttpParams httpParams = new BasicHttpParams();

    httpParams.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgent());

    /* Set connection parameters */
    HttpConnectionParams.setConnectionTimeout(httpParams, 50000);
    HttpConnectionParams.setSoTimeout(httpParams, 50000);
    HttpConnectionParams.setStaleCheckingEnabled(httpParams, true);
    HttpConnectionParams.setTcpNoDelay(httpParams, true);

    /* Set connection manager */
    PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
    connectionManager.setMaxTotal(config.getMaxConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnections());

    /* Set http client */
    httpClient = new DefaultHttpClient(connectionManager, httpParams);
    httpContext = new BasicHttpContext();

    /* Set proxy if configured */
    if (config.isSetProxyHost() && config.isSetProxyPort()) {
        log.info("Configuring Proxy. Proxy Host: " + config.getProxyHost() + "Proxy Port: "
                + config.getProxyPort());
        final HttpHost hostConfiguration = new HttpHost(config.getProxyHost(), config.getProxyPort(),
                (usesHttps(config.getServiceURL()) ? "https" : "http"));
        httpContext = new BasicHttpContext();
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, hostConfiguration);

        if (config.isSetProxyUsername() && config.isSetProxyPassword()) {
            credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));
            httpContext.setAttribute(ClientContext.CREDS_PROVIDER, credentialsProvider);

        }

    }

    return httpClient;
}

From source file:org.lightcouch.CouchDbClientBase.java

/**
 * @return {@link DefaultHttpClient} instance.
 *///from  w  w  w.j a  v  a 2  s.  c o m
private HttpClient createHttpClient(CouchDbProperties props) {
    DefaultHttpClient httpclient = null;
    try {
        SchemeSocketFactory ssf = null;
        if (props.getProtocol().equals("https")) {
            TrustManager trustManager = new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }

                public void checkServerTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }

                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            SSLContext sslcontext = SSLContext.getInstance("TLS");
            sslcontext.init(null, new TrustManager[] { trustManager }, null);
            ssf = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            SSLSocket socket = (SSLSocket) ssf.createSocket(null);
            socket.setEnabledCipherSuites(new String[] { "SSL_RSA_WITH_RC4_128_MD5" });
        } else {
            ssf = PlainSocketFactory.getSocketFactory();
        }
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme(props.getProtocol(), props.getPort(), ssf));
        PoolingClientConnectionManager ccm = new PoolingClientConnectionManager(schemeRegistry);
        httpclient = new DefaultHttpClient(ccm);
        host = new HttpHost(props.getHost(), props.getPort(), props.getProtocol());
        context = new BasicHttpContext();
        // Http params
        httpclient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
        httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, props.getSocketTimeout());
        httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
                props.getConnectionTimeout());
        int maxConnections = props.getMaxConnections();
        if (maxConnections != 0) {
            ccm.setMaxTotal(maxConnections);
            ccm.setDefaultMaxPerRoute(maxConnections);
        }
        if (props.getProxyHost() != null) {
            HttpHost proxy = new HttpHost(props.getProxyHost(), props.getProxyPort());
            httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        }
        // basic authentication
        if (props.getUsername() != null && props.getPassword() != null) {
            httpclient.getCredentialsProvider().setCredentials(new AuthScope(props.getHost(), props.getPort()),
                    new UsernamePasswordCredentials(props.getUsername(), props.getPassword()));
            props.clearPassword();
            AuthCache authCache = new BasicAuthCache();
            BasicScheme basicAuth = new BasicScheme();
            authCache.put(host, basicAuth);
            context.setAttribute(ClientContext.AUTH_CACHE, authCache);
        }
        // request interceptor
        httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
            public void process(final HttpRequest request, final HttpContext context) throws IOException {
                if (log.isInfoEnabled())
                    log.info(">> " + request.getRequestLine());
            }
        });
        // response interceptor
        httpclient.addResponseInterceptor(new HttpResponseInterceptor() {
            public void process(final HttpResponse response, final HttpContext context) throws IOException {
                validate(response);
                if (log.isInfoEnabled())
                    log.info("<< Status: " + response.getStatusLine().getStatusCode());
            }
        });
    } catch (Exception e) {
        log.error("Error Creating HTTP client. " + e.getMessage());
        throw new IllegalStateException(e);
    }
    return httpclient;
}

From source file:org.wso2.carbon.analytics.api.internal.client.AnalyticsAPIHttpClient.java

private AnalyticsAPIHttpClient(String protocol, String hostname, int port, int maxPerRoute, int maxConnection,
        int socketTimeout, int connectionTimeout, String trustStoreLocation, String trustStorePassword) {
    this.hostname = hostname;
    this.port = port;
    this.protocol = protocol;
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    if (this.protocol.equalsIgnoreCase(AnalyticsDataConstants.HTTP_PROTOCOL)) {
        schemeRegistry.register(new Scheme(this.protocol, port, PlainSocketFactory.getSocketFactory()));
    } else {//  ww w . ja  v a  2s.c  o  m
        System.setProperty(AnalyticsDataConstants.SSL_TRUST_STORE_SYS_PROP, trustStoreLocation);
        System.setProperty(AnalyticsDataConstants.SSL_TRUST_STORE_PASSWORD_SYS_PROP, trustStorePassword);
        schemeRegistry.register(new Scheme(this.protocol, port, SSLSocketFactory.getSocketFactory()));
    }
    PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(schemeRegistry);
    connectionManager.setDefaultMaxPerRoute(maxPerRoute);
    connectionManager.setMaxTotal(maxConnection);
    BasicHttpParams params = new BasicHttpParams();
    params.setParameter(AllClientPNames.SO_TIMEOUT, socketTimeout);
    params.setParameter(AllClientPNames.CONNECTION_TIMEOUT, connectionTimeout);
    this.httpClient = new DefaultHttpClient(connectionManager, params);
    gson = new GsonBuilder().create();
}

From source file:edu.ehu.galan.lite.utils.wikiminer.WikiminnerHelper.java

private WikiminnerHelper(String pPropDirs) {
    //PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    // Increase max total connection to 200
    // cm.setMaxTotal(100);
    // Increase default max connection per route to 20
    // cm.setDefaultMaxPerRoute(20);
    // Increase max connections for localhost:80 to 50
    PoolingClientConnectionManager pm = new PoolingClientConnectionManager();
    pm.setDefaultMaxPerRoute(20);
    pm.setMaxTotal(200);/*from   w  w w  .  j  a  va  2  s . c om*/
    httpClient = new DefaultHttpClient(pm);
    //        ConnectionConfig connectionConfig = ConnectionConfig.custom()
    //                .setMalformedInputAction(CodingErrorAction.IGNORE)
    //                .setUnmappableInputAction(CodingErrorAction.IGNORE)
    //                .setCharset(Consts.UTF_8).build();
    //        cm.setDefaultConnectionConfig(connectionConfig);

    //        httpClient = HttpClients.custom()
    //                .setConnectionManager(cm)
    //                .build();
    httpClient.getParams().setParameter("http.protocol.content-charset", "UTF-8");
    cache = CacheManager.getInstance().getCache("LiteCache");
    props = new Properties();
    caches = new Caches();
    try {
        props.load(new FileInputStream(new File(pPropDirs + "lite/configs/general.conf")));
        wikiminerUrl = props.getProperty("serviceUrl");
        maxTopics = Integer.parseInt(props.getProperty("maxTopics"));
    } catch (IOException ex) {
        logger.error("Error while setting WikiminerHelper properties files, check dirs", ex);
    }
    localMode = !props.get("localMode").equals("false");
}

From source file:com.emc.esu.api.rest.EsuRestApiApache.java

/**
 * Creates a new EsuRestApiApache object.
 * //  w w w . j av  a  2  s  . c o  m
 * @param host the hostname or IP address of the ESU server
 * @param port the port on the server to communicate with. Generally this is
 *            80 for HTTP and 443 for HTTPS.
 * @param uid the username to use when connecting to the server
 * @param sharedSecret the Base64 encoded shared secret to use to sign
 *            requests to the server.
 */
public EsuRestApiApache(String host, int port, String uid, String sharedSecret) {
    super(host, port, uid, sharedSecret);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("https", port, SSLSocketFactory.getSocketFactory()));
    schemeRegistry.register(new Scheme("http", port, PlainSocketFactory.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(200);

    httpClient = new DefaultHttpClient(cm, null);
}