perf[net]: 把优雅关闭线程池的日志打印的更加详细

This commit is contained in:
godotg
2022-08-23 16:47:06 +08:00
parent 77ba9c0ee8
commit 36f895d279
3 changed files with 28 additions and 6 deletions
@@ -18,6 +18,7 @@ import com.zfoo.net.handler.BaseRouteHandler;
import com.zfoo.net.session.model.Session;
import com.zfoo.protocol.exception.ExceptionUtils;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
@@ -87,7 +88,7 @@ public abstract class AbstractClient implements IClient {
public synchronized static void shutdown() {
AbstractServer.shutdownEventLoopGracefully(nioEventLoopGroup);
ThreadUtils.shutdownEventLoopGracefully("netty-client", nioEventLoopGroup);
}
}
@@ -14,6 +14,7 @@
package com.zfoo.net.core;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.util.ThreadUtils;
import com.zfoo.util.net.HostAndPort;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
@@ -103,9 +104,8 @@ public abstract class AbstractServer implements IServer {
@Override
public synchronized void shutdown() {
shutdownEventLoopGracefully(bossGroup);
shutdownEventLoopGracefully(workerGroup);
ThreadUtils.shutdownEventLoopGracefully("netty-boss", bossGroup);
ThreadUtils.shutdownEventLoopGracefully("netty-worker", workerGroup);
if (channelFuture != null) {
try {
@@ -136,7 +136,7 @@ public abstract class AbstractServer implements IServer {
logger.error("EventLoop Thread pool [{}] is failed to shutdown! ", executor, e);
return;
}
logger.info("EventLoop Thread pool [{}] shuts down gracefully.", executor);
logger.info("EventLoop Thread pool [{}] shutdown gracefully.", executor);
}
public synchronized static void shutdownAllServers() {
@@ -13,6 +13,10 @@
package com.zfoo.util;
import io.netty.util.concurrent.EventExecutorGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
@@ -23,6 +27,8 @@ import java.util.concurrent.TimeUnit;
*/
public abstract class ThreadUtils {
private static final Logger logger = LoggerFactory.getLogger(ThreadUtils.class);
private static final int WAIT_TIME = 10;
private static final TimeUnit TIME_UNIT = TimeUnit.SECONDS;
@@ -46,10 +52,25 @@ public abstract class ThreadUtils {
}
} catch (Exception e) {
throw new RuntimeException(e);
logger.error("[{}] is failed to shutdown! ", executor, e);
}
}
public synchronized static void shutdownEventLoopGracefully(String executorGroupName, EventExecutorGroup executor) {
if (executor == null) {
return;
}
try {
if (!executor.isTerminated()) {
executor.shutdownGracefully();
}
} catch (Exception e) {
logger.error("[{}] is failed to shutdown! ", executorGroupName, e);
return;
}
logger.info("[{}] shutdown gracefully.", executorGroupName);
}
public static void shutdownForkJoinPool() {
try {
ForkJoinPool.commonPool().shutdown();