#
Junjie
2026-01-15 34b2641f4a039d4e1dcf8f4a93dd7928616c4b11
src/main/java/com/zy/core/MainProcess.java
@@ -1,56 +1,75 @@
package com.zy.core;
import com.zy.asrs.service.impl.MainServiceImpl;
import com.core.common.SpringUtils;
import com.zy.core.plugin.api.MainProcessPluginApi;
import com.zy.core.properties.SystemProperties;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PreDestroy;
/**
 * WCS主流程
 * Created by vincent on 2020/8/6
 */
@Data
@Slf4j
@Component
public class MainProcess {
    @Autowired
    private MainServiceImpl mainService;
    @Value("${mainProcessPlugin}")
    private String mainProcessPlugin;
    private MainProcessPluginApi mainProcessPluginApi;
    // 所属线程
    private Thread thread;
    // 频率
    private int i = 0;
    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) {
                        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 (Exception e) {
                            log.error("Failed to load mainProcessPlugin: {}", mainProcessPlugin, e);
                        }
                    }
                    // 间隔
                    Thread.sleep(300);
                    // 系统运行状态判断
                    if (!SystemProperties.WCS_RUNNING_STATUS.get()) {
                    // 如果加载失败,等待后重试,防止空指针
                    if (mainProcessPluginApi == null) {
                        Thread.sleep(3000);
                        continue;
                    }
                    // 初始化库位地图数据结构
                    mainService.initLocMap();
                    // 2. 系统运行状态判断
                    if (!SystemProperties.WCS_RUNNING_STATUS.get()) {
                        // 防止忙等 (Busy-wait optimization)
                        Thread.sleep(1000);
                        continue;
                    }
                    // 间隔
                    // 3. 执行主流程
                    mainProcessPluginApi.run();
                    // 4. 间隔
                    Thread.sleep(200);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("Error in MainProcess execution loop", e);
                }
            }
        });
        thread.setName("MainProcess");
        thread.setDaemon(true);
        thread.start();
    }