From 584e07328c59cc89e20349cfb3ae2ec01811a953 Mon Sep 17 00:00:00 2001 From: Gravit Date: Sat, 26 Sep 2020 02:47:19 +0700 Subject: [PATCH] [FEATURE][EXPERIMENTAL] NettyThreadFactory --- .../socket/LauncherNettyServer.java | 4 ++-- .../socket/NettyObjectFactory.java | 7 +++---- .../socket/NettyThreadFactory.java | 20 +++++++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/socket/NettyThreadFactory.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/LauncherNettyServer.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/LauncherNettyServer.java index 7b4f621e..668df90d 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/LauncherNettyServer.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/LauncherNettyServer.java @@ -42,8 +42,8 @@ public LauncherNettyServer(LaunchServer server) { if (config.performance.usingEpoll && !Epoll.isAvailable()) { LogHelper.error("Epoll is not available: (netty,perfomance.usingEpoll configured wrongly)", Epoll.unavailabilityCause()); } - bossGroup = NettyObjectFactory.newEventLoopGroup(config.performance.bossThread); - workerGroup = NettyObjectFactory.newEventLoopGroup(config.performance.workerThread); + bossGroup = NettyObjectFactory.newEventLoopGroup(config.performance.bossThread, "LauncherNettyServer.bossGroup"); + workerGroup = NettyObjectFactory.newEventLoopGroup(config.performance.workerThread, "LauncherNettyServer.workerGroup"); serverBootstrap = new ServerBootstrap(); service = new WebSocketService(new DefaultChannelGroup(GlobalEventExecutor.INSTANCE), server); serverBootstrap.group(bossGroup, workerGroup) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/NettyObjectFactory.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/NettyObjectFactory.java index 1e814b65..69cb593a 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/NettyObjectFactory.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/NettyObjectFactory.java @@ -15,11 +15,11 @@ public static void setUsingEpoll(boolean value) { epoll = value; } - public static EventLoopGroup newEventLoopGroup(int threads) { + public static EventLoopGroup newEventLoopGroup(int threads, String poolName) { if (epoll) - return new EpollEventLoopGroup(threads); + return new EpollEventLoopGroup(threads, new NettyThreadFactory(poolName)); else - return new NioEventLoopGroup(threads); + return new NioEventLoopGroup(threads, new NettyThreadFactory(poolName)); } public static ChannelFactory getServerSocketChannelFactory() { @@ -28,5 +28,4 @@ public static ChannelFactory getServerSocketChannelFact else return NioServerSocketChannel::new; } - } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/NettyThreadFactory.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/NettyThreadFactory.java new file mode 100644 index 00000000..98b81f14 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/NettyThreadFactory.java @@ -0,0 +1,20 @@ +package pro.gravit.launchserver.socket; + +import io.netty.util.concurrent.DefaultThreadFactory; +import pro.gravit.utils.helper.LogHelper; + +public class NettyThreadFactory extends DefaultThreadFactory { + public NettyThreadFactory(String poolName) { + super(poolName); + } + + @Override + protected Thread newThread(Runnable r, String name) { + Thread thread = super.newThread(r, name); + thread.setUncaughtExceptionHandler((th, e) -> { + if(LogHelper.isDebugEnabled()) + LogHelper.error(e); + }); + return thread; + } +}