/** * Removes the given connection manager from this reaper, * and shutting down the reaper if there is zero connection manager left. * * @return true if the connection manager has been successfully removed; * false otherwise. */ public static boolean removeConnectionManager(HttpClientConnectionManager connectionManager) { boolean wasRemoved = connectionManagers.remove(connectionManager) != null; if (connectionManagers.isEmpty()) { shutdown(); } return wasRemoved; }
/** * Registers the given connection manager with this reaper. * * @return true if the connection manager has been successfully registered; false otherwise. * @deprecated By {@link #registerConnectionManager(HttpClientConnectionManager, long)}. */ @Deprecated public static boolean registerConnectionManager(HttpClientConnectionManager connectionManager) { return registerConnectionManager(connectionManager, DEFAULT_MAX_IDLE_MILLIS); }
/** * Registers the given connection manager with this reaper; * * @param connectionManager Connection manager to register * @param maxIdleInMs Max idle connection timeout in milliseconds for this connection manager. * @return true if the connection manager has been successfully registered; false otherwise. */ public static boolean registerConnectionManager(HttpClientConnectionManager connectionManager, long maxIdleInMs) { if (instance == null) { synchronized (IdleConnectionReaper.class) { if (instance == null) { instance = new IdleConnectionReaper(); instance.start(); } } } return connectionManagers.put(connectionManager, maxIdleInMs) == null; }
/** * Shuts down the thread, allowing the class and instance to be collected. * <p/> * Since this is a daemon thread, its running will not prevent JVM shutdown. * It will, however, prevent this class from being unloaded or garbage * collected, in the context of a long-running application, until it is * interrupted. This method will stop the thread's execution and clear its * state. Any use of a service client will cause the thread to be restarted. * * @return true if an actual shutdown has been made; false otherwise. */ public static synchronized boolean shutdown() { if (instance != null) { instance.markShuttingDown(); instance.interrupt(); connectionManagers.clear(); instance = null; return true; } return false; }
@Test public void autoShutdown() throws Exception { // final int threadCount = getThreadCount(); assertEquals(0, IdleConnectionReaper.size()); for (int i = 0; i < 3; i++) { ClientConnectionManager m = new TestClientConnectionManager(); ClientConnectionManager m2 = new TestClientConnectionManager(); assertTrue(IdleConnectionReaper .registerConnectionManager(m)); assertEquals(1, IdleConnectionReaper.size()); // Thread.sleep(100); // assertTrue(getThreadCount() > threadCount); assertTrue(IdleConnectionReaper .registerConnectionManager(m2)); assertEquals(2, IdleConnectionReaper.size()); assertTrue(IdleConnectionReaper.removeConnectionManager(m)); assertEquals(1, IdleConnectionReaper.size()); // Thread.sleep(100); // assertTrue(getThreadCount() > threadCount); assertTrue(IdleConnectionReaper.removeConnectionManager(m2)); assertEquals(0, IdleConnectionReaper.size()); // Thread.sleep(100); // assertEquals(threadCount, getThreadCount()); assertFalse(IdleConnectionReaper.shutdown()); } // assertEquals(threadCount, getThreadCount()); }
@Test public void forceShutdown() throws Exception { // final int threadCount = getThreadCount(); assertEquals(0, IdleConnectionReaper.size()); for (int i = 0; i < 3; i++) { assertTrue(IdleConnectionReaper .registerConnectionManager(new TestClientConnectionManager())); assertEquals(1, IdleConnectionReaper.size()); // Thread.sleep(100); // assertTrue(getThreadCount() > threadCount); assertTrue(IdleConnectionReaper.shutdown()); assertEquals(0, IdleConnectionReaper.size()); // Thread.sleep(100); // assertTrue(getThreadCount() == threadCount); assertFalse(IdleConnectionReaper.shutdown()); } // assertEquals(threadCount, getThreadCount()); }
/** * Private constructor - singleton pattern. */ private IdleConnectionReaper() { super("java-sdk-http-connection-reaper"); setDaemon(true); }
/** * Shuts down this HTTP client object, releasing any resources that might be held open. This is * an optional method, and callers are not expected to call it, but can if they want to * explicitly release any open resources. Once a client has been shutdown, it cannot be used to * make more requests. */ public void shutdown() { clientExecutionTimer.shutdown(); httpRequestTimer.shutdown(); IdleConnectionReaper.removeConnectionManager(httpClient.getHttpClientConnectionManager()); httpClient.getHttpClientConnectionManager().shutdown(); }
/** * Shuts down the thread, allowing the class and instance to be collected. * <p/> * Since this is a daemon thread, its running will not prevent JVM shutdown. * It will, however, prevent this class from being unloaded or garbage * collected, in the context of a long-running application, until it is * interrupted. This method will stop the thread's execution and clear its * state. Any use of a service client will cause the thread to be restarted. * * @return true if an actual shutdown has been made; false otherwise. */ public static synchronized boolean shutdown() { if (instance != null) { instance.markShuttingDown(); instance.interrupt(); connectionManagers.clear(); instance = null; return true; } return false; }
/** * Private constructor - singleton pattern. */ private IdleConnectionReaper() { super("java-sdk-http-connection-reaper"); setDaemon(true); }
/** * Shuts down this HTTP client object, releasing any resources that might be held open. This is * an optional method, and callers are not expected to call it, but can if they want to * explicitly release any open resources. Once a client has been shutdown, it cannot be used to * make more requests. */ public void shutdown() { clientExecutionTimer.shutdown(); httpRequestTimer.shutdown(); IdleConnectionReaper.removeConnectionManager(httpClient.getHttpClientConnectionManager()); httpClient.getHttpClientConnectionManager().shutdown(); }
/** * Registers the given connection manager with this reaper; * @param connectionManager the connection manager. * @return true if the connection manager has been successfully registered; * false otherwise. */ public static synchronized boolean registerConnectionManager( ClientConnectionManager connectionManager) { if (instance == null) { instance = new IdleConnectionReaper(); instance.start(); } return CONNECTION_MANAGERS.add(connectionManager); }
/** * Shuts down the thread, allowing the class and instance to be collected. * <p> * Since this is a daemon thread, its running will not prevent JVM shutdown. * It will, however, prevent this class from being unloaded or garbage * collected, in the context of a long-running application, until it is * interrupted. This method will stop the thread's execution and clear its * state. Any use of a service client will cause the thread to be restarted. * * @return true if an actual shutdown has been made; false otherwise. */ public static synchronized boolean shutdown() { if (instance != null) { instance.markShuttingDown(); instance.interrupt(); CONNECTION_MANAGERS.clear(); instance = null; return true; } return false; }
/** * Removes the given connection manager from this reaper, and shutting down * the reaper if there is zero connection manager left. * @param connectionManager the connection manager. * @return true if the connection manager has been successfully removed; * false otherwise. */ public static synchronized boolean removeConnectionManager( ClientConnectionManager connectionManager) { boolean b = CONNECTION_MANAGERS.remove(connectionManager); if (CONNECTION_MANAGERS.isEmpty()) shutdown(); return b; }
IdleConnectionReaper.registerConnectionManager(cm, settings.getMaxIdleConnectionTime());
/** Private constructor - singleton pattern. */ private IdleConnectionReaper() { super("java-sdk-http-connection-reaper"); setDaemon(true); }
/** * Shuts down this HTTP client object, releasing any resources that might be held open. This is * an optional method, and callers are not expected to call it, but can if they want to * explicitly release any open resources. Once a client has been shutdown, it cannot be used to * make more requests. */ public void shutdown() { clientExecutionTimer.shutdown(); httpRequestTimer.shutdown(); IdleConnectionReaper.removeConnectionManager(httpClient.getHttpClientConnectionManager()); httpClient.getHttpClientConnectionManager().shutdown(); }
/** * Registers the given connection manager with this reaper; * * @param connectionManager Connection manager to register * @param maxIdleInMs Max idle connection timeout in milliseconds for this connection manager. * @return true if the connection manager has been successfully registered; false otherwise. */ public static boolean registerConnectionManager(HttpClientConnectionManager connectionManager, long maxIdleInMs) { if (instance == null) { synchronized (IdleConnectionReaper.class) { if (instance == null) { instance = new IdleConnectionReaper(); instance.start(); } } } return connectionManagers.put(connectionManager, maxIdleInMs) == null; }
/** * Shuts down the thread, allowing the class and instance to be collected. * <p> * Since this is a daemon thread, its running will not prevent JVM shutdown. * It will, however, prevent this class from being unloaded or garbage * collected, in the context of a long-running application, until it is * interrupted. This method will stop the thread's execution and clear its * state. Any use of a service client will cause the thread to be restarted. * * @return true if an actual shutdown has been made; false otherwise. */ public static synchronized boolean shutdown() { if (instance != null) { instance.markShuttingDown(); instance.interrupt(); CONNECTION_MANAGERS.clear(); instance = null; return true; } return false; }
/** * Removes the given connection manager from this reaper, * and shutting down the reaper if there is zero connection manager left. * * @return true if the connection manager has been successfully removed; * false otherwise. */ public static boolean removeConnectionManager(HttpClientConnectionManager connectionManager) { boolean wasRemoved = connectionManagers.remove(connectionManager) != null; if (connectionManagers.isEmpty()) { shutdown(); } return wasRemoved; }