Skip to content

Commit 98872c9

Browse files
author
Stephane Landelle
committed
Merge pull request #219 from andekaputra/master
Allow to configure NioClientSocketChannelFactory to NettyAsyncHttpProvider
2 parents e01620a + c431fc1 commit 98872c9

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

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

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ public class NettyAsyncHttpProvider extends SimpleChannelUpstreamHandler impleme
158158
private final AsyncHttpClientConfig config;
159159
private final AtomicBoolean isClose = new AtomicBoolean(false);
160160
private final ClientSocketChannelFactory socketChannelFactory;
161+
private final boolean allowReleaseSocketChannelFactory;
161162

162163
private final ChannelGroup openChannels = new
163164
CleanupChannelGroup("asyncHttpClient") {
@@ -193,17 +194,28 @@ public NettyAsyncHttpProvider(AsyncHttpClientConfig config) {
193194

194195
if (asyncHttpProviderConfig.getProperty(NettyAsyncHttpProviderConfig.USE_BLOCKING_IO) != null) {
195196
socketChannelFactory = new OioClientSocketChannelFactory(config.executorService());
197+
this.allowReleaseSocketChannelFactory = true;
196198
} 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;
201206
} 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;
203218
}
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);
207219
}
208220
plainBootstrap = new ClientBootstrap(socketChannelFactory);
209221
secureBootstrap = new ClientBootstrap(socketChannelFactory);
@@ -860,11 +872,13 @@ public void close() {
860872

861873
config.executorService().shutdown();
862874
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+
}
868882
} catch (Throwable t) {
869883
log.warn("Unexpected error on close", t);
870884
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public class NettyAsyncHttpProviderConfig implements AsyncHttpProviderConfig<Str
5151
* Allow configuring the Netty's boss executor service.
5252
*/
5353
public final static String BOSS_EXECUTOR_SERVICE = "bossExecutorService";
54+
55+
/**
56+
* Allow configuring the Netty's socket channel factory.
57+
*/
58+
public final static String SOCKET_CHANNEL_FACTORY = "socketChannelFactory";
5459

5560
/**
5661
* See {@link java.net.Socket#setReuseAddress(boolean)}

0 commit comments

Comments
 (0)