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

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

Introduction

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

Prototype

public void setMaxTotal(final int max) 

Source Link

Usage

From source file:org.apache.maven.wagon.providers.http.AbstractHttpClientWagon.java

private static PoolingHttpClientConnectionManager createConnManager() {

    String sslProtocolsStr = System.getProperty("https.protocols");
    String cipherSuitesStr = System.getProperty("https.cipherSuites");
    String[] sslProtocols = sslProtocolsStr != null ? sslProtocolsStr.split(" *, *") : null;
    String[] cipherSuites = cipherSuitesStr != null ? cipherSuitesStr.split(" *, *") : null;

    SSLConnectionSocketFactory sslConnectionSocketFactory;
    if (SSL_INSECURE) {
        try {/*w  w w  . jav  a 2  s .  co m*/
            SSLContext sslContext = new SSLContextBuilder().useSSL()
                    .loadTrustMaterial(null, new RelaxedTrustStrategy(IGNORE_SSL_VALIDITY_DATES)).build();
            sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, sslProtocols, cipherSuites,
                    SSL_ALLOW_ALL ? SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER
                            : SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        } catch (Exception ex) {
            throw new SSLInitializationException(ex.getMessage(), ex);
        }
    } else {
        sslConnectionSocketFactory = new SSLConnectionSocketFactory(
                HttpsURLConnection.getDefaultSSLSocketFactory(), sslProtocols, cipherSuites,
                SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    }

    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.INSTANCE)
            .register("https", sslConnectionSocketFactory).build();

    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry);
    if (PERSISTENT_POOL) {
        connManager.setDefaultMaxPerRoute(MAX_CONN_PER_ROUTE);
        connManager.setMaxTotal(MAX_CONN_TOTAL);
    } else {
        connManager.setMaxTotal(1);
    }
    return connManager;
}

From source file:co.cask.cdap.client.rest.RestStreamClient.java

@Override
public StreamWriter createWriter(String stream) throws IOException {
    //get the Stream TTL for check does the requested Stream exist
    long ttl = getTTL(stream);
    LOG.debug("The Stream with id {} exists. Got the current Stream TTL value {} successfully.", stream, ttl);
    PoolingHttpClientConnectionManager connectionManager = createConnectionManager();
    connectionManager.setMaxTotal(writerPoolSize);
    connectionManager.setDefaultMaxPerRoute(writerPoolSize);
    RestClient writerRestClient = new RestClient(config, connectionManager);
    return new RestStreamWriter(writerRestClient, writerPoolSize, stream);
}

From source file:org.mule.module.http.functional.listener.HttpListenerWorkerThreadingProfileTestCase.java

@Before
public void setup() {
    // Need to configure the maximum number of connections of HttpClient, because the default is less than
    // the default number of workers in the HTTP listener.
    PoolingHttpClientConnectionManager mgr = new PoolingHttpClientConnectionManager();
    mgr.setDefaultMaxPerRoute(HTTP_CLIENT_MAX_CONNECTIONS);
    mgr.setMaxTotal(HTTP_CLIENT_MAX_CONNECTIONS);
    httpClientExecutor = Executor.newInstance(HttpClientBuilder.create().setConnectionManager(mgr).build());
}

From source file:org.auraframework.integration.test.http.AuraResourceServletLoggingHttpTest.java

/**
 * test add for W-2792895/*w  ww .  java 2  s  .c  om*/
   also since I ask cache to log something when hit miss, this kind of verify W-2105858 as well
 */
@Test
public void testConcurrentGetRequests() throws Exception {
    // I tried to use obtainGetMethod(url) then perform(HttpGet) , but
    // our default httpClient use BasicClientConnectionManager, which doesn't work well with MultiThread
    // let's use PoolingHttpClientConnectionManager instead.
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    // Increase max total connection to 200 -- just some big number
    cm.setMaxTotal(200);
    // Increase default max connection per route to 20 -- again, just some big numer
    cm.setDefaultMaxPerRoute(20);
    CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();

    String modeAndContext = getSimpleContext(Format.JS, false);
    String url = "/l/" + AuraTextUtil.urlencode(modeAndContext) + "/app.js";

    int numOfRequest = 5;
    List<Request> requests = new ArrayList<>();
    for (int i = 1; i <= numOfRequest; i++) {
        requests.add(new Request(httpClient, url));
    }

    ExecutorService excutor = Executors.newFixedThreadPool(numOfRequest);
    List<Future<Integer>> responses = new ArrayList<>();
    for (Request request : requests) {
        responses.add(excutor.submit(request));
    }
    for (Future<Integer> response : responses) {
        response.get();
    }

    int counter = 0;
    String message;
    List<LoggingEvent> logs = appender.getLog();
    for (LoggingEvent le : logs) {
        message = le.getMessage().toString();
        if (message.contains("StringsCache")) {
            counter++;
            assertTrue("get unexpected logging message for cache miss:" + message,
                    message.contains("cache miss for key: JS:DEV:"));
        }
    }
    //run this test right after server is up, we get one miss. second time, what we looking for is cached already, no miss.
    assertTrue("we should only have no more than one cache miss, instead we have " + counter, counter <= 1);
}

