Skip to content

Commit 61451e0

Browse files
committed
1 parent 93b8b7b commit 61451e0

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

api/src/main/java/org/asynchttpclient/AsyncHttpClientConfig.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public class AsyncHttpClientConfig {
106106
protected boolean allowSslConnectionPool;
107107
protected boolean useRawUrl;
108108
protected boolean removeQueryParamOnRedirect;
109+
protected boolean managedApplicationThreadPool;
109110
protected HostnameVerifier hostnameVerifier;
110111
protected int ioThreadMultiplier;
111112
protected boolean strict302Handling;
@@ -190,6 +191,7 @@ private AsyncHttpClientConfig(int maxTotalConnections,
190191
this.useRelativeURIsWithSSLProxies = useRelativeURIsWithSSLProxies;
191192

192193
if (applicationThreadPool == null) {
194+
managedApplicationThreadPool = true;
193195
this.applicationThreadPool = Executors.newCachedThreadPool();
194196
} else {
195197
this.applicationThreadPool = applicationThreadPool;
@@ -342,6 +344,19 @@ public ExecutorService executorService() {
342344
return applicationThreadPool;
343345
}
344346

347+
/**
348+
* @return <code>true</code> if this <code>AsyncHttpClientConfig</code> instance created the
349+
* {@link ExecutorService} returned by {@link #executorService()}, otherwise returns <code>false</code>.
350+
* The return from this method is typically used by the various provider implementations to determine
351+
* if it should shutdown the {@link ExecutorService} when the {@link AsyncHttpClient} is closed. Developers
352+
* should take care and not share managed {@link ExecutorService} instances between client instances.
353+
*
354+
* @since 2.2.0
355+
*/
356+
public boolean isManagedExecutorService() {
357+
return managedApplicationThreadPool;
358+
}
359+
345360
/**
346361
* An instance of {@link ProxyServer} used by an {@link AsyncHttpClient}
347362
*

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/GrizzlyAsyncHttpProvider.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,13 @@ public void close() {
183183
try {
184184
connectionManager.destroy();
185185
clientTransport.stop();
186-
final ExecutorService service = clientConfig.executorService();
187-
if (service != null) {
188-
service.shutdown();
186+
if (clientConfig.isManagedExecutorService()) {
187+
final ExecutorService service = clientConfig.executorService();
188+
// service may be null due to a custom configuration that
189+
// leverages Grizzly's SameThreadIOStrategy.
190+
if (service != null) {
191+
service.shutdown();
192+
}
189193
}
190194
if (timeoutExecutor != null) {
191195
timeoutExecutor.stop();

providers/netty/src/main/java/org/asynchttpclient/providers/netty/NettyAsyncHttpProvider.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,9 @@ public void close() {
828828
}
829829
}
830830

831-
config.executorService().shutdown();
831+
if (config.isManagedExecutorService()) {
832+
config.executorService().shutdown();
833+
}
832834
config.reaper().shutdown();
833835
if (this.allowReleaseSocketChannelFactory) {
834836
socketChannelFactory.releaseExternalResources();

0 commit comments

Comments
 (0)