mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2026-06-10 15:37:28 +00:00
fix: ShutdownHook 改为同步等待 vertx.close(),修复 JVM 提前退出导致资源未释放
审查发现 vertx.close() 是异步操作,ShutdownHook 线程提交关闭任务后立即退出, JVM 在资源实际释放前就终止了,与未修复时行为等价。 改为 CompletableFuture.get(10s) 阻塞等待,超时有 warn 日志。 同时移除无用的 mainVertx 字段,修正 JsExecUtils 误导性注释。
This commit is contained in:
@@ -43,7 +43,6 @@ 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;
|
||||||
@@ -138,13 +137,16 @@ 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,确保进程退出时优雅关闭资源
|
// 注册 ShutdownHook,确保进程退出时优雅关闭资源
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||||
LOGGER.info("JVM shutting down, closing Vert.x...");
|
LOGGER.info("JVM shutting down, closing Vert.x...");
|
||||||
vertx.close().onComplete(ar ->
|
try {
|
||||||
LOGGER.info("Vert.x closed: {}", ar.succeeded() ? "success" : ar.cause().getMessage()));
|
vertx.close().toCompletionStage().toCompletableFuture().get(10, java.util.concurrent.TimeUnit.SECONDS);
|
||||||
|
LOGGER.info("Vert.x closed successfully");
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.warn("Vert.x close error or timeout", e);
|
||||||
|
}
|
||||||
}));
|
}));
|
||||||
//配置保存在共享数据中
|
//配置保存在共享数据中
|
||||||
var sharedData = vertx.sharedData();
|
var sharedData = vertx.sharedData();
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ public class JsExecUtils {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 调用执行js文件(复用已缓存的引擎实例,避免每次创建)
|
* 调用执行js文件(使用缓存的 ScriptEngineManager 创建新引擎实例)
|
||||||
*/
|
*/
|
||||||
public static Object executeOtherJs(String jsText, String funName, Object ... args) throws ScriptException,
|
public static Object executeOtherJs(String jsText, String funName, Object ... args) throws ScriptException,
|
||||||
NoSuchMethodException {
|
NoSuchMethodException {
|
||||||
|
|||||||
Reference in New Issue
Block a user