|
@@ -4,6 +4,7 @@ import com.incubator.common.log4j.Log;
|
|
|
import com.incubator.common.thread.NamedThreadFactory;
|
|
|
import io.netty.channel.EventLoopGroup;
|
|
|
import io.netty.channel.ServerChannel;
|
|
|
+import io.netty.channel.epoll.EpollEventLoopGroup;
|
|
|
import io.netty.channel.epoll.EpollServerSocketChannel;
|
|
|
import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
@@ -32,14 +33,14 @@ public class NettyServerConfig {
|
|
|
|
|
|
public NettyServerConfig() {
|
|
|
this(getDefaultChannelClass(),
|
|
|
- new NioEventLoopGroup(DEFAULT_BOSS_THREADS, new NamedThreadFactory("Server-boss", true)),
|
|
|
- new NioEventLoopGroup(DEFAULT_IO_THREADS, new NamedThreadFactory("Server-ioWorkers", true)));
|
|
|
+ getEventLoopGroup(DEFAULT_BOSS_THREADS, "Server-boss"),
|
|
|
+ getEventLoopGroup(DEFAULT_IO_THREADS, "Server-ioWorkers"));
|
|
|
}
|
|
|
|
|
|
public NettyServerConfig(int bossThreads, int ioThreads) {
|
|
|
this(getDefaultChannelClass(),
|
|
|
- new NioEventLoopGroup(bossThreads, new NamedThreadFactory("Server-boss", true)),
|
|
|
- new NioEventLoopGroup(ioThreads, new NamedThreadFactory("Server-ioWorkers", true)));
|
|
|
+ getEventLoopGroup(bossThreads, "Server-boss"),
|
|
|
+ getEventLoopGroup(ioThreads, "Server-ioWorkers"));
|
|
|
}
|
|
|
|
|
|
public Class<? extends ServerChannel> getChannelClass() {
|
|
@@ -57,13 +58,26 @@ public class NettyServerConfig {
|
|
|
|
|
|
* 获取默认的 Channel 类型,根据操作系统选择。
|
|
|
*/
|
|
|
- private static Class<? extends ServerChannel> getDefaultChannelClass() {
|
|
|
+ public static Class<? extends ServerChannel> getDefaultChannelClass() {
|
|
|
if (isLinuxPlatform()) {
|
|
|
return EpollServerSocketChannel.class;
|
|
|
}
|
|
|
return NioServerSocketChannel.class;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * 获取合适的 EventLoopGroup 实例,根据操作系统选择 Nio 或 Epoll。
|
|
|
+ */
|
|
|
+ private static EventLoopGroup getEventLoopGroup(int threads, String namePrefix) {
|
|
|
+ if (isLinuxPlatform() && isEpollAvailable()) {
|
|
|
+
|
|
|
+ return new EpollEventLoopGroup(threads, new NamedThreadFactory(namePrefix, true));
|
|
|
+ } else {
|
|
|
+
|
|
|
+ return new NioEventLoopGroup(threads, new NamedThreadFactory(namePrefix, true));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
* 判断是否为 Linux 平台。
|
|
|
*/
|
|
@@ -72,6 +86,19 @@ public class NettyServerConfig {
|
|
|
return osName.contains("linux");
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ * 判断是否支持 Epoll。
|
|
|
+ */
|
|
|
+ private static boolean isEpollAvailable() {
|
|
|
+ try {
|
|
|
+
|
|
|
+ Class.forName("io.netty.channel.epoll.Epoll");
|
|
|
+ return true;
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
* 释放资源。
|
|
|
*/
|