From source file:ca.uhn.fhir.rest.client.apache.ApacheRestfulClientFactory.java

public synchronized HttpClient getNativeHttpClient() {
    if (myHttpClient == null) {

        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000,
                TimeUnit.MILLISECONDS);
        connectionManager.setMaxTotal(getPoolMaxTotal());
        connectionManager.setDefaultMaxPerRoute(getPoolMaxPerRoute());

        // @formatter:off
        RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(getSocketTimeout())
                .setConnectTimeout(getConnectTimeout())
                .setConnectionRequestTimeout(getConnectionRequestTimeout()).setStaleConnectionCheckEnabled(true)
                .setProxy(myProxy).build();

        HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connectionManager)
                .setDefaultRequestConfig(defaultRequestConfig).disableCookieManagement();

        if (myProxy != null && StringUtils.isNotBlank(getProxyUsername())
                && StringUtils.isNotBlank(getProxyPassword())) {
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new AuthScope(myProxy.getHostName(), myProxy.getPort()),
                    new UsernamePasswordCredentials(getProxyUsername(), getProxyPassword()));
            builder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
            builder.setDefaultCredentialsProvider(credsProvider);
        }//from w  w  w . ja  v a 2 s .c  om

        myHttpClient = builder.build();
        // @formatter:on

    }

    return myHttpClient;
}

From source file:com.ericsson.gerrit.plugins.highavailability.forwarder.rest.HttpClientProvider.java

private HttpClientConnectionManager customConnectionManager() {
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("https", sslSocketFactory).register("http", PlainConnectionSocketFactory.INSTANCE)
            .build();//from w  w  w.  j a  v a  2s .c  o  m
    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(
            socketFactoryRegistry);
    connManager.setDefaultMaxPerRoute(CONNECTIONS_PER_ROUTE);
    connManager.setMaxTotal(MAX_CONNECTIONS);
    connManager.setValidateAfterInactivity(MAX_CONNECTION_INACTIVITY);
    return connManager;
}

From source file:ok.MyService2.java

@Override
protected Task<BlockingQueue> createTask() {
    final Task<BlockingQueue> task;
    task = new Task<BlockingQueue>() {

        @Override// w ww. j a  v a  2  s . c o m
        protected BlockingQueue call() throws Exception {
            BlockingQueue result = new LinkedBlockingQueue<String>();

            PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
            cm.setMaxTotal(100);

            CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build();
            try {
                ExecutorService executor = Executors.newFixedThreadPool(sites.size());
                List<Future<String>> results = new ArrayList<Future<String>>();
                for (int i = 0; i < sites.size(); i++) {
                    HttpGet httpget = new HttpGet(sites.get(i));
                    Callable worker = new MyCallable(httpclient, httpget);
                    Future<String> res = executor.submit(worker);
                    results.add(res);
                    // String url = hostList[i];
                    //   Runnable worker = new MyRunnable(url);
                    //   executor.execute(worker);
                    //   executor.submit(null);

                }
                executor.shutdown();
                // Wait until all threads are finish
                //                   while (!executor.isTerminated()) {
                //
                //                   }
                for (Future<String> element : results) {
                    result.add(element.get());
                }
                System.out.println("\nFinished all threads");

            } finally {
                httpclient.close();
            }
            return result;
        }

    };
    return task;
}

From source file:ch.cyberduck.core.http.HttpConnectionPoolBuilder.java

protected PoolingHttpClientConnectionManager pool(final Registry<ConnectionSocketFactory> registry) {
    if (log.isDebugEnabled()) {
        log.debug(String.format("Setup connection pool with registry %s", registry));
    }/*from  www  .  j a v  a  2  s .  c o m*/
    final PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(registry);
    manager.setMaxTotal(preferences.getInteger("http.connections.total"));
    manager.setDefaultMaxPerRoute(preferences.getInteger("http.connections.route"));
    manager.setValidateAfterInactivity(5000);
    return manager;
}

From source file:jp.co.ctc_g.jse.core.rest.springmvc.client.ProxyClientHttpRequestFactory.java

/**
 * ???????HttpClient????????//  w  w w .java2s  . com
 * {@inheritDoc}
 */
@Override
public void afterPropertiesSet() throws Exception {

    Assert.notNull(proxyHost, "(proxyHost)???");
    Assert.notNull(proxyPort, "??(proxyPort)???");

    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(maxTotal);
    connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);

    HttpClientBuilder builder = HttpClients.custom();
    builder.setConnectionManager(connectionManager);
    if (authentication) {
        Assert.notNull(username,
                "??true???????(username)???");
        Assert.notNull(password,
                "??true?????(password)???");
        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(
                new HttpHost(proxyHost, Integer.parseInt(proxyPort)));
        builder.setRoutePlanner(routePlanner);
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(proxyHost, Integer.parseInt(proxyPort)),
                new UsernamePasswordCredentials(username, password));
        builder.setDefaultCredentialsProvider(credsProvider);
    }
    builder.setDefaultRequestConfig(RequestConfig.custom().setSocketTimeout(readTimeout).build());
    CloseableHttpClient client = builder.build();
    setHttpClient(client);
}