#
Junjie
昨天 6aaa4bd87948dc09acbde1b647e62f1cc67a4bcb
src/main/java/com/zy/core/plugin/GslProcess.java
@@ -22,21 +22,29 @@
import com.zy.core.plugin.store.StoreInTaskContext;
import com.zy.core.plugin.store.StoreInTaskGenerationService;
import com.zy.core.plugin.store.StoreInTaskPolicy;
import com.zy.core.properties.SystemProperties;
import com.zy.core.thread.StationThread;
import com.zy.core.utils.CrnOperateProcessUtils;
import com.zy.core.utils.StationOperateProcessUtils;
import com.zy.system.entity.Config;
import jakarta.annotation.PreDestroy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@Slf4j
@Component
public class GslProcess implements MainProcessPluginApi, StoreInTaskPolicy {
    private static final long STATION_DISPATCH_INTERVAL_MS = 200L;
    private static final long STATION_MAINTENANCE_INTERVAL_MS = 500L;
    private static final long STATION_TASK_SLOW_LOG_THRESHOLD_MS = 1000L;
    private static final long STATION_EXECUTOR_SHUTDOWN_TIMEOUT_SECONDS = 5L;
    @Autowired
    private CrnOperateProcessUtils crnOperateUtils;
@@ -53,52 +61,33 @@
    @Autowired
    private StationCommandDispatcher stationCommandDispatcher;
    private Thread crnRunThread = null;
    private final ExecutorService stationTaskExecutor = Executors.newSingleThreadExecutor(r -> {
        Thread thread = new Thread(r);
        thread.setName("StationTask");
        thread.setDaemon(true);
        return thread;
    });
    private final Map<String, AtomicBoolean> stationTaskRunningMap = new ConcurrentHashMap<>();
    private final Map<String, Long> stationTaskLastSubmitTimeMap = new ConcurrentHashMap<>();
    @Override
    public void run() {
        if (crnRunThread == null) {
            crnRunThread = new Thread(() -> {
                while (!Thread.currentThread().isInterrupted()) {
                    try {
                        //执行堆垛机任务
                        crnOperateUtils.crnIoExecute();
                        //堆垛机任务执行完成
                        crnOperateUtils.crnIoExecuteFinish();
                        // 间隔
                        Thread.sleep(50);
                    } catch (InterruptedException ie) {
                        Thread.currentThread().interrupt();
                        crnRunThread = null;
                        break;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            crnRunThread.setName("crnRunProcess");
            crnRunThread.setDaemon(true);
            crnRunThread.start();
        }
        //检测入库站是否有任务生成,并启动入库
        checkInStationHasTask();
        //请求生成入库任务
        generateStoreWrkFile();
        //执行输送站点入库任务
        stationOperateProcessUtils.stationInExecute();
        //执行输送站点出库任务
        stationOperateProcessUtils.crnStationOutExecute();
        // 检测出库排序
        stationOperateProcessUtils.checkStationOutOrder();
        // 监控绕圈站点
        stationOperateProcessUtils.watchCircleStation();
        //检测输送站点是否运行堵塞
        stationOperateProcessUtils.checkStationRunBlock();
        //检测输送站点任务停留超时后重新计算路径
        stationOperateProcessUtils.checkStationIdleRecover();
        //执行堆垛机任务
        crnOperateUtils.crnIoExecute();
        //堆垛机任务执行完成
        crnOperateUtils.crnIoExecuteFinish();
        //输送站点逻辑切到后台串行执行,避免阻塞主流程里的堆垛机发任务
        submitStationTask("stationInExecute", STATION_DISPATCH_INTERVAL_MS, stationOperateProcessUtils::stationInExecute);
        submitStationTask("crnStationOutExecute", STATION_DISPATCH_INTERVAL_MS, stationOperateProcessUtils::crnStationOutExecute);
        submitStationTask("checkStationOutOrder", STATION_MAINTENANCE_INTERVAL_MS, stationOperateProcessUtils::checkStationOutOrder);
        submitStationTask("watchCircleStation", STATION_MAINTENANCE_INTERVAL_MS, stationOperateProcessUtils::watchCircleStation);
        submitStationTask("checkStationRunBlock", STATION_MAINTENANCE_INTERVAL_MS, stationOperateProcessUtils::checkStationRunBlock);
        submitStationTask("checkStationIdleRecover", STATION_MAINTENANCE_INTERVAL_MS, stationOperateProcessUtils::checkStationIdleRecover);
    }
    /**
@@ -196,4 +185,51 @@
        }
    }
    private void submitStationTask(String taskName, long minIntervalMs, Runnable task) {
        long now = System.currentTimeMillis();
        Long lastSubmitTime = stationTaskLastSubmitTimeMap.get(taskName);
        if (lastSubmitTime != null && now - lastSubmitTime < minIntervalMs) {
            return;
        }
        AtomicBoolean running = stationTaskRunningMap.computeIfAbsent(taskName, key -> new AtomicBoolean(false));
        if (!running.compareAndSet(false, true)) {
            return;
        }
        stationTaskLastSubmitTimeMap.put(taskName, now);
        try {
            stationTaskExecutor.execute(() -> {
                long startMs = System.currentTimeMillis();
                try {
                    task.run();
                } catch (Exception e) {
                    log.error("GslProcess station task {} execute error", taskName, e);
                } finally {
                    long costMs = System.currentTimeMillis() - startMs;
                    if (costMs > STATION_TASK_SLOW_LOG_THRESHOLD_MS) {
                        log.warn("GslProcess station task {} executed slowly, cost={}ms", taskName, costMs);
                    }
                    running.set(false);
                }
            });
        } catch (Exception e) {
            running.set(false);
            stationTaskLastSubmitTimeMap.remove(taskName);
            log.error("GslProcess station task {} submit error", taskName, e);
        }
    }
    @PreDestroy
    public void shutDown() {
        stationTaskExecutor.shutdownNow();
        try {
            if (!stationTaskExecutor.awaitTermination(STATION_EXECUTOR_SHUTDOWN_TIMEOUT_SECONDS, TimeUnit.SECONDS)) {
                log.warn("GslProcess station task executor did not terminate within {}s", STATION_EXECUTOR_SHUTDOWN_TIMEOUT_SECONDS);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}