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

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

Introduction

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

Prototype

public PoolingHttpClientConnectionManager() 

Source Link

Usage

From source file:org.kaaproject.kaa.server.verifiers.facebook.verifier.FacebookUserVerifier.java

@Override
public void start() {
    LOG.info("facebook user verifier started");
    tokenVerifiersPool = new ThreadPoolExecutor(0, configuration.getMaxParallelConnections(),
            MAX_SEC_FACEBOOK_REQUEST_TIME, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();
    // Increase max total connection
    connectionManager.setMaxTotal(configuration.getMaxParallelConnections());
}

From source file:com.basistech.rosette.api.HttpRosetteAPI.java

private void initClient(String key, List<Header> additionalHeaders) {
    HttpClientBuilder builder = HttpClients.custom();
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(connectionConcurrency);
    builder.setConnectionManager(cm);//from w  ww  .jav  a  2 s.c  o  m

    initHeaders(key, additionalHeaders);
    builder.setDefaultHeaders(this.additionalHeaders);

    httpClient = builder.build();
    this.additionalHeaders = new ArrayList<>();
}

From source file:com.uber.stream.kafka.mirrormaker.manager.core.ControllerHelixManager.java

public ControllerHelixManager(SourceKafkaClusterValidationManager srcKafkaValidationManager,
        ManagerConf managerConf) {//  w w  w. j  a v a 2  s.  c o  m
    _conf = managerConf;
    _enableRebalance = managerConf.getEnableRebalance();
    _srcKafkaValidationManager = srcKafkaValidationManager;
    _initMaxNumPartitionsPerRoute = managerConf.getInitMaxNumPartitionsPerRoute();
    _maxNumPartitionsPerRoute = managerConf.getMaxNumPartitionsPerRoute();
    _initMaxNumWorkersPerRoute = managerConf.getInitMaxNumWorkersPerRoute();
    _maxNumWorkersPerRoute = managerConf.getMaxNumWorkersPerRoute();
    _workloadRefreshPeriodInSeconds = managerConf.getWorkloadRefreshPeriodInSeconds();
    _workerHelixManager = new WorkerHelixManager(managerConf);
    _pipelineWorkloadMap = new ConcurrentHashMap<>();
    _helixZkURL = HelixUtils.getAbsoluteZkPathForHelix(managerConf.getManagerZkStr());
    _helixClusterName = MANAGER_CONTROLLER_HELIX_PREFIX + "-" + managerConf.getManagerDeployment();
    _instanceId = managerConf.getManagerInstanceId();
    _topicToPipelineInstanceMap = new ConcurrentHashMap<>();
    _pipelineToInstanceMap = new ConcurrentHashMap<>();
    _availableControllerList = new ArrayList<>();
    _routeToCounterMap = new ConcurrentHashMap<>();
    _zkClient = new ZkClient(_helixZkURL, 30000, 30000, ZKStringSerializer$.MODULE$);
    registerMetrics();

    PoolingHttpClientConnectionManager limitedConnMgr = new PoolingHttpClientConnectionManager();
    // TODO: make it configurable
    limitedConnMgr.setDefaultMaxPerRoute(100);
    limitedConnMgr.setMaxTotal(100);
    _httpClient = HttpClients.createMinimal(limitedConnMgr);
    _controllerPort = managerConf.getControllerPort();
    // requestConfig is immutable. These three timeouts are for
    // 1. getting connection from connection manager;
    // 2. establishing connection with server;
    // 3. getting next data snippet from server.
    _requestConfig = RequestConfig.custom().setConnectionRequestTimeout(30000).setConnectTimeout(30000)
            .setSocketTimeout(30000).build();
}

From source file:com.normalexception.app.rx8club.html.LoginFactory.java

/**
 * Initialize the client, cookie store, and context
 *//*from  w  ww . j a  v a2s.co  m*/
private void initializeClientInformation() {
    Log.d(TAG, "Initializing Client...");

    /*
    Log.d(TAG, "Creating Custom Cache Configuration");
    CacheConfig cacheConfig = CacheConfig.custom()
      .setMaxCacheEntries(1000)
      .setMaxObjectSize(8192)
      .build();
    */

    Log.d(TAG, "Creating Custom Request Configuration");
    RequestConfig rConfig = RequestConfig.custom().setCircularRedirectsAllowed(true)
            .setConnectionRequestTimeout(TIMEOUT).setSocketTimeout(TIMEOUT).build();

    cookieStore = new BasicCookieStore();
    httpContext = new BasicHttpContext();

    Log.d(TAG, "Building Custom HTTP Client");
    HttpClientBuilder httpclientbuilder = HttpClients.custom();
    //httpclientbuilder.setCacheConfig(cacheConfig);
    httpclientbuilder.setDefaultRequestConfig(rConfig);
    httpclientbuilder.setDefaultCookieStore(cookieStore);

    Log.d(TAG, "Connection Manager Initializing");
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(200);
    cm.setDefaultMaxPerRoute(20);
    httpclientbuilder.setConnectionManager(cm);

    Log.d(TAG, "Enable GZIP Compression");
    httpclientbuilder.addInterceptorLast(new HttpRequestInterceptor() {

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

    });
    httpclientbuilder.addInterceptorLast(new HttpResponseInterceptor() {

        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            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;
                        }
                    }
                }
            }
        }

    });

    // Follow Redirects
    Log.d(TAG, "Registering Redirect Strategy");
    httpclientbuilder.setRedirectStrategy(new RedirectStrategy());

    // Setup retry handler
    Log.d(TAG, "Registering Retry Handler");
    httpclientbuilder.setRetryHandler(new RetryHandler());

    // Setup KAS
    Log.d(TAG, "Registering Keep Alive Strategy");
    httpclientbuilder.setKeepAliveStrategy(new KeepAliveStrategy());

    httpclient = httpclientbuilder.build();

    //httpclient.log.enableDebug(
    //      MainApplication.isHttpClientLogEnabled());

    isInitialized = true;
}

