src/main/java/com/zy/Boot.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/zy/core/ServerBootstrap.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/zy/core/thread/SiemensCrnThread.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,13 @@ 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.Map; import java.util.concurrent.ConcurrentHashMap; /** * Created by vincent on 2020/8/4 @@ -28,6 +31,9 @@ private SlaveProperties slaveProperties; @Autowired private MainProcess mainProcess; public static final Map<CrnThread, Thread> map = new ConcurrentHashMap<>(); /** * PostConstruct会在加载servlet的时候运行一次 @@ -129,5 +135,41 @@ 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++; SiemensCrnThread key = (SiemensCrnThread) 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/SiemensCrnThread.java
@@ -39,6 +39,10 @@ private CrnProtocol crnProtocol; private boolean resetFlag = false; public Long sign = System.currentTimeMillis(); public boolean isRunning = true; /** * 堆垛机是否在回原点运动中标记 */ @@ -52,7 +56,7 @@ @SuppressWarnings("InfiniteLoopStatement") public void run() { this.connect(); while (true) { while (isRunning) { try { int step = 1; Task task = MessageQueue.poll(SlaveType.Crn, slave.getId()); @@ -93,7 +97,8 @@ } catch (Exception e) { // e.printStackTrace(); } // System.out.println("第"+slave.getId()+"个堆垛机线程运行一轮:"+(System.currentTimeMillis()-sign)); sign = System.currentTimeMillis(); } } @@ -415,4 +420,9 @@ } // 提供一个方法来停止线程 public void requestStop() { isRunning = false; } }