Authmatically shut down file system based cache storage instantiated by CachingHttpClientBuilder
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1423960 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ba065fca60
commit
b6d7989aae
|
@ -88,6 +88,13 @@ public class CachingHttpClientBuilder extends HttpClientBuilder {
|
|||
}
|
||||
HttpCacheStorage storage = this.storage;
|
||||
if (storage == null) {
|
||||
if (this.cacheDir == null) {
|
||||
storage = new BasicHttpCacheStorage(cacheConfig);
|
||||
} else {
|
||||
ManagedHttpCacheStorage managedStorage = new ManagedHttpCacheStorage(cacheConfig);
|
||||
addCloseable(managedStorage);
|
||||
storage = managedStorage;
|
||||
}
|
||||
storage = new BasicHttpCacheStorage(cacheConfig);
|
||||
}
|
||||
return new CachingExec(mainExec,
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
*/
|
||||
package org.apache.http.impl.client.cache;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.lang.ref.PhantomReference;
|
||||
import java.lang.ref.ReferenceQueue;
|
||||
|
@ -54,7 +55,7 @@ import org.apache.http.util.Args;
|
|||
* @since 4.1
|
||||
*/
|
||||
@ThreadSafe
|
||||
public class ManagedHttpCacheStorage implements HttpCacheStorage {
|
||||
public class ManagedHttpCacheStorage implements HttpCacheStorage, Closeable {
|
||||
|
||||
private final CacheMap entries;
|
||||
private final ReferenceQueue<HttpCacheEntry> morque;
|
||||
|
@ -157,4 +158,8 @@ public class ManagedHttpCacheStorage implements HttpCacheStorage {
|
|||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
shutdown();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -27,10 +27,13 @@
|
|||
|
||||
package org.apache.http.impl.client;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.net.ProxySelector;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.http.ConnectionReuseStrategy;
|
||||
|
@ -181,6 +184,8 @@ public class HttpClientBuilder {
|
|||
private int maxConnTotal = 0;
|
||||
private int maxConnPerRoute = 0;
|
||||
|
||||
private List<Closeable> closeables;
|
||||
|
||||
public static HttpClientBuilder create() {
|
||||
return new HttpClientBuilder();
|
||||
}
|
||||
|
@ -431,6 +436,17 @@ public class HttpClientBuilder {
|
|||
return protocolExec;
|
||||
}
|
||||
|
||||
protected void addCloseable(final Closeable closeable) {
|
||||
if (closeable == null) {
|
||||
return;
|
||||
}
|
||||
if (closeables == null) {
|
||||
closeables = new ArrayList<Closeable>();
|
||||
}
|
||||
closeables.add(closeable);
|
||||
}
|
||||
|
||||
|
||||
public CloseableHttpClient build() {
|
||||
// Create main request executor
|
||||
HttpRequestExecutor requestExec = this.requestExec;
|
||||
|
@ -679,7 +695,8 @@ public class HttpClientBuilder {
|
|||
authSchemeRegistry,
|
||||
defaultCookieStore,
|
||||
defaultCredentialsProvider,
|
||||
defaultRequestConfig != null ? defaultRequestConfig : RequestConfig.DEFAULT);
|
||||
defaultRequestConfig != null ? defaultRequestConfig : RequestConfig.DEFAULT,
|
||||
closeables != null ? new ArrayList<Closeable>(closeables) : null);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -27,9 +27,13 @@
|
|||
|
||||
package org.apache.http.impl.client;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.http.HttpException;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpRequest;
|
||||
|
@ -72,6 +76,8 @@ import org.apache.http.util.Asserts;
|
|||
@SuppressWarnings("deprecation")
|
||||
class InternalHttpClient extends CloseableHttpClient {
|
||||
|
||||
private final Log log = LogFactory.getLog(getClass());
|
||||
|
||||
private final ClientExecChain execChain;
|
||||
private final HttpClientConnectionManager connManager;
|
||||
private final HttpRoutePlanner routePlanner;
|
||||
|
@ -80,6 +86,7 @@ class InternalHttpClient extends CloseableHttpClient {
|
|||
private final CookieStore cookieStore;
|
||||
private final CredentialsProvider credentialsProvider;
|
||||
private final RequestConfig defaultConfig;
|
||||
private final List<Closeable> closeables;
|
||||
private final HttpParams params;
|
||||
|
||||
public InternalHttpClient(
|
||||
|
@ -90,7 +97,8 @@ class InternalHttpClient extends CloseableHttpClient {
|
|||
final Lookup<AuthSchemeProvider> authSchemeRegistry,
|
||||
final CookieStore cookieStore,
|
||||
final CredentialsProvider credentialsProvider,
|
||||
final RequestConfig defaultConfig) {
|
||||
final RequestConfig defaultConfig,
|
||||
final List<Closeable> closeables) {
|
||||
super();
|
||||
Args.notNull(execChain, "HTTP client exec chain");
|
||||
Args.notNull(connManager, "HTTP connection manager");
|
||||
|
@ -103,6 +111,7 @@ class InternalHttpClient extends CloseableHttpClient {
|
|||
this.cookieStore = cookieStore;
|
||||
this.credentialsProvider = credentialsProvider;
|
||||
this.defaultConfig = defaultConfig;
|
||||
this.closeables = closeables;
|
||||
this.params = new BasicHttpParams();
|
||||
}
|
||||
|
||||
|
@ -179,6 +188,15 @@ class InternalHttpClient extends CloseableHttpClient {
|
|||
|
||||
public void close() {
|
||||
this.connManager.shutdown();
|
||||
if (this.closeables != null) {
|
||||
for (Closeable closeable: this.closeables) {
|
||||
try {
|
||||
closeable.close();
|
||||
} catch (IOException ex) {
|
||||
this.log.error(ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ClientConnectionManager getConnectionManager() {
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
|
||||
package org.apache.http.impl.conn;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
|
@ -76,7 +77,7 @@ import org.apache.http.util.LangUtils;
|
|||
* @since 4.3
|
||||
*/
|
||||
@ThreadSafe
|
||||
public class BasicHttpClientConnectionManager implements HttpClientConnectionManager {
|
||||
public class BasicHttpClientConnectionManager implements HttpClientConnectionManager, Closeable {
|
||||
|
||||
private final Log log = LogFactory.getLog(getClass());
|
||||
|
||||
|
@ -155,6 +156,10 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan
|
|||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
shutdown();
|
||||
}
|
||||
|
||||
HttpRoute getRoute() {
|
||||
return route;
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
|
||||
package org.apache.http.impl.conn;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
|
@ -83,7 +84,7 @@ import org.apache.http.util.Asserts;
|
|||
* @since 4.3
|
||||
*/
|
||||
@ThreadSafe
|
||||
public class PoolingHttpClientConnectionManager implements HttpClientConnectionManager {
|
||||
public class PoolingHttpClientConnectionManager implements HttpClientConnectionManager, Closeable {
|
||||
|
||||
private final Log log = LogFactory.getLog(getClass());
|
||||
|
||||
|
@ -159,6 +160,10 @@ public class PoolingHttpClientConnectionManager implements HttpClientConnectionM
|
|||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
shutdown();
|
||||
}
|
||||
|
||||
private String format(final HttpRoute route, final Object state) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("[route: ").append(route).append("]");
|
||||
|
|
|
@ -447,6 +447,9 @@ public class TestAbortHandling extends IntegrationTestBase {
|
|||
public void shutdown() {
|
||||
}
|
||||
|
||||
public void close() {
|
||||
}
|
||||
|
||||
public void releaseConnection(
|
||||
final HttpClientConnection conn,
|
||||
final Object newState,
|
||||
|
|
Loading…
Reference in New Issue