diff --git a/pom.xml b/pom.xml index 624f598997..3bb2bcca45 100644 --- a/pom.xml +++ b/pom.xml @@ -433,6 +433,7 @@ java.nio.channels.DatagramChannel java.nio.channels.MembershipKey java.net.StandardProtocolFamily + java.nio.channels.spi.SelectorProvider java.nio.channels.AsynchronousChannel diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java index 296dbd846d..3feef9283e 100644 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java @@ -43,6 +43,7 @@ import java.nio.ByteBuffer; import java.nio.channels.DatagramChannel; import java.nio.channels.MembershipKey; import java.nio.channels.SelectionKey; +import java.nio.channels.spi.SelectorProvider; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -60,6 +61,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 final DatagramChannelConfig config; private final Map> memberships = @@ -69,7 +71,13 @@ public final class NioDatagramChannel private static DatagramChannel newSocket() { try { - return DatagramChannel.open(); + /** + * Use the {@link SelectorProvider} to open {@link SocketChannel} and so remove condition in + * {@link SelectorProvider#provider()} which is called by each DatagramChannel.open() otherwise. + * + * See #2308. + */ + return SELECTOR_PROVIDER.openDatagramChannel(); } catch (IOException e) { throw new ChannelException("Failed to open a socket.", e); } @@ -83,7 +91,7 @@ public final class NioDatagramChannel checkJavaVersion(); try { - return DatagramChannel.open(ProtocolFamilyConverter.convert(ipFamily)); + return SELECTOR_PROVIDER.openDatagramChannel(ProtocolFamilyConverter.convert(ipFamily)); } catch (IOException e) { throw new ChannelException("Failed to open a socket.", e); } diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioServerSocketChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/NioServerSocketChannel.java index e294f52b5d..fc04d7a99b 100644 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioServerSocketChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioServerSocketChannel.java @@ -30,6 +30,7 @@ import java.net.SocketAddress; import java.nio.channels.SelectionKey; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; +import java.nio.channels.spi.SelectorProvider; import java.util.List; /** @@ -40,12 +41,19 @@ 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 InternalLogger logger = InternalLoggerFactory.getInstance(NioServerSocketChannel.class); private static ServerSocketChannel newSocket() { try { - return ServerSocketChannel.open(); + /** + * Use the {@link SelectorProvider} to open {@link SocketChannel} and so remove condition in + * {@link SelectorProvider#provider()} which is called by each ServerSocketChannel.open() otherwise. + * + * See #2308. + */ + return SELECTOR_PROVIDER.openServerSocketChannel(); } catch (IOException e) { throw new ChannelException( "Failed to open a server socket.", e); diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioSocketChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/NioSocketChannel.java index 83bc5d0bf3..d0e046c10a 100644 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioSocketChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioSocketChannel.java @@ -36,6 +36,7 @@ import java.net.SocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.SocketChannel; +import java.nio.channels.spi.SelectorProvider; /** * {@link io.netty.channel.socket.SocketChannel} which uses NIO selector based implementation. @@ -43,10 +44,17 @@ import java.nio.channels.SocketChannel; 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 SocketChannel newSocket() { try { - return SocketChannel.open(); + /** + * Use the {@link SelectorProvider} to open {@link SocketChannel} and so remove condition in + * {@link SelectorProvider#provider()} which is called by each SocketChannel.open() otherwise. + * + * See #2308. + */ + return SELECTOR_PROVIDER.openSocketChannel(); } catch (IOException e) { throw new ChannelException("Failed to open a socket.", e); }