Search in sources :

Example 16 with PoolStats

use of org.apache.hc.core5.pool.PoolStats in project httpcomponents-core by apache.

the class TestStrictConnPool method testMaxLimits.

@Test
public void testMaxLimits() throws Exception {
    final HttpConnection conn1 = Mockito.mock(HttpConnection.class);
    final HttpConnection conn2 = Mockito.mock(HttpConnection.class);
    final HttpConnection conn3 = Mockito.mock(HttpConnection.class);
    final StrictConnPool<String, HttpConnection> pool = new StrictConnPool<>(2, 10);
    pool.setMaxPerRoute("somehost", 2);
    pool.setMaxPerRoute("otherhost", 1);
    pool.setMaxTotal(3);
    final Future<PoolEntry<String, HttpConnection>> future1 = pool.lease("somehost", null);
    final Future<PoolEntry<String, HttpConnection>> future2 = pool.lease("somehost", null);
    final Future<PoolEntry<String, HttpConnection>> future3 = pool.lease("otherhost", null);
    final PoolEntry<String, HttpConnection> entry1 = future1.get();
    Assertions.assertNotNull(entry1);
    entry1.assignConnection(conn1);
    final PoolEntry<String, HttpConnection> entry2 = future2.get();
    Assertions.assertNotNull(entry2);
    entry2.assignConnection(conn2);
    final PoolEntry<String, HttpConnection> entry3 = future3.get();
    Assertions.assertNotNull(entry3);
    entry3.assignConnection(conn3);
    pool.release(entry1, true);
    pool.release(entry2, true);
    pool.release(entry3, true);
    final PoolStats totals = pool.getTotalStats();
    Assertions.assertEquals(3, totals.getAvailable());
    Assertions.assertEquals(0, totals.getLeased());
    Assertions.assertEquals(0, totals.getPending());
    final Future<PoolEntry<String, HttpConnection>> future4 = pool.lease("somehost", null);
    final Future<PoolEntry<String, HttpConnection>> future5 = pool.lease("somehost", null);
    final Future<PoolEntry<String, HttpConnection>> future6 = pool.lease("otherhost", null);
    final Future<PoolEntry<String, HttpConnection>> future7 = pool.lease("somehost", null);
    final Future<PoolEntry<String, HttpConnection>> future8 = pool.lease("somehost", null);
    final Future<PoolEntry<String, HttpConnection>> future9 = pool.lease("otherhost", null);
    Assertions.assertTrue(future4.isDone());
    final PoolEntry<String, HttpConnection> entry4 = future4.get();
    Assertions.assertNotNull(entry4);
    Assertions.assertSame(conn2, entry4.getConnection());
    Assertions.assertTrue(future5.isDone());
    final PoolEntry<String, HttpConnection> entry5 = future5.get();
    Assertions.assertNotNull(entry5);
    Assertions.assertSame(conn1, entry5.getConnection());
    Assertions.assertTrue(future6.isDone());
    final PoolEntry<String, HttpConnection> entry6 = future6.get();
    Assertions.assertNotNull(entry6);
    Assertions.assertSame(conn3, entry6.getConnection());
    Assertions.assertFalse(future7.isDone());
    Assertions.assertFalse(future8.isDone());
    Assertions.assertFalse(future9.isDone());
    pool.release(entry4, true);
    pool.release(entry5, false);
    pool.release(entry6, true);
    Assertions.assertTrue(future7.isDone());
    final PoolEntry<String, HttpConnection> entry7 = future7.get();
    Assertions.assertNotNull(entry7);
    Assertions.assertSame(conn2, entry7.getConnection());
    Assertions.assertTrue(future8.isDone());
    final PoolEntry<String, HttpConnection> entry8 = future8.get();
    Assertions.assertNotNull(entry8);
    Assertions.assertNull(entry8.getConnection());
    Assertions.assertTrue(future9.isDone());
    final PoolEntry<String, HttpConnection> entry9 = future9.get();
    Assertions.assertNotNull(entry9);
    Assertions.assertSame(conn3, entry9.getConnection());
}
Also used : HttpConnection(org.apache.hc.core5.http.HttpConnection) Test(org.junit.jupiter.api.Test)

Example 17 with PoolStats

use of org.apache.hc.core5.pool.PoolStats in project mercury by yellow013.

the class ClientEvictExpiredConnections method main.

public static void main(final String[] args) throws Exception {
    final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(100);
    try (final CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).evictExpiredConnections().evictIdleConnections(TimeValue.ofSeconds(5)).build()) {
        // create an array of URIs to perform GETs on
        final String[] urisToGet = { "http://hc.apache.org/", "http://hc.apache.org/httpcomponents-core-ga/", "http://hc.apache.org/httpcomponents-client-ga/" };
        for (final String requestURI : urisToGet) {
            final HttpGet request = new HttpGet(requestURI);
            System.out.println("Executing request " + request.getMethod() + " " + request.getRequestUri());
            try (final CloseableHttpResponse response = httpclient.execute(request)) {
                System.out.println("----------------------------------------");
                System.out.println(response.getCode() + " " + response.getReasonPhrase());
                EntityUtils.consume(response.getEntity());
            }
        }
        final 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);
        final PoolStats stats2 = cm.getTotalStats();
        System.out.println("Connections kept alive: " + stats2.getAvailable());
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) PoolingHttpClientConnectionManager(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager) PoolStats(org.apache.hc.core5.pool.PoolStats)

Aggregations

HttpConnection (org.apache.hc.core5.http.HttpConnection)14 Test (org.junit.jupiter.api.Test)12 PoolStats (org.apache.hc.core5.pool.PoolStats)5 ConnPoolStats (org.apache.hc.core5.pool.ConnPoolStats)4 HttpHost (org.apache.hc.core5.http.HttpHost)2 HttpRequest (org.apache.hc.core5.http.HttpRequest)2 HttpResponse (org.apache.hc.core5.http.HttpResponse)2 Http1StreamListener (org.apache.hc.core5.http.impl.Http1StreamListener)2 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)1 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)1 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)1 PoolingHttpClientConnectionManager (org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager)1 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)1 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)1 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)1 ExceptionListener (org.apache.hc.core5.http.ExceptionListener)1