1
zhang
2026-01-24 6f2e70d03c9dde5cdb26b8911d6c19e2993a38ab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.zy.core;
 
import com.zy.core.properties.CtuOperationConfig;
import com.zy.core.properties.CtuServiceProperties;
import com.zy.core.properties.SystemProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
 
@Slf4j
@Component
public class ConveyorBackgroundService {
 
    @Autowired
    private ApplicationContext applicationContext;
 
    @Autowired
    private ConveyorOperationExecutor operationExecutor;
 
    @Autowired
    private CtuServiceProperties properties;
 
    private ScheduledExecutorService executorService;
 
    private final AtomicBoolean running = new AtomicBoolean(false);
 
    @PostConstruct
    public void init() {
        // 初始化线程池
        executorService = Executors.newSingleThreadScheduledExecutor(r -> {
            Thread thread = new Thread(r);
            thread.setName("cv-background-thread");
            thread.setDaemon(true);
            return thread;
        });
 
        // 启动定时任务
        executorService.scheduleAtFixedRate(this::processConveyorTasks,
                properties.getInitialDelay(),
                properties.getInterval(),
                TimeUnit.MILLISECONDS);
        running.set(true);
        log.info("输送线后台服务线程初始化完成");
    }
 
    private void processConveyorTasks() {
        // 系统运行状态判断
        if (!SystemProperties.WCS_RUNNING_STATUS.get() || !running.get()) {
            log.debug("系统未运行或服务已停止,跳过输送线任务处理");
            return;
        }
 
        try {
            // 执行配置的操作序列
            for (CtuOperationConfig config : properties.getOperations()) {
                operationExecutor.execute(config);
            }
 
            //log.info("输送线任务处理完成");
        } catch (Exception e) {
            log.error("输送线任务处理异常", e);
        }
    }
 
    @PreDestroy
    public void shutDown() {
        running.set(false);
        if (executorService != null && !executorService.isShutdown()) {
            log.info("正在关闭输送线后台服务线程...");
            executorService.shutdown();
            try {
                if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) {
                    executorService.shutdownNow();
                }
            } catch (InterruptedException e) {
                executorService.shutdownNow();
                Thread.currentThread().interrupt();
            }
            log.info("输送线后台服务线程已关闭");
        }
    }
}