From e5670f55e06ba0bc7a884b305738e7d9f678bc00 Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@163.com>
Date: 星期一, 23 十二月 2024 16:40:39 +0800
Subject: [PATCH] #

---
 zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/KernelScheduler.java |  105 +++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 83 insertions(+), 22 deletions(-)

diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/KernelScheduler.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/KernelScheduler.java
index 5e03f6d..7da812d 100644
--- a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/KernelScheduler.java
+++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/KernelScheduler.java
@@ -18,6 +18,8 @@
 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.Collections;
 import java.util.Date;
@@ -35,6 +37,10 @@
 
     private static final int LOCK_TIMEOUT = 5;
     private final ReentrantLock lock = new ReentrantLock(Boolean.TRUE);
+
+    private Thread trafficCalcThread;
+
+    private Thread actionPublicThread;
 
     @Autowired
     private AgvService agvService;
@@ -61,7 +67,7 @@
     @Autowired
     private SnowflakeIdWorker snowflakeIdWorker;
 
-    @Scheduled(cron = "0/3 * * * * ? ")
+    @Scheduled(cron = "0/1 * * * * ? ")
     private void startupBus() throws InterruptedException {
         if (!configService.getVal("TaskAssignMode", Boolean.class)) { return; }
         if (!this.lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS)) { return; }
@@ -72,7 +78,7 @@
         this.lock.unlock();
     }
 
-    @Scheduled(cron = "0/3 * * * * ? ")
+    @Scheduled(cron = "0/1 * * * * ? ")
     private void calculateSeg() throws InterruptedException {
         if (!this.lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS)) { return; }
         List<Task> taskList = taskService.selectBySts(TaskStsType.WAITING);
@@ -95,27 +101,74 @@
         this.lock.unlock();
     }
 
-    @Scheduled(cron = "0/1 * * * * ? ")
-    private void traffic() {
-        List<Segment> segments = segmentService.list(new LambdaQueryWrapper<Segment>()
-                .eq(Segment::getState, SegmentStateType.WAITING.toString())
-        );
-        for (Segment segment : segments) {
-            long startTime = System.currentTimeMillis();
-            trafficService.trigger(segment);
-            log.info("traffic calculation spend {} ms", System.currentTimeMillis() - startTime);
-        }
+    @PostConstruct
+    public void init() {
+        try { Thread.sleep(1200); } catch (InterruptedException ignore) {}
+        // traffic calculate
+        this.trafficCalcThread = new Thread(() -> {
+            while (!Thread.currentThread().isInterrupted()) {
+                try {
+                    Thread.sleep(100);
+
+                    List<Segment> segments = segmentService.list(new LambdaQueryWrapper<Segment>()
+                            .eq(Segment::getState, SegmentStateType.WAITING.toString())
+                    );
+                    for (Segment segment : segments) {
+                        long startTime = System.currentTimeMillis();
+                        trafficService.trigger(segment);
+                        log.info("traffic calculation spend {} ms", System.currentTimeMillis() - startTime);
+                    }
+
+                } catch (Exception e) {
+                    log.error("KernelScheduler.trafficCalcThread fail", e);
+                }
+            }
+        });
+        this.trafficCalcThread.start();
+        // public action
+        this.actionPublicThread = new Thread(() -> {
+            while (!Thread.currentThread().isInterrupted()) {
+                try {
+                    Thread.sleep(100);
+
+                    List<String> actionGroupIds = actionService.selectPrepareGroup();
+                    for (String actionGroupId : actionGroupIds) {
+                        long startTime = System.currentTimeMillis();
+                        mainService.publishAction(actionGroupId);
+                        log.info("publish action calculation spend {} ms", System.currentTimeMillis() - startTime);
+                    }
+
+                } catch (Exception e) {
+                    log.error("KernelScheduler.actionPublicThread fail", e);
+                }
+            }
+        });
+        this.actionPublicThread.start();
     }
 
-    @Scheduled(cron = "0/1 * * * * ? ")
-    private void publishAction(){
-        List<String> actionGroupIds = actionService.selectPrepareGroup();
-        for (String actionGroupId : actionGroupIds) {
-            long startTime = System.currentTimeMillis();
-            mainService.publishAction(actionGroupId);
-            log.info("publish action calculation spend {} ms", System.currentTimeMillis() - startTime);
-        }
-    }
+//    @Scheduled(fixedRate = 500) // 500姣鎵ц涓�娆★紝涓嶅悓姝�
+//    @Scheduled(fixedDelay = 500) // 500姣鎵ц涓�娆★紝鍚屾
+//    @Scheduled(cron = "0/1 * * * * ? ")
+//    private void traffic() {
+//        List<Segment> segments = segmentService.list(new LambdaQueryWrapper<Segment>()
+//                .eq(Segment::getState, SegmentStateType.WAITING.toString())
+//        );
+//        for (Segment segment : segments) {
+//            long startTime = System.currentTimeMillis();
+//            trafficService.trigger(segment);
+//            log.info("traffic calculation spend {} ms", System.currentTimeMillis() - startTime);
+//        }
+//    }
+//
+//    @Scheduled(cron = "0/1 * * * * ? ")
+//    private void publishAction(){
+//        List<String> actionGroupIds = actionService.selectPrepareGroup();
+//        for (String actionGroupId : actionGroupIds) {
+//            long startTime = System.currentTimeMillis();
+//            mainService.publishAction(actionGroupId);
+//            log.info("publish action calculation spend {} ms", System.currentTimeMillis() - startTime);
+//        }
+//    }
 
     // patch ----------------------------------------------------------------------------------------------
 
@@ -151,7 +204,15 @@
     }
 
 
-
+    @PreDestroy
+    public void shutDown(){
+        if (this.trafficCalcThread != null) {
+            this.trafficCalcThread .interrupt();
+        }
+        if (this.actionPublicThread != null) {
+            this.actionPublicThread .interrupt();
+        }
+    }
 
 
 

--
Gitblit v1.9.1