From source file:com.zacwolf.commons.wbxcon.WBXCONorg.java

/**
 * Class <code>Contructor</code> initializes WBXCONorg instance for the given managed org (domain) instance.
 * As part of initialization the Constructor makes a call to establish orgID and namespaceID for the domain.
 * /*w  w  w. ja v a2s  . co m*/
 * The REST API calls are made via https GET and POST.  As such, the <code>HTTPSCLIENT</code> needs to be
 * initialized via a certificate stored in a default keystore.  Since the keystore contains a "static"
 * certificate provided by WebEx Connect, the keystore is generated "in source".  If WebEx Connect modifies
 * their default https certificate, you will need to download the latest version of this package from:<br />
 * <br />
 * <a href="https://github.com/ZacWolf/com.zacwolf.commons">https://github.com/ZacWolf/com.zacwolf.commons</a>
 * 
 * 
 * Whatever user is specified for wapiUSER, the following special privileges need to be granted to the account:
 * 
 * WBX:ManageDomain
 * WBX:ManageUsers
 * WBX:ManageRoles
 * 
 * @param domain_name   Name of the WebEx Connect Managed Org
 * @param wapiAUTHURL   (optional) URL used to override the default URL used to generate the initial login token
 * @param wapiUSER      WebEx UserName to use in making the REST calls
 * @param wapiPASS      WebEx user password to use in making the REST calls
 * @throws WBXCONexception
 */
