package com.zy.core; import com.zy.common.utils.News; import com.zy.core.cache.MessageQueue; import com.zy.core.cache.SlaveConnection; import com.zy.core.enums.SlaveType; import com.zy.core.model.CrnSlave; import com.zy.core.model.DevpSlave; import com.zy.core.model.LedSlave; import com.zy.core.properties.SlaveProperties; import com.zy.core.thread.*; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * Created by vincent on 2020/8/4 */ @Slf4j @Component public class ServerBootstrap { @Autowired private SlaveProperties slaveProperties; @Autowired private MainProcess mainProcess; int CRN_COUNT = 0; public static final Map map = new ConcurrentHashMap<>(); /** * PostConstruct会在加载servlet的时候运行一次 * @throws InterruptedException */ @PostConstruct @Async public void init() throws InterruptedException { News.info("核心控制层开始初始化..............................................."); Thread.sleep(2000); // 初始化消息队列 initMq(); // 初始化下位机线程 initThread(); // 开始主流程进程 mainProcess.start(); News.info("核心控制层已启动..............................................."); } private void initMq(){ // 初始化堆垛机mq for (Slave crn : slaveProperties.getCrn()) { MessageQueue.init(SlaveType.Crn, crn); } // 初始化输送线mq for (Slave devp : slaveProperties.getDevp()) { MessageQueue.init(SlaveType.Devp, devp); } // 初始化条码扫描仪mq for (Slave barcode : slaveProperties.getBarcode()) { MessageQueue.init(SlaveType.Barcode, barcode); } // 初始化Led灯mq for (Slave led : slaveProperties.getLed()) { MessageQueue.init(SlaveType.Led, led); } // 初始化磅称mq for (Slave scale : slaveProperties.getScale()) { MessageQueue.init(SlaveType.Scale, scale); } // 初始化台车mq for (Slave car : slaveProperties.getCar()) { MessageQueue.init(SlaveType.Car, car); } } private void initThread() throws InterruptedException { // 初始化堆垛机线程 News.info("初始化堆垛机线程..................................................."); for (CrnSlave crn : slaveProperties.getCrn()) { CrnThread crnThread = new MelsecCrnThread(crn); Thread thread = new Thread((Runnable) crnThread); thread.start(); map.put(crnThread, thread); SlaveConnection.put(SlaveType.Crn, crn.getId(), crnThread); } // 初始化输送线线程 News.info("初始化输送线线程..................................................."); for (DevpSlave devp : slaveProperties.getDevp()) { DevpThread devpThread = new SiemensDevpThread(devp); new Thread((Runnable) devpThread).start(); SlaveConnection.put(SlaveType.Devp, devp.getId(), devpThread); } // 初始化条码扫描仪线程 News.info("初始化条码扫描仪线程..................................................."); for (Slave barcode : slaveProperties.getBarcode()) { BarcodeThread barcodeThread = new BarcodeThread(barcode); // new Thread(barcodeThread).start(); SlaveConnection.put(SlaveType.Barcode, barcode.getId(), barcodeThread); } // 初始化LED线程 News.info("初始化LED线程..................................................."); for (LedSlave led : slaveProperties.getLed()) { LedThread ledThread = new LedThread(led); new Thread(ledThread).start(); SlaveConnection.put(SlaveType.Led, led.getId(), ledThread); } // 初始化磅秤线程 // News.info("初始化磅秤线程..................................................."); // for (Slave scale : slaveProperties.getScale()) { // ScaleThread barcodeThread = new ScaleThread(scale); // new Thread(barcodeThread).start(); // SlaveConnection.put(SlaveType.Scale, scale.getId(), barcodeThread); // } } @PreDestroy public void destroy() { } @Component class killThread { @Scheduled(cron = "0/3 * * * * ? ") public void kill() { for (Map.Entry entry : map.entrySet()) { CRN_COUNT++; SiemensCrnThread key = (SiemensCrnThread) entry.getKey(); // System.out.println("第"+i+"个堆垛机线程重启线程条件:"+(System.currentTimeMillis() - key.getSign())); if (CRN_COUNT>10000){ continue; } if (System.currentTimeMillis() - key.getSign() > 120000 && System.currentTimeMillis() - key.getSign()<1200000) { System.out.println("第"+CRN_COUNT+"个堆垛机线程被重启"); // 请求线程安全停止 key.requestStop(); // 等待线程完成停止, 可以这样做也可以选择不这么做,取决于是否需要等待线程结束 try { entry.getValue().join(20000); // 等待最多20秒 } catch (InterruptedException e) { // // 可适当处理中断 // Thread.currentThread().interrupt(); // 保留中断状态 entry.getValue().stop(); } // 重新启动线程 Thread thread = new Thread((Runnable) key); // 假设CrnThread实现了Runnable thread.start(); // 替换映射关系中的旧线程 map.put(entry.getKey(), thread); } } } } }