mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-18 06:27:01 +00:00
fix[zfoo]: 优雅关闭服务器,避免bean财富回调shutdown方法
This commit is contained in:
@@ -26,7 +26,6 @@ import org.springframework.context.event.ContextClosedEvent;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
/**
|
||||
@@ -49,13 +48,13 @@ public class EventContext implements ApplicationListener<ApplicationContextEvent
|
||||
return instance.applicationContext;
|
||||
}
|
||||
|
||||
public synchronized static void shutdown() {
|
||||
private synchronized void shutdown() {
|
||||
try {
|
||||
Field field = EventBus.class.getDeclaredField("executors");
|
||||
var field = EventBus.class.getDeclaredField("executors");
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
|
||||
var executors = (ExecutorService[]) ReflectionUtils.getField(field, null);
|
||||
for (ExecutorService executor : executors) {
|
||||
for (var executor : executors) {
|
||||
ThreadUtils.shutdown(executor);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
|
||||
@@ -13,16 +13,18 @@
|
||||
|
||||
package com.zfoo.net;
|
||||
|
||||
import com.zfoo.event.manager.EventBus;
|
||||
import com.zfoo.net.config.manager.IConfigManager;
|
||||
import com.zfoo.net.consumer.service.IConsumer;
|
||||
import com.zfoo.net.core.AbstractClient;
|
||||
import com.zfoo.net.core.AbstractServer;
|
||||
import com.zfoo.net.core.tcp.TcpClient;
|
||||
import com.zfoo.net.dispatcher.manager.IPacketDispatcher;
|
||||
import com.zfoo.net.packet.service.IPacketService;
|
||||
import com.zfoo.net.session.manager.ISessionManager;
|
||||
import com.zfoo.net.session.model.Session;
|
||||
import com.zfoo.net.task.TaskManager;
|
||||
import com.zfoo.protocol.collection.ArrayUtils;
|
||||
import com.zfoo.protocol.exception.ExceptionUtils;
|
||||
import com.zfoo.protocol.util.IOUtils;
|
||||
import com.zfoo.protocol.util.ReflectionUtils;
|
||||
import com.zfoo.scheduler.SchedulerContext;
|
||||
import com.zfoo.scheduler.model.StopWatch;
|
||||
@@ -37,6 +39,7 @@ import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
/**
|
||||
@@ -112,25 +115,25 @@ public class NetContext implements ApplicationListener<ApplicationContextEvent>,
|
||||
}
|
||||
|
||||
|
||||
public synchronized static void shutdownBefore() {
|
||||
public synchronized void shutdownBefore() {
|
||||
SchedulerContext.shutdown();
|
||||
}
|
||||
|
||||
public static synchronized void shutdownAfter() {
|
||||
public synchronized void shutdownAfter() {
|
||||
// 关闭zookeeper的客户端
|
||||
NetContext.getConfigManager().getRegistry().shutdown();
|
||||
|
||||
configManager.getRegistry().shutdown();
|
||||
|
||||
// 先关闭所有session
|
||||
NetContext.getSessionManager().shutdown();
|
||||
IOUtils.closeIO(ArrayUtils.listToArray(new ArrayList<>(sessionManager.getClientSessionMap().values()), Session.class));
|
||||
IOUtils.closeIO(ArrayUtils.listToArray(new ArrayList<>(sessionManager.getServerSessionMap().values()), Session.class));
|
||||
|
||||
// 关闭客户端和服务器
|
||||
TcpClient.shutdown();
|
||||
AbstractClient.shutdown();
|
||||
AbstractServer.shutdownAllServers();
|
||||
|
||||
// 关闭TaskManager
|
||||
try {
|
||||
Field field = EventBus.class.getDeclaredField("executors");
|
||||
Field field = TaskManager.class.getDeclaredField("executors");
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
|
||||
var executors = (ExecutorService[]) ReflectionUtils.getField(field, TaskManager.getInstance());
|
||||
|
||||
@@ -42,6 +42,4 @@ public interface ISessionManager {
|
||||
|
||||
int getClientSessionChangeId();
|
||||
|
||||
void shutdown();
|
||||
|
||||
}
|
||||
|
||||
@@ -110,24 +110,4 @@ public class SessionManager implements ISessionManager {
|
||||
return clientSessionChangeId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void shutdown() {
|
||||
clientSessionMap.values().forEach(it -> {
|
||||
try {
|
||||
it.close();
|
||||
} catch (Exception e) {
|
||||
logger.error("关闭[session:{}]发生未知异常", SessionUtils.sessionInfo(it), e);
|
||||
}
|
||||
});
|
||||
|
||||
serverSessionMap.values().forEach(it -> {
|
||||
try {
|
||||
it.close();
|
||||
} catch (Exception e) {
|
||||
logger.error("关闭[session:{}]发生未知异常", SessionUtils.sessionInfo(it), e);
|
||||
}
|
||||
});
|
||||
|
||||
logger.info("已关闭客户端和服务器所有的session");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ package com.zfoo.net.session.model;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
import io.netty.channel.Channel;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
@@ -25,7 +26,7 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class Session {
|
||||
public class Session implements Closeable {
|
||||
|
||||
private static final AtomicLong ATOMIC_LONG = new AtomicLong(0);
|
||||
|
||||
@@ -73,6 +74,11 @@ public class Session {
|
||||
return Objects.hash(sid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
channel.close();
|
||||
}
|
||||
|
||||
public long getSid() {
|
||||
return sid;
|
||||
}
|
||||
@@ -98,7 +104,4 @@ public class Session {
|
||||
return channel;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
channel.close();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user