From 664d9030efca22edd8e43b4db4b2c0700fff44af Mon Sep 17 00:00:00 2001
From: Junjie <DELL@qq.com>
Date: 星期三, 14 一月 2026 09:18:33 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/MainProcess.java |   45 ++++++++++++++++++++++++++++++++++-----------
 1 files changed, 34 insertions(+), 11 deletions(-)

diff --git a/src/main/java/com/zy/core/MainProcess.java b/src/main/java/com/zy/core/MainProcess.java
index be87702..70f7954 100644
--- a/src/main/java/com/zy/core/MainProcess.java
+++ b/src/main/java/com/zy/core/MainProcess.java
@@ -1,7 +1,6 @@
 package com.zy.core;
 
 import com.core.common.SpringUtils;
-import com.core.exception.CoolException;
 import com.zy.core.plugin.api.MainProcessPluginApi;
 import com.zy.core.properties.SystemProperties;
 import lombok.extern.slf4j.Slf4j;
@@ -20,38 +19,62 @@
     private String mainProcessPlugin;
     private MainProcessPluginApi mainProcessPluginApi;
     // 鎵�灞炵嚎绋�
-    private Thread thread;
+    private volatile Thread thread;
 
     /**
      * =====>>  寮�濮嬪伐浣�
      */
-    public void start(){
+    public void start() {
+        if (thread != null && thread.isAlive()) {
+            log.warn("MainProcess is already running.");
+            return;
+        }
+
         thread = new Thread(() -> {
             while (!Thread.currentThread().isInterrupted()) {
                 try {
+                    // 1. 鍔犺浇鎻掍欢
                     if (mainProcessPluginApi == null) {
-                        String className = mainProcessPlugin.contains(".") ? mainProcessPlugin : "com.zy.core.plugin." + mainProcessPlugin;
-                        Class<? extends MainProcessPluginApi> clazz = Class.forName(className).asSubclass(MainProcessPluginApi.class);
                         try {
+                            String className = mainProcessPlugin.contains(".") ? mainProcessPlugin : "com.zy.core.plugin." + mainProcessPlugin;
+                            Class<? extends MainProcessPluginApi> clazz = Class.forName(className).asSubclass(MainProcessPluginApi.class);
                             mainProcessPluginApi = SpringUtils.getBean(clazz);
-                        } catch (CoolException coolException) {
-                            Thread.sleep(300);
+                        } catch (Exception e) {
+                            log.error("Failed to load mainProcessPlugin: {}", mainProcessPlugin, e);
                         }
                     }
 
-                    // 绯荤粺杩愯鐘舵�佸垽鏂�
-                    if (!SystemProperties.WCS_RUNNING_STATUS.get()) {
+                    // 濡傛灉鍔犺浇澶辫触锛岀瓑寰呭悗閲嶈瘯锛岄槻姝㈢┖鎸囬拡
+                    if (mainProcessPluginApi == null) {
+                        Thread.sleep(3000);
                         continue;
                     }
 
+                    // 2. 绯荤粺杩愯鐘舵�佸垽鏂�
+                    if (!SystemProperties.WCS_RUNNING_STATUS.get()) {
+                        // 闃叉蹇欑瓑 (Busy-wait optimization)
+                        Thread.sleep(1000);
+                        continue;
+                    }
+
+                    // 3. 鎵ц涓绘祦绋�
                     mainProcessPluginApi.run();
-                    // 闂撮殧
+
+                    // 4. 闂撮殧
                     Thread.sleep(200);
+
                 } catch (InterruptedException ie) {
+                    log.info("MainProcess thread interrupted, stopping...");
                     Thread.currentThread().interrupt();
                     break;
                 } catch (Exception e) {
-                    e.printStackTrace();
+                    log.error("Error in MainProcess execution loop", e);
+                    try {
+                        // 閬垮厤寮傚父瀵艰嚧鐨勭媯鍒锋棩蹇�
+                        Thread.sleep(1000);
+                    } catch (InterruptedException ex) {
+                        Thread.currentThread().interrupt();
+                    }
                 }
             }
         });

--
Gitblit v1.9.1