WBXCONorg(final String domain_name, final String wapiAUTHURL, final String wapiUSER, final String wapiPASS)
        throws WBXCONexception {
    if (HTTPSCLIENT == null)
        try {
            //Quiet the various apache http client loggers
            Logger.getLogger("org.apache.http").setLevel(Level.SEVERE);
            Logger.getLogger("org.apache.http.wire").setLevel(Level.SEVERE);
            Logger.getLogger("org.apache.http.headers").setLevel(Level.SEVERE);
            System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
            System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
            System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "ERROR");
            System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "ERROR");
            System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.headers", "ERROR");

            final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
            cm.setMaxTotal(MAX_HTTP_REQUESTS);
            final KeyStore trustStore = KeyStore.getInstance("JCEKS");
            // Use the default keystore that is in the same package directory
            final InputStream instream = WBXCONorg.class.getClassLoader().getResourceAsStream(
                    WBXCONorg.class.getPackage().getName().replaceAll("\\.", "/") + "/" + TRUSTSTOREFILENAME);
            try {
                trustStore.load(instream, TRUSTSTOREPASS.toCharArray());
            } finally {
                instream.close();
            }
            final SSLContext sslcontext = SSLContexts.custom()// Trust own CA and all self-signed certs
                    .loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();
            final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,
                    new String[] { "TLSv1" }, // Allow TLSv1 protocol only
                    null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
            final RequestConfig config = RequestConfig.custom().setConnectTimeout(HTTP_TIMEOUT * 60000)
                    .setConnectionRequestTimeout(HTTP_TIMEOUT * 60000).setSocketTimeout(HTTP_TIMEOUT * 60000)
                    .build();
            HTTPSCLIENT = HttpClients.custom().setConnectionManager(cm).setSSLSocketFactory(sslsf)
                    .setDefaultRequestConfig(config).build();
        } catch (final Exception e) {
            System.err.println(WBXCONorg.class.getCanonicalName()
                    + " UNABLE TO ESTABLISH HTTPSCLIENT FOR WAPI CALLS. All WAPI CALLS WILL FAIL!!!");
            e.printStackTrace();
            //System.exit(2);
        }
    Runtime.getRuntime().addShutdownHook(new Thread("WBXCONorg shutdownhook") {
        @Override
        public void run() {
            try {
                finalize();
            } catch (final Throwable e) {
                e.printStackTrace();
            }
        }
    });
    this.orgName = domain_name;
    this.wapiAUTHURL = wapiAUTHURL != null ? wapiAUTHURL : this.wapiAUTHURL;
    this.wapiUSER = wapiUSER + (!wapiUSER.endsWith("@" + domain_name) ? "@" + domain_name : "");
    this.wapiPASS = wapiPASS;

    final Document dom;
    try {
        System.out.println("===============  1");
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        System.out.println("===============  2");
        factory.setValidating(false);
        System.out.println("===============  3");
        factory.setCoalescing(true);
        System.out.println("===============  4");
        final DocumentBuilder db = factory.newDocumentBuilder();
        System.out.println("===============  5");
        final List<NameValuePair> params = new ArrayList<NameValuePair>();
        System.out.println("===============  6");
        params.add(new BasicNameValuePair("cmd", "get"));
        System.out.println("===============  7");
        params.add(new BasicNameValuePair("type", "org"));
        System.out.println("===============  8");
        params.add(new BasicNameValuePair("select", "org/orgID:/org/namespaceID:ext/WBX/PWSRule"));
        System.out.println("===============  9");
        params.add(new BasicNameValuePair("id", "current"));
        System.out.println("===============  10");
        System.out.println("===============  getDomainCredToken() :" + getDomainCredToken());
        params.add(new BasicNameValuePair("cred", getDomainCredToken()));
        System.out.println("===============  11");
        System.out.println("===============  params" + params.toString());
        System.out.println("===============Before wapiURL :" + this.wapiURL);
        final HttpPost httpPost = new HttpPost(this.wapiURL);
        System.out.println("=============== after wapiURL :" + this.wapiURL);

        httpPost.setEntity(new UrlEncodedFormEntity(params, org.apache.http.Consts.UTF_8));
        System.out.println("===============  12");
        final CloseableHttpResponse httpRes = HTTPSCLIENT.execute(httpPost, new BasicHttpContext());
        System.out.println("===============  13");

        if (httpRes == null) {
            System.out.println("===============  httpRes is NULL");
        }

        try {
            dom = db.parse(httpRes.getEntity().getContent());
            System.out.println("===============  14");
        } finally {
            httpRes.close();
        }
    } catch (final Exception e) {
        throw new WBXCONexception(e);
    }
    final NodeList result = dom.getElementsByTagName("result");
    if (result == null || result.item(0) == null
            || !result.item(0).getTextContent().equalsIgnoreCase("success"))
        throw new WBXCONexception(
                "ERROR::WBXCONorg:constructor(\"" + domain_name + "\")::" + documentGetErrorString(dom));

    this.orgID = dom.getElementsByTagName("orgID").item(0).getTextContent();
    this.namespaceID = dom.getElementsByTagName("namespaceID").item(0).getTextContent();
    this.passwordrule = new PWSRule(Integer.parseInt(documentGetTextContentByTagName(dom, "PWMinimumLength_9")),
            Integer.parseInt(documentGetTextContentByTagName(dom, "PWMinimumAlpha_9")),
            Integer.parseInt(documentGetTextContentByTagName(dom, "PWMinimumNumeric_9")),
            Integer.parseInt(documentGetTextContentByTagName(dom, "PWMinimumSpecial_9")),
            documentGetTextContentByTagName(dom, "PWRequireMixedCase_B").equalsIgnoreCase("true"));
    this.wapiUser = restapiAccountGet(this.wapiUSER);
}

