192.168.9.165 1 maand geleden
bovenliggende
commit
c3dc21d79f

+ 2 - 0
src/main/java/com/fdkankan/scene/config/FdkkLaserConfig.java

@@ -63,6 +63,7 @@ public class FdkkLaserConfig {
     public String buildCallPath;
     public String buildCallPath;
     public String settingJson;
     public String settingJson;
 
 
+    public Long pid;
     public Integer getLaserPort() {
     public Integer getLaserPort() {
         return laserPort;
         return laserPort;
     }
     }
@@ -81,6 +82,7 @@ public class FdkkLaserConfig {
             this.setProfile(config.getString("profilePath"));
             this.setProfile(config.getString("profilePath"));
             this.setBuildModelPath(config.getString("buildModelPath"));
             this.setBuildModelPath(config.getString("buildModelPath"));
             this.setBuildCallPath(config.getString("buildCallPath"));
             this.setBuildCallPath(config.getString("buildCallPath"));
+            this.setPid(config.getLong("pid"));
         }
         }
     }
     }
 
 

+ 98 - 0
src/main/java/com/fdkankan/scene/schedule/AppListener.java

@@ -0,0 +1,98 @@
+package com.fdkankan.scene.schedule;
+
+
+import cn.hutool.core.date.DateUnit;
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.system.SystemUtil;
+import com.fdkankan.scene.config.FdkkLaserConfig;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.SpringApplication;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+
+@Component
+@Slf4j
+public class AppListener {
+
+    @Autowired
+    FdkkLaserConfig fdkkLaserConfig;
+
+    private final ConfigurableApplicationContext context;
+
+    public AppListener(ConfigurableApplicationContext context) {
+        this.context = context;
+    }
+
+    @PostConstruct
+    public void init() {
+        if (fdkkLaserConfig.getPid() <= 0) {
+           log.info("未配置 app.monitorPid,跳过 PID 监听。");
+            return;
+        }
+
+        log.info("启动监听 PID: " + fdkkLaserConfig.getPid());
+
+        Thread monitorThread = new Thread(() -> {
+            while (true) {
+                try {
+                    if (!isProcessAlive(fdkkLaserConfig.getPid())) {
+                        System.out.println("目标 PID 不存在,准备退出 SpringBoot 服务...");
+                        shutdownApplication();
+                        break;
+                    }
+                    Thread.sleep(2000);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        });
+
+        monitorThread.setDaemon(true);
+        monitorThread.start();
+    }
+
+    /**
+     * 判断 PID 是否存活 (Java 8 版本,使用系统命令)
+     */
+    private boolean isProcessAlive(long pid) {
+        try {
+            if (SystemUtil.getOsInfo().isWindows()) {
+                // Windows 使用 tasklist
+                Process process = Runtime.getRuntime().exec("tasklist /FI \"PID eq " + pid + "\"");
+                try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
+                    String line;
+                    while ((line = reader.readLine()) != null) {
+                        if (line.contains(String.valueOf(pid))) {
+                            return true;
+                        }
+                    }
+                }
+            } else {
+                // Linux / Mac 使用 kill -0
+                Process process = Runtime.getRuntime().exec("kill -0 " + pid);
+                int exitCode = process.waitFor();
+                return exitCode == 0;
+            }
+        } catch (Exception e) {
+            return false;
+        }
+        return false;
+    }
+
+    /**
+     * 优雅关闭 SpringBoot,并退出 JVM
+     */
+    private void shutdownApplication() {
+        try {
+            SpringApplication.exit(context, () -> 0);
+        } finally {
+            System.exit(0);
+        }
+    }
+}

+ 1 - 1
src/main/resources/application-standAloneProd.yml

@@ -78,7 +78,7 @@ logging:
   fdkk:
   fdkk:
     maxHistory: 1
     maxHistory: 1
     level: Info
     level: Info
-  path: ${FDMGEA_HOME}/bin/log
+  path: ./log
 
 
 main:
 main:
   url: https://test.4dkankan.com
   url: https://test.4dkankan.com