@@ -158,6 +158,7 @@ public class NettyAsyncHttpProvider extends SimpleChannelUpstreamHandler impleme
158
158
private final AsyncHttpClientConfig config ;
159
159
private final AtomicBoolean isClose = new AtomicBoolean (false );
160
160
private final ClientSocketChannelFactory socketChannelFactory ;
161
+ private final boolean allowReleaseSocketChannelFactory ;
161
162
162
163
private final ChannelGroup openChannels = new
163
164
CleanupChannelGroup ("asyncHttpClient" ) {
@@ -193,17 +194,28 @@ public NettyAsyncHttpProvider(AsyncHttpClientConfig config) {
193
194
194
195
if (asyncHttpProviderConfig .getProperty (NettyAsyncHttpProviderConfig .USE_BLOCKING_IO ) != null ) {
195
196
socketChannelFactory = new OioClientSocketChannelFactory (config .executorService ());
197
+ this .allowReleaseSocketChannelFactory = true ;
196
198
} else {
197
- ExecutorService e ;
198
- Object o = asyncHttpProviderConfig .getProperty (NettyAsyncHttpProviderConfig .BOSS_EXECUTOR_SERVICE );
199
- if (o != null && ExecutorService .class .isAssignableFrom (o .getClass ())) {
200
- e = ExecutorService .class .cast (o );
199
+ // check if external NioClientSocketChannelFactory is defined
200
+ Object oo = asyncHttpProviderConfig .getProperty (NettyAsyncHttpProviderConfig .SOCKET_CHANNEL_FACTORY );
201
+ if (oo != null && NioClientSocketChannelFactory .class .isAssignableFrom (oo .getClass ())) {
202
+ this .socketChannelFactory = NioClientSocketChannelFactory .class .cast (oo );
203
+
204
+ // cannot allow releasing shared channel factory
205
+ this .allowReleaseSocketChannelFactory = false ;
201
206
} else {
202
- e = Executors .newCachedThreadPool ();
207
+ ExecutorService e = null ;
208
+ Object o = asyncHttpProviderConfig .getProperty (NettyAsyncHttpProviderConfig .BOSS_EXECUTOR_SERVICE );
209
+ if (o != null && ExecutorService .class .isAssignableFrom (o .getClass ())) {
210
+ e = ExecutorService .class .cast (o );
211
+ } else {
212
+ e = Executors .newCachedThreadPool ();
213
+ }
214
+ int numWorkers = config .getIoThreadMultiplier () * Runtime .getRuntime ().availableProcessors ();
215
+ log .debug ("Number of application's worker threads is {}" , numWorkers );
216
+ socketChannelFactory = new NioClientSocketChannelFactory (e , config .executorService (), numWorkers );
217
+ this .allowReleaseSocketChannelFactory = true ;
203
218
}
204
- int numWorkers = config .getIoThreadMultiplier () * Runtime .getRuntime ().availableProcessors ();
205
- log .debug ("Number of application's worker threads is {}" , numWorkers );
206
- socketChannelFactory = new NioClientSocketChannelFactory (e , config .executorService (), numWorkers );
207
219
}
208
220
plainBootstrap = new ClientBootstrap (socketChannelFactory );
209
221
secureBootstrap = new ClientBootstrap (socketChannelFactory );
@@ -860,11 +872,13 @@ public void close() {
860
872
861
873
config .executorService ().shutdown ();
862
874
config .reaper ().shutdown ();
863
- socketChannelFactory .releaseExternalResources ();
864
- plainBootstrap .releaseExternalResources ();
865
- secureBootstrap .releaseExternalResources ();
866
- webSocketBootstrap .releaseExternalResources ();
867
- secureWebSocketBootstrap .releaseExternalResources ();
875
+ if (this .allowReleaseSocketChannelFactory ) {
876
+ socketChannelFactory .releaseExternalResources ();
877
+ plainBootstrap .releaseExternalResources ();
878
+ secureBootstrap .releaseExternalResources ();
879
+ webSocketBootstrap .releaseExternalResources ();
880
+ secureWebSocketBootstrap .releaseExternalResources ();
881
+ }
868
882
} catch (Throwable t ) {
869
883
log .warn ("Unexpected error on close" , t );
870
884
}
0 commit comments