src/main/java/com/zy/Boot.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/zy/core/ServerBootstrap.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/zy/core/thread/MelsecCrnThread.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
src/main/java/com/zy/Boot.java
@@ -5,8 +5,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; @EnableAsync @EnableScheduling @SpringBootApplication public class Boot extends SpringBootServletInitializer { src/main/java/com/zy/core/ServerBootstrap.java
@@ -12,10 +12,16 @@ 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 @@ -28,6 +34,8 @@ private SlaveProperties slaveProperties; @Autowired private MainProcess mainProcess; public static final Map<CrnThread, Thread> map = new ConcurrentHashMap<>(); /** * PostConstruct会在加载servlet的时候运行一次 @@ -74,12 +82,14 @@ } } private void initThread(){ private void initThread() throws InterruptedException { // 初始化堆垛机线程 News.info("初始化堆垛机线程..................................................."); for (CrnSlave crn : slaveProperties.getCrn()) { CrnThread crnThread = new MelsecCrnThread(crn); new Thread((Runnable) crnThread).start(); Thread thread = new Thread((Runnable) crnThread); thread.start(); map.put(crnThread, thread); SlaveConnection.put(SlaveType.Crn, crn.getId(), crnThread); } // 初始化输送线线程 @@ -117,5 +127,42 @@ public void destroy() { } @Component class killThread { @Scheduled(cron = "0/3 * * * * ? ") public void kill() { int i = 0; for (Map.Entry<CrnThread, Thread> entry : map.entrySet()) { i++; MelsecCrnThread key = (MelsecCrnThread) entry.getKey(); System.out.println("第"+i+"个堆垛机线程重启线程条件:"+(System.currentTimeMillis() - key.getSign())); if (System.currentTimeMillis() - key.getSign() > 120000) { System.out.println("第"+i+"个堆垛机线程被重启"); // 请求线程安全停止 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); } } } } } src/main/java/com/zy/core/thread/MelsecCrnThread.java
@@ -48,6 +48,10 @@ */ private boolean backHpFlag = false; public Long sign = System.currentTimeMillis(); public boolean isRunning = true; public MelsecCrnThread(CrnSlave slave) { this.slave = slave; } @@ -55,13 +59,15 @@ @Override @SuppressWarnings("InfiniteLoopStatement") public void run() { System.out.println("线程启动"); System.out.println("crnProtocol:"+crnProtocol); this.connect(); // try { // Thread.sleep(2000); // } catch (InterruptedException e) { // e.printStackTrace(); // } while (true) { while (isRunning) { try { int step = 1; Task task = MessageQueue.poll(SlaveType.Crn, slave.getId()); @@ -104,7 +110,8 @@ } catch (Exception e) { e.printStackTrace(); } System.out.println("第"+slave.getId()+"个堆垛机线程运行一轮:"+(System.currentTimeMillis()-sign)); sign = System.currentTimeMillis(); } } @@ -226,6 +233,8 @@ OutputQueue.CRN.offer(MessageFormat.format("【{0}】读取堆垛机plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); News.error("MelsecCrn"+" - 5"+" - 读取堆垛机plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); initCrn(); } finally { sign = System.currentTimeMillis(); } } @@ -473,4 +482,17 @@ } // 提供一个方法来停止线程 public void requestStop() { isRunning = false; } // 提供一个方法来重启线程 public Thread restartThread() { isRunning = true; Thread newThread = new Thread(this); newThread.start(); return newThread; } }