fix: 注册 JVM ShutdownHook,修复 Vert.x 实例进程退出时不关闭的资源泄漏

Deploy.deployVerticle() 中创建的 Vert.x 实例是局部变量,进程退出时无法优雅关闭,
导致 Netty EventLoopGroup、JDBC 连接池、内部定时器等资源泄漏。
添加 ShutdownHook 在 JVM 关闭时调用 vertx.close() 级联释放所有资源。
This commit is contained in:
yukaidi
2026-05-28 23:04:54 +08:00
parent 2b9168e8df
commit 2e0127d609

View File

@@ -43,6 +43,7 @@ public final class Deploy {
private Handler<JsonObject> handle; private Handler<JsonObject> handle;
private Thread mainThread; private Thread mainThread;
private Vertx mainVertx;
public static Deploy instance() { public static Deploy instance() {
return INSTANCE; return INSTANCE;
@@ -137,6 +138,14 @@ public final class Deploy {
vertxOptions.getWorkerPoolSize()); vertxOptions.getWorkerPoolSize());
var vertx = Vertx.vertx(vertxOptions); var vertx = Vertx.vertx(vertxOptions);
VertxHolder.init(vertx); VertxHolder.init(vertx);
this.mainVertx = vertx;
// 注册 ShutdownHook确保进程退出时优雅关闭资源
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
LOGGER.info("JVM shutting down, closing Vert.x...");
vertx.close().onComplete(ar ->
LOGGER.info("Vert.x closed: {}", ar.succeeded() ? "success" : ar.cause().getMessage()));
}));
//配置保存在共享数据中 //配置保存在共享数据中
var sharedData = vertx.sharedData(); var sharedData = vertx.sharedData();
LocalMap<String, Object> localMap = sharedData.getLocalMap(LOCAL); LocalMap<String, Object> localMap = sharedData.getLocalMap(LOCAL);