package com.zy.acs.conveyor.core.operation;
|
|
import com.zy.acs.common.utils.News;
|
import com.zy.acs.conveyor.core.properties.CtuOperationConfig;
|
import com.zy.acs.conveyor.core.properties.CtuServiceProperties;
|
import com.zy.acs.conveyor.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 long lastDbUpdateTime = 0;
|
|
|
private static final int LOG_INTERVAL_MS = 300000;
|
|
@PostConstruct
|
public void init() {
|
// 初始化线程池
|
executorService = Executors.newSingleThreadScheduledExecutor(r -> {
|
Thread thread = new Thread(r);
|
thread.setName("cv-background-thread");
|
//thread.setDaemon(true);
|
return thread;
|
});
|
|
// 启动定时任务
|
executorService.scheduleWithFixedDelay(this::processConveyorTasks,
|
properties.getInitialDelay(),
|
properties.getInterval(),
|
TimeUnit.MILLISECONDS);
|
News.info("输送线后台服务线程初始化完成");
|
}
|
|
private void processConveyorTasks() {
|
// 系统运行状态判断
|
if (!SystemProperties.WCS_RUNNING_STATUS.get()) {
|
News.info("系统未运行或服务已停止,跳过输送线任务处理");
|
return;
|
}
|
long currentTime = System.currentTimeMillis();
|
if (currentTime - lastDbUpdateTime > LOG_INTERVAL_MS) {
|
News.info("执行输送线操作");
|
lastDbUpdateTime = currentTime;
|
}
|
|
// 执行配置的操作序列
|
for (CtuOperationConfig config : properties.getOperations()) {
|
try {
|
operationExecutor.execute(config);
|
} catch (Exception e) {
|
News.error("输送线操作执行最终失败, type={}, error={}", config.getType(), e.getMessage());
|
// 可选:发送告警、暂停调度等
|
}
|
}
|
|
}
|
|
@PreDestroy
|
public void shutDown() {
|
if (executorService != null && !executorService.isShutdown()) {
|
News.info("正在关闭输送线后台服务线程...");
|
executorService.shutdown();
|
try {
|
if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) {
|
executorService.shutdownNow();
|
}
|
} catch (InterruptedException e) {
|
executorService.shutdownNow();
|
Thread.currentThread().interrupt();
|
}
|
News.info("输送线后台服务线程已关闭");
|
}
|
}
|
}
|