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:
Oleg Kalnichevski 2012-12-19 17:12:03 +00:00
parent ba065fca60
commit b6d7989aae
7 changed files with 65 additions and 5 deletions

View File

@ -88,6 +88,13 @@ public class CachingHttpClientBuilder extends HttpClientBuilder {
} }
HttpCacheStorage storage = this.storage; HttpCacheStorage storage = this.storage;
if (storage == null) { 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); storage = new BasicHttpCacheStorage(cacheConfig);
} }
return new CachingExec(mainExec, return new CachingExec(mainExec,

View File

@ -26,6 +26,7 @@
*/ */
package org.apache.http.impl.client.cache; package org.apache.http.impl.client.cache;
import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.lang.ref.PhantomReference; import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue; import java.lang.ref.ReferenceQueue;
@ -54,7 +55,7 @@ import org.apache.http.util.Args;
* @since 4.1 * @since 4.1
*/ */
@ThreadSafe @ThreadSafe
public class ManagedHttpCacheStorage implements HttpCacheStorage { public class ManagedHttpCacheStorage implements HttpCacheStorage, Closeable {
private final CacheMap entries; private final CacheMap entries;
private final ReferenceQueue<HttpCacheEntry> morque; private final ReferenceQueue<HttpCacheEntry> morque;
@ -157,4 +158,8 @@ public class ManagedHttpCacheStorage implements HttpCacheStorage {
} }
} }
public void close() {
shutdown();
}
} }

View File

@ -27,10 +27,13 @@
package org.apache.http.impl.client; package org.apache.http.impl.client;
import java.io.Closeable;
import java.net.ProxySelector; import java.net.ProxySelector;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.http.ConnectionReuseStrategy; import org.apache.http.ConnectionReuseStrategy;
@ -181,6 +184,8 @@ public class HttpClientBuilder {
private int maxConnTotal = 0; private int maxConnTotal = 0;
private int maxConnPerRoute = 0; private int maxConnPerRoute = 0;
private List<Closeable> closeables;
public static HttpClientBuilder create() { public static HttpClientBuilder create() {
return new HttpClientBuilder(); return new HttpClientBuilder();
} }
@ -431,6 +436,17 @@ public class HttpClientBuilder {
return protocolExec; 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() { public CloseableHttpClient build() {
// Create main request executor // Create main request executor
HttpRequestExecutor requestExec = this.requestExec; HttpRequestExecutor requestExec = this.requestExec;
@ -679,7 +695,8 @@ public class HttpClientBuilder {
authSchemeRegistry, authSchemeRegistry,
defaultCookieStore, defaultCookieStore,
defaultCredentialsProvider, defaultCredentialsProvider,
defaultRequestConfig != null ? defaultRequestConfig : RequestConfig.DEFAULT); defaultRequestConfig != null ? defaultRequestConfig : RequestConfig.DEFAULT,
closeables != null ? new ArrayList<Closeable>(closeables) : null);
} }
} }

View File

@ -27,9 +27,13 @@
package org.apache.http.impl.client; package org.apache.http.impl.client;
import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit; 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.HttpException;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.HttpRequest; import org.apache.http.HttpRequest;
@ -72,6 +76,8 @@ import org.apache.http.util.Asserts;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
class InternalHttpClient extends CloseableHttpClient { class InternalHttpClient extends CloseableHttpClient {
private final Log log = LogFactory.getLog(getClass());
private final ClientExecChain execChain; private final ClientExecChain execChain;
private final HttpClientConnectionManager connManager; private final HttpClientConnectionManager connManager;
private final HttpRoutePlanner routePlanner; private final HttpRoutePlanner routePlanner;
@ -80,6 +86,7 @@ class InternalHttpClient extends CloseableHttpClient {
private final CookieStore cookieStore; private final CookieStore cookieStore;
private final CredentialsProvider credentialsProvider; private final CredentialsProvider credentialsProvider;
private final RequestConfig defaultConfig; private final RequestConfig defaultConfig;
private final List<Closeable> closeables;
private final HttpParams params; private final HttpParams params;
public InternalHttpClient( public InternalHttpClient(
@ -90,7 +97,8 @@ class InternalHttpClient extends CloseableHttpClient {
final Lookup<AuthSchemeProvider> authSchemeRegistry, final Lookup<AuthSchemeProvider> authSchemeRegistry,
final CookieStore cookieStore, final CookieStore cookieStore,
final CredentialsProvider credentialsProvider, final CredentialsProvider credentialsProvider,
final RequestConfig defaultConfig) { final RequestConfig defaultConfig,
final List<Closeable> closeables) {
super(); super();
Args.notNull(execChain, "HTTP client exec chain"); Args.notNull(execChain, "HTTP client exec chain");
Args.notNull(connManager, "HTTP connection manager"); Args.notNull(connManager, "HTTP connection manager");
@ -103,6 +111,7 @@ class InternalHttpClient extends CloseableHttpClient {
this.cookieStore = cookieStore; this.cookieStore = cookieStore;
this.credentialsProvider = credentialsProvider; this.credentialsProvider = credentialsProvider;
this.defaultConfig = defaultConfig; this.defaultConfig = defaultConfig;
this.closeables = closeables;
this.params = new BasicHttpParams(); this.params = new BasicHttpParams();
} }
@ -179,6 +188,15 @@ class InternalHttpClient extends CloseableHttpClient {
public void close() { public void close() {
this.connManager.shutdown(); 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() { public ClientConnectionManager getConnectionManager() {

View File

@ -27,6 +27,7 @@
package org.apache.http.impl.conn; package org.apache.http.impl.conn;
import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@ -76,7 +77,7 @@ import org.apache.http.util.LangUtils;
* @since 4.3 * @since 4.3
*/ */
@ThreadSafe @ThreadSafe
public class BasicHttpClientConnectionManager implements HttpClientConnectionManager { public class BasicHttpClientConnectionManager implements HttpClientConnectionManager, Closeable {
private final Log log = LogFactory.getLog(getClass()); private final Log log = LogFactory.getLog(getClass());
@ -155,6 +156,10 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan
} }
} }
public void close() {
shutdown();
}
HttpRoute getRoute() { HttpRoute getRoute() {
return route; return route;
} }

View File

@ -26,6 +26,7 @@
package org.apache.http.impl.conn; package org.apache.http.impl.conn;
import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@ -83,7 +84,7 @@ import org.apache.http.util.Asserts;
* @since 4.3 * @since 4.3
*/ */
@ThreadSafe @ThreadSafe
public class PoolingHttpClientConnectionManager implements HttpClientConnectionManager { public class PoolingHttpClientConnectionManager implements HttpClientConnectionManager, Closeable {
private final Log log = LogFactory.getLog(getClass()); 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) { private String format(final HttpRoute route, final Object state) {
StringBuilder buf = new StringBuilder(); StringBuilder buf = new StringBuilder();
buf.append("[route: ").append(route).append("]"); buf.append("[route: ").append(route).append("]");

View File

@ -447,6 +447,9 @@ public class TestAbortHandling extends IntegrationTestBase {
public void shutdown() { public void shutdown() {
} }
public void close() {
}
public void releaseConnection( public void releaseConnection(
final HttpClientConnection conn, final HttpClientConnection conn,
final Object newState, final Object newState,