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

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

Introduction

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

Prototype

public PoolStats getTotalStats() 

Source Link

Usage

From source file:interoperabilite.webservice.client.ClientEvictExpiredConnections.java

public static void main(String[] args) throws Exception {
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(100);//from  w w w.  j  ava 2 s .  c om
    CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).evictExpiredConnections()
            .evictIdleConnections(5L, TimeUnit.SECONDS).build();
    try {
        // create an array of URIs to perform GETs on
        String[] urisToGet = { "http://hc.apache.org/", "http://hc.apache.org/httpcomponents-core-ga/",
                "http://hc.apache.org/httpcomponents-client-ga/", };

        for (int i = 0; i < urisToGet.length; i++) {
            String requestURI = urisToGet[i];
            HttpGet request = new HttpGet(requestURI);

            System.out.println("Executing request " + requestURI);

            CloseableHttpResponse response = httpclient.execute(request);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                System.out.println(EntityUtils.toString(response.getEntity()));
            } finally {
                response.close();
            }
        }

        PoolStats stats1 = cm.getTotalStats();
        System.out.println("Connections kept alive: " + stats1.getAvailable());

        // Sleep 10 sec and let the connection evictor do its job
        Thread.sleep(10000);

        PoolStats stats2 = cm.getTotalStats();
        System.out.println("Connections kept alive: " + stats2.getAvailable());

    } finally {
        httpclient.close();
    }
}

From source file:com.joyent.manta.http.PoolStatsMBean.java

/**
 * Updates the backing {@link PoolStats} object so that JMX data can be
 * refreshed.//w  w  w .j  ava  2 s. c  om
 */
private void updatePoolStats() {
    if (connectionManagerRef.isEnqueued()) {
        return;
    }

    PoolingHttpClientConnectionManager manager = connectionManagerRef.get();

    if (manager != null) {
        this.stats = manager.getTotalStats();
    }

}

From source file:org.apache.solr.client.solrj.impl.HttpSolrClientConPoolTest.java

public void testPoolSize() throws SolrServerException, IOException {
    PoolingHttpClientConnectionManager pool = HttpClientUtil.createPoolingConnectionManager();
    final HttpSolrClient client1;
    final String fooUrl;
    {/* www  . j  ava  2 s  .  c o m*/
        fooUrl = jetty.getBaseUrl().toString() + "/" + "collection1";
        CloseableHttpClient httpClient = HttpClientUtil.createClient(new ModifiableSolrParams(), pool,
                false /* let client shutdown it*/);
        client1 = getHttpSolrClient(fooUrl, httpClient);
        client1.setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);
    }
    final String barUrl = yetty.getBaseUrl().toString() + "/" + "collection1";

    {
        client1.setBaseURL(fooUrl);
        client1.deleteByQuery("*:*");
        client1.setBaseURL(barUrl);
        client1.deleteByQuery("*:*");
    }

    List<String> urls = new ArrayList<>();
    for (int i = 0; i < 17; i++) {
        urls.add(fooUrl);
    }
    for (int i = 0; i < 31; i++) {
        urls.add(barUrl);
    }

    Collections.shuffle(urls, random());

    try {
        int i = 0;
        for (String url : urls) {
            if (!client1.getBaseURL().equals(url)) {
                client1.setBaseURL(url);
            }
            client1.add(new SolrInputDocument("id", "" + (i++)));
        }
        client1.setBaseURL(fooUrl);
        client1.commit();
        assertEquals(17, client1.query(new SolrQuery("*:*")).getResults().getNumFound());

        client1.setBaseURL(barUrl);
        client1.commit();
        assertEquals(31, client1.query(new SolrQuery("*:*")).getResults().getNumFound());

        PoolStats stats = pool.getTotalStats();
        assertEquals("oh " + stats, 2, stats.getAvailable());
    } finally {
        for (HttpSolrClient c : new HttpSolrClient[] { client1 }) {
            HttpClientUtil.close(c.getHttpClient());
            c.close();
        }
    }
}

From source file:org.apache.solr.client.solrj.impl.HttpSolrClientConPoolTest.java

public void testLBClient() throws IOException, SolrServerException {

    PoolingHttpClientConnectionManager pool = HttpClientUtil.createPoolingConnectionManager();
    final HttpSolrClient client1;
    int threadCount = atLeast(2);
    final ExecutorService threads = ExecutorUtil.newMDCAwareFixedThreadPool(threadCount,
            new SolrjNamedThreadFactory(getClass().getSimpleName() + "TestScheduler"));
    CloseableHttpClient httpClient = HttpClientUtil.createClient(new ModifiableSolrParams(), pool);
    try {/*from  w  w  w  .  j a va 2s. co  m*/
        final LBHttpSolrClient roundRobin = new LBHttpSolrClient.Builder().withBaseSolrUrl(fooUrl)
                .withBaseSolrUrl(barUrl).withHttpClient(httpClient).build();

        List<ConcurrentUpdateSolrClient> concurrentClients = Arrays.asList(
                new ConcurrentUpdateSolrClient.Builder(fooUrl).withHttpClient(httpClient)
                        .withThreadCount(threadCount).withQueueSize(10).withExecutorService(threads).build(),
                new ConcurrentUpdateSolrClient.Builder(barUrl).withHttpClient(httpClient)
                        .withThreadCount(threadCount).withQueueSize(10).withExecutorService(threads).build());

        for (int i = 0; i < 2; i++) {
            roundRobin.deleteByQuery("*:*");
        }

        for (int i = 0; i < 57; i++) {
            final SolrInputDocument doc = new SolrInputDocument("id", "" + i);
            if (random().nextBoolean()) {
                final ConcurrentUpdateSolrClient concurrentClient = concurrentClients
                        .get(random().nextInt(concurrentClients.size()));
                concurrentClient.add(doc); // here we are testing that CUSC and plain clients reuse pool 
                concurrentClient.blockUntilFinished();
            } else {
                if (random().nextBoolean()) {
                    roundRobin.add(doc);
                } else {
                    final UpdateRequest updateRequest = new UpdateRequest();
                    updateRequest.add(doc); // here we mimic CloudSolrClient impl
                    final List<String> urls = Arrays.asList(fooUrl, barUrl);
                    Collections.shuffle(urls, random());
                    LBHttpSolrClient.Req req = new LBHttpSolrClient.Req(updateRequest, urls);
                    roundRobin.request(req);
                }
            }
        }

        for (int i = 0; i < 2; i++) {
            roundRobin.commit();
        }
        int total = 0;
        for (int i = 0; i < 2; i++) {
            total += roundRobin.query(new SolrQuery("*:*")).getResults().getNumFound();
        }
        assertEquals(57, total);
        PoolStats stats = pool.getTotalStats();
        //System.out.println("\n"+stats);
        assertEquals("expected number of connections shouldn't exceed number of endpoints" + stats, 2,
                stats.getAvailable());
    } finally {
        threads.shutdown();
        HttpClientUtil.close(httpClient);
    }
}