@Override public ProtocolResponse getProtocolOutput(String url, Metadata md) throws Exception { LOG.debug("HTTP connection manager stats {}", CONNECTION_MANAGER.getTotalStats()); HttpGet httpget = new HttpGet(url); httpget.setConfig(requestConfig); if (md != null) { String ifModifiedSince = md.getFirstValue("cachedLastModified"); if (StringUtils.isNotBlank(ifModifiedSince)) { httpget.addHeader("If-Modified-Since", ifModifiedSince); } String ifNoneMatch = md.getFirstValue("cachedEtag"); if (StringUtils.isNotBlank(ifNoneMatch)) { httpget.addHeader("If-None-Match", ifNoneMatch); } } // no need to release the connection explicitly as this is handled // automatically. The client itself must be closed though. try (CloseableHttpClient httpclient = builder.build()) { return httpclient.execute(httpget, this); } }
@Override public void run() { try { if (this != null) logger.log(Level.SEVERE, "Thread Running: " + getName()); client.execute(get); if (connManager != null) { logger.log(Level.SEVERE, "Leased Connections " + connManager.getTotalStats().getLeased()); leasedConn = connManager.getTotalStats().getLeased(); logger.log( Level.SEVERE, "Available Connections " + connManager.getTotalStats().getAvailable()); } } catch (final ClientProtocolException ex) { } catch (final IOException ex) { } }
@Scheduled(fixedRate = 20000) public void httpConnectionRelease() { PoolingHttpClientConnectionManager connectionManager = (PoolingHttpClientConnectionManager) ContextLoader.getCurrentWebApplicationContext().getBean("httpPoolManager"); if (logger.isInfoEnabled()) { logger.info( "release start connect count:=" + connectionManager.getTotalStats().getAvailable()); } // Close expired connections connectionManager.closeExpiredConnections(); // Optionally, close connections // that have been idle longer than readTimeout*2 MILLISECONDS connectionManager.closeIdleConnections(60000, TimeUnit.MILLISECONDS); if (logger.isInfoEnabled()) { logger.info("release end connect count:=" + connectionManager.getTotalStats().getAvailable()); logger.info("release end connect count:=" + connectionManager.getTotalStats().getMax()); } }
/** * Forgive me these sins. This is the only way I can think of to determine the *actual* size of * the connection pool without wrapping large quantities of the underlying client. * * <p>This whole method is cheating and full of bad examples. Don't copy this. You've been warned. */ private int getPoolSize(JestHttpClient client) throws Exception { try { Field fieldHttpClient = client.getClass().getDeclaredField("httpClient"); fieldHttpClient.setAccessible(true); Object objInternalHttpClient = fieldHttpClient.get(client); Field fieldConnectionManager = objInternalHttpClient.getClass().getDeclaredField("connManager"); fieldConnectionManager.setAccessible(true); PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = (PoolingHttpClientConnectionManager) fieldConnectionManager.get(objInternalHttpClient); PoolStats poolStats = poolingHttpClientConnectionManager.getTotalStats(); return poolStats.getAvailable() + poolStats.getLeased(); } catch (Exception e) { e.printStackTrace(); } return -1; }
@Override public void run() { try { // Holds the stop request that stopped the process. Stop stopRequest; // Every 5 seconds. while ((stopRequest = stopSignal.poll(5, TimeUnit.SECONDS)) == null) { // Close expired connections cm.closeExpiredConnections(); // Optionally, close connections that have been idle too long. cm.closeIdleConnections(60, TimeUnit.SECONDS); // Look at pool stats. logger.debug("Stats: {}", cm.getTotalStats()); } // Acknowledge the stop request. stopRequest.stopped(); } catch (InterruptedException ex) { // terminate } }