Allow specifying SelectorProvider when constructing an NIO channel #2311

Motivation:

At the moment we use the system-wide default selector provider for this invocation of the Java virtual machine when constructing a new NIO channel, which makes using an alternative SelectorProvider practically useless.
This change allows user specify his/her preferred SelectorProvider.

Modifications:

Add SelectorProvider as a param for current `private static *Channel newSocket` method of NioSocketChannel, NioServerSocketChannel and NioDatagramChannel.
Change default constructors of NioSocketChannel, NioServerSocketChannel and NioDatagramChannel to use DEFAULT_SELECTOR_PROVIDER when calling newSocket(SelectorProvider).
Add new constructors for NioSocketChannel, NioServerSocketChannel and NioDatagramChannel which allow user specify his/her preferred SelectorProvider.

Result:

Now users can specify his/her preferred SelectorProvider when constructing an NIO channel.
This commit is contained in:
CoNDoRip 2014-03-23 13:09:41 +04:00 committed by Norman Maurer
parent 76d091a884
commit 68670ba195
3 changed files with 47 additions and 16 deletions

View File

@ -63,7 +63,7 @@ public final class NioDatagramChannel
extends AbstractNioMessageChannel implements io.netty.channel.socket.DatagramChannel {
private static final ChannelMetadata METADATA = new ChannelMetadata(true);
private static final SelectorProvider SELECTOR_PROVIDER = SelectorProvider.provider();
private static final SelectorProvider DEFAULT_SELECTOR_PROVIDER = SelectorProvider.provider();
private final DatagramChannelConfig config;
private final Map<InetAddress, List<MembershipKey>> memberships =
@ -71,7 +71,7 @@ public final class NioDatagramChannel
private RecvByteBufAllocator.Handle allocHandle;
private static DatagramChannel newSocket() {
private static DatagramChannel newSocket(SelectorProvider provider) {
try {
/**
* Use the {@link SelectorProvider} to open {@link SocketChannel} and so remove condition in
@ -79,21 +79,21 @@ public final class NioDatagramChannel
*
* See <a href="See https://github.com/netty/netty/issues/2308">#2308</a>.
*/
return SELECTOR_PROVIDER.openDatagramChannel();
return provider.openDatagramChannel();
} catch (IOException e) {
throw new ChannelException("Failed to open a socket.", e);
}
}
private static DatagramChannel newSocket(InternetProtocolFamily ipFamily) {
private static DatagramChannel newSocket(SelectorProvider provider, InternetProtocolFamily ipFamily) {
if (ipFamily == null) {
return newSocket();
return newSocket(provider);
}
checkJavaVersion();
try {
return SELECTOR_PROVIDER.openDatagramChannel(ProtocolFamilyConverter.convert(ipFamily));
return provider.openDatagramChannel(ProtocolFamilyConverter.convert(ipFamily));
} catch (IOException e) {
throw new ChannelException("Failed to open a socket.", e);
}
@ -109,7 +109,15 @@ public final class NioDatagramChannel
* Create a new instance which will use the Operation Systems default {@link InternetProtocolFamily}.
*/
public NioDatagramChannel() {
this(newSocket());
this(newSocket(DEFAULT_SELECTOR_PROVIDER));
}
/**
* Create a new instance using the given {@link SelectorProvider}
* which will use the Operation Systems default {@link InternetProtocolFamily}.
*/
public NioDatagramChannel(SelectorProvider provider) {
this(newSocket(provider));
}
/**
@ -117,7 +125,16 @@ public final class NioDatagramChannel
* on the Operation Systems default which will be chosen.
*/
public NioDatagramChannel(InternetProtocolFamily ipFamily) {
this(newSocket(ipFamily));
this(newSocket(DEFAULT_SELECTOR_PROVIDER, ipFamily));
}
/**
* Create a new instance using the given {@link SelectorProvider} and {@link InternetProtocolFamily}.
* If {@link InternetProtocolFamily} is {@code null} it will depend on the Operation Systems default
* which will be chosen.
*/
public NioDatagramChannel(SelectorProvider provider, InternetProtocolFamily ipFamily) {
this(newSocket(provider, ipFamily));
}
/**

View File

@ -41,11 +41,11 @@ public class NioServerSocketChannel extends AbstractNioMessageChannel
implements io.netty.channel.socket.ServerSocketChannel {
private static final ChannelMetadata METADATA = new ChannelMetadata(false);
private static final SelectorProvider SELECTOR_PROVIDER = SelectorProvider.provider();
private static final SelectorProvider DEFAULT_SELECTOR_PROVIDER = SelectorProvider.provider();
private static final InternalLogger logger = InternalLoggerFactory.getInstance(NioServerSocketChannel.class);
private static ServerSocketChannel newSocket() {
private static ServerSocketChannel newSocket(SelectorProvider provider) {
try {
/**
* Use the {@link SelectorProvider} to open {@link SocketChannel} and so remove condition in
@ -53,7 +53,7 @@ public class NioServerSocketChannel extends AbstractNioMessageChannel
*
* See <a href="See https://github.com/netty/netty/issues/2308">#2308</a>.
*/
return SELECTOR_PROVIDER.openServerSocketChannel();
return provider.openServerSocketChannel();
} catch (IOException e) {
throw new ChannelException(
"Failed to open a server socket.", e);
@ -66,7 +66,14 @@ public class NioServerSocketChannel extends AbstractNioMessageChannel
* Create a new instance
*/
public NioServerSocketChannel() {
this(newSocket());
this(newSocket(DEFAULT_SELECTOR_PROVIDER));
}
/**
* Create a new instance using the given {@link SelectorProvider}.
*/
public NioServerSocketChannel(SelectorProvider provider) {
this(newSocket(provider));
}
/**

View File

@ -44,9 +44,9 @@ import java.nio.channels.spi.SelectorProvider;
public class NioSocketChannel extends AbstractNioByteChannel implements io.netty.channel.socket.SocketChannel {
private static final ChannelMetadata METADATA = new ChannelMetadata(false);
private static final SelectorProvider SELECTOR_PROVIDER = SelectorProvider.provider();
private static final SelectorProvider DEFAULT_SELECTOR_PROVIDER = SelectorProvider.provider();
private static SocketChannel newSocket() {
private static SocketChannel newSocket(SelectorProvider provider) {
try {
/**
* Use the {@link SelectorProvider} to open {@link SocketChannel} and so remove condition in
@ -54,7 +54,7 @@ public class NioSocketChannel extends AbstractNioByteChannel implements io.netty
*
* See <a href="See https://github.com/netty/netty/issues/2308">#2308</a>.
*/
return SELECTOR_PROVIDER.openSocketChannel();
return provider.openSocketChannel();
} catch (IOException e) {
throw new ChannelException("Failed to open a socket.", e);
}
@ -66,7 +66,14 @@ public class NioSocketChannel extends AbstractNioByteChannel implements io.netty
* Create a new instance
*/
public NioSocketChannel() {
this(newSocket());
this(newSocket(DEFAULT_SELECTOR_PROVIDER));
}
/**
* Create a new instance using the given {@link SelectorProvider}.
*/
public NioSocketChannel(SelectorProvider provider) {
this(newSocket(provider));
}
/**