Skip to content

Commit 754bdbf

Browse files
author
Stephane Landelle
committed
Move reaper to Apache provider specific config, close AsyncHttpClient#331
1 parent 88ccbf4 commit 754bdbf

File tree

5 files changed

+38
-37
lines changed

5 files changed

+38
-37
lines changed

src/main/java/com/ning/http/client/AsyncHttpClientConfig.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public class AsyncHttpClientConfig {
6565
protected boolean compressionEnabled;
6666
protected String userAgent;
6767
protected boolean allowPoolingConnection;
68-
protected ScheduledExecutorService reaper;
6968
protected ExecutorService applicationThreadPool;
7069
protected ProxyServerSelector proxyServerSelector;
7170
protected SSLContext sslContext;
@@ -148,7 +147,6 @@ private AsyncHttpClientConfig(int maxTotalConnections,
148147
this.ioExceptionFilters = ioExceptionFilters;
149148
this.requestCompressionLevel = requestCompressionLevel;
150149
this.maxRequestRetry = maxRequestRetry;
151-
this.reaper = reaper;
152150
this.allowSslConnectionPool = allowSslConnectionCaching;
153151
this.removeQueryParamOnRedirect = removeQueryParamOnRedirect;
154152
this.hostnameVerifier = hostnameVerifier;
@@ -166,15 +164,6 @@ private AsyncHttpClientConfig(int maxTotalConnections,
166164
this.useRawUrl = useRawUrl;
167165
}
168166

169-
/**
170-
* A {@link ScheduledExecutorService} used to expire idle connections.
171-
*
172-
* @return {@link ScheduledExecutorService}
173-
*/
174-
public ScheduledExecutorService reaper() {
175-
return reaper;
176-
}
177-
178167
/**
179168
* Return the maximum number of connections an {@link com.ning.http.client.AsyncHttpClient} can handle.
180169
*
@@ -469,7 +458,7 @@ public boolean isValid() {
469458
// when using a ManagedExecutorService.
470459
// When this is the case, we assume it's running.
471460
}
472-
return (atpRunning && !reaper.isShutdown());
461+
return atpRunning;
473462
}
474463

475464
/**
@@ -1091,7 +1080,6 @@ public Builder(AsyncHttpClientConfig prototype) {
10911080
userAgent = prototype.getUserAgent();
10921081
redirectEnabled = prototype.isRedirectEnabled();
10931082
compressionEnabled = prototype.isCompressionEnabled();
1094-
reaper = prototype.reaper();
10951083
applicationThreadPool = prototype.executorService();
10961084

10971085
requestFilters.clear();

src/main/java/com/ning/http/client/AsyncHttpClientConfigBean.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.LinkedList;
2424
import java.util.concurrent.ExecutorService;
2525
import java.util.concurrent.Executors;
26-
import java.util.concurrent.ScheduledExecutorService;
2726
import java.util.concurrent.ThreadFactory;
2827

2928
/**
@@ -79,13 +78,6 @@ public boolean verify(String s, SSLSession sslSession) {
7978
}
8079

8180
void configureExecutors() {
82-
reaper = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {
83-
public Thread newThread(Runnable r) {
84-
Thread t = new Thread(r, "AsyncHttpClient-Reaper");
85-
t.setDaemon(true);
86-
return t;
87-
}
88-
});
8981
applicationThreadPool = Executors.newCachedThreadPool(new ThreadFactory() {
9082
public Thread newThread(Runnable r) {
9183
Thread t = new Thread(r, "AsyncHttpClient-Callback");
@@ -150,14 +142,6 @@ public AsyncHttpClientConfigBean setAllowPoolingConnection(boolean allowPoolingC
150142
return this;
151143
}
152144

153-
public AsyncHttpClientConfigBean setReaper(ScheduledExecutorService reaper) {
154-
if (this.reaper != null) {
155-
this.reaper.shutdownNow();
156-
}
157-
this.reaper = reaper;
158-
return this;
159-
}
160-
161145
public AsyncHttpClientConfigBean setApplicationThreadPool(ExecutorService applicationThreadPool) {
162146
if (this.applicationThreadPool != null) {
163147
this.applicationThreadPool.shutdownNow();

src/main/java/com/ning/http/client/providers/apache/ApacheAsyncHttpProvider.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.ning.http.util.AsyncHttpProviderUtils;
4646
import com.ning.http.util.ProxyUtils;
4747
import com.ning.http.util.UTF8UrlEncoder;
48+
4849
import org.apache.commons.httpclient.CircularRedirectException;
4950
import org.apache.commons.httpclient.Credentials;
5051
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
@@ -85,6 +86,7 @@
8586
import javax.net.ssl.SSLSocketFactory;
8687
import javax.net.ssl.TrustManager;
8788
import javax.net.ssl.X509TrustManager;
89+
8890
import java.io.ByteArrayInputStream;
8991
import java.io.File;
9092
import java.io.FileInputStream;
@@ -107,7 +109,10 @@
107109
import java.util.Map;
108110
import java.util.concurrent.Callable;
109111
import java.util.concurrent.ExecutionException;
112+
import java.util.concurrent.Executors;
110113
import java.util.concurrent.Future;
114+
import java.util.concurrent.ScheduledExecutorService;
115+
import java.util.concurrent.ThreadFactory;
111116
import java.util.concurrent.TimeUnit;
112117
import java.util.concurrent.TimeoutException;
113118
import java.util.concurrent.atomic.AtomicBoolean;
@@ -129,6 +134,7 @@ public class ApacheAsyncHttpProvider implements AsyncHttpProvider {
129134
private final AtomicInteger maxConnections = new AtomicInteger();
130135
private final MultiThreadedHttpConnectionManager connectionManager;
131136
private final HttpClientParams params;
137+
private final ScheduledExecutorService reaper;
132138

133139
static {
134140
final SocketFactory factory = new TrustingSSLSocketFactory();
@@ -157,13 +163,26 @@ public ApacheAsyncHttpProvider(AsyncHttpClientConfig config) {
157163
params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
158164
params.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
159165

160-
AsyncHttpProviderConfig<?, ?> providerConfig = config.getAsyncHttpProviderConfig();
166+
reaper = getReaper(config.getAsyncHttpProviderConfig());
167+
}
168+
169+
private ScheduledExecutorService getReaper(AsyncHttpProviderConfig<?, ?> providerConfig) {
170+
171+
ScheduledExecutorService reaper = null;
161172
if (providerConfig instanceof ApacheAsyncHttpProvider) {
162-
configure(ApacheAsyncHttpProviderConfig.class.cast(providerConfig));
173+
reaper = ApacheAsyncHttpProviderConfig.class.cast(providerConfig).getReaper();
163174
}
164-
}
165175

166-
private void configure(ApacheAsyncHttpProviderConfig config) {
176+
if (reaper == null)
177+
reaper = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {
178+
public Thread newThread(Runnable r) {
179+
Thread t = new Thread(r, "AsyncHttpClient-Reaper");
180+
t.setDaemon(true);
181+
return t;
182+
}
183+
});
184+
185+
return reaper;
167186
}
168187

169188
public <T> ListenableFuture<T> execute(Request request, AsyncHandler<T> handler) throws IOException {
@@ -211,6 +230,7 @@ public <T> ListenableFuture<T> execute(Request request, AsyncHandler<T> handler)
211230
}
212231

213232
public void close() {
233+
reaper.shutdown();
214234
if (idleConnectionTimeoutThread != null) {
215235
idleConnectionTimeoutThread.shutdown();
216236
idleConnectionTimeoutThread = null;
@@ -455,7 +475,7 @@ public T call() {
455475
int delay = requestTimeout(config, future.getRequest().getPerRequestConfig());
456476
if (delay != -1) {
457477
ReaperFuture reaperFuture = new ReaperFuture(future);
458-
Future scheduledFuture = config.reaper().scheduleAtFixedRate(reaperFuture, delay, 500, TimeUnit.MILLISECONDS);
478+
Future scheduledFuture = reaper.scheduleAtFixedRate(reaperFuture, delay, 500, TimeUnit.MILLISECONDS);
459479
reaperFuture.setScheduledFuture(scheduledFuture);
460480
future.setReaperFuture(reaperFuture);
461481
}

src/main/java/com/ning/http/client/providers/apache/ApacheAsyncHttpProviderConfig.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@
1212
*/
1313
package com.ning.http.client.providers.apache;
1414

15-
import com.ning.http.client.AsyncHttpProviderConfig;
16-
1715
import java.util.Map;
1816
import java.util.Set;
1917
import java.util.concurrent.ConcurrentHashMap;
18+
import java.util.concurrent.ScheduledExecutorService;
19+
20+
import com.ning.http.client.AsyncHttpProviderConfig;
2021

2122
public class ApacheAsyncHttpProviderConfig implements AsyncHttpProviderConfig<String, String> {
2223

2324
private final ConcurrentHashMap<String, String> properties = new ConcurrentHashMap<String, String>();
2425

26+
private ScheduledExecutorService reaper;
2527

2628
public AsyncHttpProviderConfig addProperty(String name, String value) {
2729
properties.put(name, value);
@@ -39,4 +41,12 @@ public String removeProperty(String name) {
3941
public Set<Map.Entry<String, String>> propertiesSet() {
4042
return properties.entrySet();
4143
}
44+
45+
public void setReaper(ScheduledExecutorService reaper) {
46+
this.reaper = reaper;
47+
}
48+
49+
public ScheduledExecutorService getReaper() {
50+
return reaper;
51+
}
4252
}

src/main/java/com/ning/http/client/providers/netty/NettyAsyncHttpProvider.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,6 @@ public void close() {
889889
}
890890

891891
config.executorService().shutdown();
892-
config.reaper().shutdown();
893892
if (this.allowReleaseSocketChannelFactory) {
894893
socketChannelFactory.releaseExternalResources();
895894
plainBootstrap.releaseExternalResources();

0 commit comments

Comments
 (0)