fix: JsParserExecutor 和 JsHttpClient 添加资源清理,修复解析完成后资源泄漏

- JsHttpClient 添加 close() 方法释放 WebClient 连接池
- JsParserExecutor 添加 close() 方法,清除 ScriptEngine 中注入的 Java 对象引用
- parse()/parseFileList()/parseById() 均在 onComplete 回调中调用 close() 释放资源
This commit is contained in:
yukaidi
2026-05-28 23:13:09 +08:00
parent 7419e536cf
commit 255e7b2fb5
2 changed files with 29 additions and 4 deletions

View File

@@ -61,7 +61,7 @@ public class JsHttpClient {
}; };
public JsHttpClient() { public JsHttpClient() {
this.client = WebClient.create(WebClientVertxInit.get(), new WebClientOptions());; this.client = WebClient.create(WebClientVertxInit.get(), new WebClientOptions());
this.clientSession = WebClientSession.create(client); this.clientSession = WebClientSession.create(client);
this.headers = MultiMap.caseInsensitiveMultiMap(); this.headers = MultiMap.caseInsensitiveMultiMap();
// 设置默认的Accept-Encoding头以支持压缩响应 // 设置默认的Accept-Encoding头以支持压缩响应
@@ -677,4 +677,13 @@ public class JsHttpClient {
return buffer.length(); return buffer.length();
} }
} }
/**
* 关闭 WebClient 释放连接池资源
*/
public void close() {
if (client != null) {
client.close();
}
}
} }

View File

@@ -146,6 +146,22 @@ public class JsParserExecutor implements IPanTool {
} }
} }
/**
* 释放资源ScriptEngine 和 HttpClient避免内存泄漏
*/
public void close() {
if (httpClient != null) {
httpClient.close();
}
// 清除 ScriptEngine 持有的 Java 对象引用,帮助 GC 回收
if (engine != null) {
engine.put("http", null);
engine.put("logger", null);
engine.put("shareLinkInfo", null);
engine.put("JavaFetch", null);
}
}
@Override @Override
public Future<String> parse() { public Future<String> parse() {
jsLogger.info("开始执行JavaScript解析器: {}", config.getType()); jsLogger.info("开始执行JavaScript解析器: {}", config.getType());
@@ -173,7 +189,7 @@ public class JsParserExecutor implements IPanTool {
} else { } else {
throw new RuntimeException("parse函数类型错误"); throw new RuntimeException("parse函数类型错误");
} }
}); }).onComplete(ar -> close());
} }
@Override @Override
@@ -206,7 +222,7 @@ public class JsParserExecutor implements IPanTool {
} else { } else {
throw new RuntimeException("parseFileList函数类型错误"); throw new RuntimeException("parseFileList函数类型错误");
} }
}); }).onComplete(ar -> close());
} }
@Override @Override
@@ -237,7 +253,7 @@ public class JsParserExecutor implements IPanTool {
} else { } else {
throw new RuntimeException("parseById函数类型错误"); throw new RuntimeException("parseById函数类型错误");
} }
}); }).onComplete(ar -> close());
} }
/** /**