private String formatStats(final HttpRoute route) { final StringBuilder buf = new StringBuilder(); final PoolStats totals = this.pool.getTotalStats(); final PoolStats stats = this.pool.getStats(route); buf.append("[total kept alive: ").append(totals.getAvailable()).append("; "); buf.append("route allocated: ").append(stats.getLeased() + stats.getAvailable()); buf.append(" of ").append(stats.getMax()).append("; "); buf.append("total allocated: ").append(totals.getLeased() + totals.getAvailable()); buf.append(" of ").append(totals.getMax()).append("]"); return buf.toString(); }
@Override public Map<String, String> getStatistics() { int i = 0; final Map<String, String> data = new LinkedHashMap<String, String>(); data.put(KEYS[i++], String.valueOf(requestsExecuted.get())); data.put(KEYS[i++], humanReadableBytes(bytesSent.get(), false)); data.put(KEYS[i++], humanReadableBytes(bytesReceived.get(), false)); data.put(KEYS[i++], humanReadableBytes(bytesFromCache.get(), false)); final PoolStats cmStats = connectionManager.getTotalStats(); data.put(KEYS[i++], connectionManager.getClass().getSimpleName()); data.put(KEYS[i++], String.valueOf(cmStats.getMax())); data.put(KEYS[i++], String.valueOf(cmStats.getAvailable())); data.put(KEYS[i++], String.valueOf(cmStats.getLeased())); data.put(KEYS[i++], String.valueOf(cmStats.getPending())); return data; }
private String formatStats(final HttpRoute route) { StringBuilder buf = new StringBuilder(); PoolStats totals = this.pool.getTotalStats(); PoolStats stats = this.pool.getStats(route); buf.append("[total kept alive: ").append(totals.getAvailable()).append("; "); buf.append("route allocated: ").append(stats.getLeased() + stats.getAvailable()); buf.append(" of ").append(stats.getMax()).append("; "); buf.append("total allocated: ").append(totals.getLeased() + totals.getAvailable()); buf.append(" of ").append(totals.getMax()).append("]"); return buf.toString(); }
public void run() { PoolStats stats = pool.getTotalStats(); Metrics.INSTANCE.count("restclient.httpc.pool.available", stats.getAvailable(), "rest_pool:" + poolName, "type:" + getType()); Metrics.INSTANCE.count("restclient.httpc.pool.leased", stats.getLeased(), "rest_pool:" + poolName, "type:" + getType()); Metrics.INSTANCE.count("restclient.httpc.pool.pending", stats.getPending(), "rest_pool:" + poolName, "type:" + getType()); }
/** * Captures the connection pool metrics. */ private void captureConnectionPoolMetrics() { if (awsRequestMetrics.isEnabled() && httpClient.getHttpClientConnectionManager() instanceof ConnPoolControl<?>) { final PoolStats stats = ((ConnPoolControl<?>) httpClient .getHttpClientConnectionManager()).getTotalStats(); awsRequestMetrics .withCounter(HttpClientPoolAvailableCount, stats.getAvailable()) .withCounter(HttpClientPoolLeasedCount, stats.getLeased()) .withCounter(HttpClientPoolPendingCount, stats.getPending()); } }
@Test public void testReleaseOnEntityConsumeContent() throws Exception { this.connManager.setDefaultMaxPerRoute(1); this.connManager.setMaxTotal(1); // Zero connections in the pool PoolStats stats = this.connManager.getTotalStats(); Assert.assertEquals(0, stats.getAvailable()); final HttpHost target = start(); // Get some random data final HttpGet httpget = new HttpGet("/random/20000"); final HttpResponse response = this.httpclient.execute(target, httpget); ConnectionRequest connreq = this.connManager.requestConnection(new HttpRoute(target), null); try { connreq.get(250, TimeUnit.MILLISECONDS); Assert.fail("ConnectionPoolTimeoutException should have been thrown"); } catch (final ConnectionPoolTimeoutException expected) { } final HttpEntity e = response.getEntity(); Assert.assertNotNull(e); EntityUtils.consume(e); // Expect one connection in the pool stats = this.connManager.getTotalStats(); Assert.assertEquals(1, stats.getAvailable()); // Make sure one connection is available connreq = this.connManager.requestConnection(new HttpRoute(target), null); final HttpClientConnection conn = connreq.get(250, TimeUnit.MILLISECONDS); this.connManager.releaseConnection(conn, null, -1, null); }
@Test public void testReleaseOnEntityWriteTo() throws Exception { this.connManager.setDefaultMaxPerRoute(1); this.connManager.setMaxTotal(1); // Zero connections in the pool PoolStats stats = this.connManager.getTotalStats(); Assert.assertEquals(0, stats.getAvailable()); final HttpHost target = start(); // Get some random data final HttpGet httpget = new HttpGet("/random/20000"); final HttpResponse response = this.httpclient.execute(target, httpget); ConnectionRequest connreq = this.connManager.requestConnection(new HttpRoute(target), null); try { connreq.get(250, TimeUnit.MILLISECONDS); Assert.fail("ConnectionPoolTimeoutException should have been thrown"); } catch (final ConnectionPoolTimeoutException expected) { } final HttpEntity e = response.getEntity(); Assert.assertNotNull(e); final ByteArrayOutputStream outsteam = new ByteArrayOutputStream(); e.writeTo(outsteam); // Expect one connection in the pool stats = this.connManager.getTotalStats(); Assert.assertEquals(1, stats.getAvailable()); // Make sure one connection is available connreq = this.connManager.requestConnection(new HttpRoute(target), null); final HttpClientConnection conn = connreq.get(250, TimeUnit.MILLISECONDS); this.connManager.releaseConnection(conn, null, -1, null); }
@Test public void testReleaseOnAbort() throws Exception { this.connManager.setDefaultMaxPerRoute(1); this.connManager.setMaxTotal(1); // Zero connections in the pool final PoolStats stats = this.connManager.getTotalStats(); Assert.assertEquals(0, stats.getAvailable()); final HttpHost target = start(); // Get some random data final HttpGet httpget = new HttpGet("/random/20000"); final HttpResponse response = this.httpclient.execute(target, httpget); ConnectionRequest connreq = this.connManager.requestConnection(new HttpRoute(target), null); try { connreq.get(250, TimeUnit.MILLISECONDS); Assert.fail("ConnectionPoolTimeoutException should have been thrown"); } catch (final ConnectionPoolTimeoutException expected) { } final HttpEntity e = response.getEntity(); Assert.assertNotNull(e); httpget.abort(); // Expect zero connections in the pool Assert.assertEquals(0, this.connManager.getTotalStats().getAvailable()); // Make sure one connection is available connreq = this.connManager.requestConnection(new HttpRoute(target), null); final HttpClientConnection conn = connreq.get(250, TimeUnit.MILLISECONDS); this.connManager.releaseConnection(conn, null, -1, null); }
@Override public HttpClientPoolStats getPoolStats() { PoolStats totalStats = connPoolControl.getTotalStats(); return new HttpClientPoolStats(totalStats.getLeased(), totalStats.getPending(), totalStats.getAvailable(), totalStats.getMax()); }
public void release(final BasicNIOPoolEntry entry, boolean reusable) { System.out.println("[proxy->origin] connection released " + entry.getConnection()); ConsoleFactory.printToConsole("[proxy->origin] connection released " + entry.getConnection(),true); super.release(entry, reusable); StringBuilder buf = new StringBuilder(); PoolStats totals = getTotalStats(); buf.append("[total kept alive: ").append(totals.getAvailable()).append("; "); buf.append("total allocated: ").append(totals.getLeased() + totals.getAvailable()); buf.append(" of ").append(totals.getMax()).append("]"); System.out.println("[proxy->origin] " + buf.toString()); ConsoleFactory.printToConsole("[proxy->origin] " + buf.toString(),true); }
public static Map<String, Integer> getConnectionPoolStats() { Map<String, Integer> stats = new HashMap<>(); PoolStats poolStats = pool.getTotalStats(); stats.put("availableConnections", poolStats.getAvailable()); stats.put("maxConnections", poolStats.getMax()); stats.put("leasedConnections", poolStats.getLeased()); stats.put("pendingConnections", poolStats.getPending()); stats.put("defaultMaxPerRoute", pool.getDefaultMaxPerRoute()); return stats; }
public PoolStats getPoolStats(){ return connectionManager.getTotalStats(); }
public PoolStats getTotalStats() { return this.pool.getTotalStats(); }
public PoolStats getStats(final HttpRoute route) { return this.pool.getStats(route); }
@Override public PoolStats getTotalStats() { return this.pool.getTotalStats(); }
@Override public PoolStats getStats(final HttpRoute route) { return this.pool.getStats(route); }
@Override public PoolStats getTotalStats() { return new PoolStats(-1, -1, -1, this.totalMax); }
@Override public PoolStats getStats(final HttpRoute route) { return new PoolStats(-1, -1, -1, getMaxPerRoute(route)); }
/** * Get the statistics */ public String getStats() { PoolingClientConnectionManager cm = (PoolingClientConnectionManager) this.client.getConnectionManager(); PoolStats stats = cm.getTotalStats(); return "Connections: " + stats.toString() + " AvailableRequests: " + processQueue.availablePermits(); }
public PoolStats getPoolStats() { return getConnectionManager().getTotalStats(); }
/** * @return the connection pool stats. */ protected PoolStats getConnectionStats() { return connectionManager.getTotalStats(); }
public static void main(String[] args) throws Exception { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(100); 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()); EntityUtils.consume(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(); } }
public static void main(String[] args) throws Exception { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(100); 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(); } }