From 12e789e034372eb892797df8c1dcaa194ed82ed2 Mon Sep 17 00:00:00 2001 From: SerLiunx-ctrl <17689543@qq.com> Date: Thu, 13 Jun 2024 14:06:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=8E=B0=E5=B7=B2=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E6=9C=9F=E9=97=B4=E5=88=B7=E6=96=B0=E5=AE=9E?= =?UTF-8?q?=E4=BE=8B=E4=B8=8A=E4=B8=8B=E6=96=87=E4=BF=A1=E6=81=AF.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ddns/support/SystemInitializer.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/serliunx/ddns/support/SystemInitializer.java b/src/main/java/com/serliunx/ddns/support/SystemInitializer.java index 03d58b1..d74bc42 100644 --- a/src/main/java/com/serliunx/ddns/support/SystemInitializer.java +++ b/src/main/java/com/serliunx/ddns/support/SystemInitializer.java @@ -19,6 +19,7 @@ import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -59,6 +60,9 @@ public final class SystemInitializer implements Refreshable, Clearable { public void refresh() { log.info("程序正在初始化, 请稍候."); + // 检查正在运行的实例信息, 安全的停止(手动刷新时需要执行的逻辑, 初始化不需要) + checkAndCloseSafely(); + // 释放配置文件 releaseResource(SystemConstants.CONFIG_PROPERTIES_FILE); @@ -88,7 +92,6 @@ public final class SystemInitializer implements Refreshable, Clearable { @Override public void clear() { instanceContext.clear(); - instances.clear(); } public MultipleSourceInstanceContext getInstanceContext() { @@ -104,6 +107,7 @@ public final class SystemInitializer implements Refreshable, Clearable { log.info("载入 {} 个实例.", instances.size()); } + @SuppressWarnings("SameParameterValue") private void releaseResource(String resourceName) { ClassLoader classLoader = SystemConstants.class.getClassLoader(); Path path = Paths.get(SystemConstants.USER_DIR + File.separator + resourceName); @@ -181,4 +185,25 @@ public final class SystemInitializer implements Refreshable, Clearable { }); } + + private void checkAndCloseSafely() { + if (scheduledThreadPoolExecutor == null) + return; + + scheduledThreadPoolExecutor.shutdown(); + try { + boolean result = scheduledThreadPoolExecutor.awaitTermination(5, TimeUnit.SECONDS); + if (result) { + log.error("线程池无法在正常的时间范围内关闭, 将强制关闭线程池!"); + if (!scheduledThreadPoolExecutor.isShutdown()) + scheduledThreadPoolExecutor.shutdownNow(); + } + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + instances.clear(); + + runningInstances.clear(); + } }