From source file:org.duniter.core.client.service.HttpServiceImpl.java

protected PoolingHttpClientConnectionManager createConnectionManager(int maxTotalConnections,
        int maxConnectionsPerRoute, int timeout) {
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(maxTotalConnections);
    connectionManager.setDefaultMaxPerRoute(maxConnectionsPerRoute);
    connectionManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(timeout).build());
    return connectionManager;
}

From source file:org.ambraproject.rhino.config.RhinoConfiguration.java

@Bean
public CloseableHttpClient httpClient(RuntimeConfiguration runtimeConfiguration) {
    PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager();

    Integer maxTotal = runtimeConfiguration.getHttpConnectionPoolConfiguration().getMaxTotal();
    manager.setMaxTotal(maxTotal == null ? 400 : maxTotal);

    Integer defaultMaxPerRoute = runtimeConfiguration.getHttpConnectionPoolConfiguration()
            .getDefaultMaxPerRoute();/*  w  ww  . j a v  a 2  s.c  o m*/
    manager.setDefaultMaxPerRoute(defaultMaxPerRoute == null ? 20 : defaultMaxPerRoute);

    return HttpClientBuilder.create().setConnectionManager(manager).build();
}

From source file:org.alfresco.provision.ActiveMQService.java

private CloseableHttpClient buildClient() {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(activeMQHost, activeMQPort),
            new UsernamePasswordCredentials(username, password));
    PoolingHttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();
    poolingConnManager.setMaxTotal(200);

    CloseableHttpClient client = HttpClients.custom().setConnectionManager(poolingConnManager)
            .setDefaultCredentialsProvider(credsProvider).build();
    client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5);
    client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5);
    return client;
}

From source file:org.asqatasun.util.http.HttpRequestHandler.java

private CloseableHttpClient getHttpClient(String url) {
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout)
            .setConnectTimeout(connectionTimeout).build();
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    httpClientBuilder.setDefaultRequestConfig(requestConfig);
    httpClientBuilder.setConnectionManager(new PoolingHttpClientConnectionManager());
    httpClientBuilder.setUserAgent(ASQATASUN_USER_AGENT);
    if (isProxySet(url)) {
        LOGGER.debug(("Set proxy with " + proxyHost + " and " + proxyPort));
        httpClientBuilder.setProxy(new HttpHost(proxyHost, Integer.valueOf(proxyPort)));
        if (isProxyCredentialSet()) {
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new AuthScope(proxyHost, Integer.valueOf(proxyPort)),
                    new UsernamePasswordCredentials(proxyUser, proxyPassword));
            httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
            LOGGER.debug(("Set proxy credentials " + proxyHost + " and " + proxyPort + " and " + proxyUser
                    + " and " + proxyPassword));
        }// ww  w . j av  a2  s  . c o m
    }
    return httpClientBuilder.build();
}