refactor(agent): support register registry shutdown hooks while agent launching

This commit is contained in:
2026-04-09 12:19:20 +08:00
parent 328befecca
commit d6593c10f9
4 changed files with 53 additions and 28 deletions

View File

@@ -46,7 +46,6 @@ public class WebSocketGateway extends WebSocketServer implements AgentGateway<In
return;
}
this.start();
setShutDownHook();
startHeartbeatThread();
AgentRuntime.INSTANCE.registerResponseChannel(getChannelName(), this);
}
@@ -106,32 +105,6 @@ public class WebSocketGateway extends WebSocketServer implements AgentGateway<In
log.debug("Received Pong from {}", conn.getRemoteSocketAddress());
}
private void setShutDownHook() {
try {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
// 先断开所有客户端连接
for (WebSocket webSocket : getConnections()) {
try {
webSocket.close(1001, "Server shutting down");
} catch (Exception e) {
log.warn("关闭客户端连接时出错: ", e);
}
}
//关闭WebSocketServer给10秒超时时间确保连接正确关闭
this.stop(10000);
log.info("WebSocketServer 已关闭");
} catch (IllegalStateException e) {
log.warn("无法添加关闭钩子JVM可能已在关闭过程中: ", e);
} catch (Exception e) {
log.error("WebSocketServer关闭失败: ", e);
}
}));
} catch (IllegalStateException e) {
log.warn("无法添加关闭钩子JVM可能已在关闭过程中: ", e);
}
}
@Override
public void onOpen(WebSocket webSocket, ClientHandshake clientHandshake) {
log.info("新连接: {}", webSocket.getRemoteSocketAddress());