|  |  |  | 
|---|
|  |  |  | import com.zy.acs.manager.system.service.ConfigService; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.boot.context.event.ApplicationReadyEvent; | 
|---|
|  |  |  | import org.springframework.context.event.EventListener; | 
|---|
|  |  |  | import org.springframework.scheduling.annotation.Scheduled; | 
|---|
|  |  |  | import org.springframework.stereotype.Component; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.PreDestroy; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Collections; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | 
|---|
|  |  |  | @Component | 
|---|
|  |  |  | public class KernelScheduler { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static int CORE_SCAN_FREQUENCY_MILLISECOND = 15; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | 
|---|
|  |  |  | @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; } | 
|---|
|  |  |  | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | 
|---|
|  |  |  | 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) { | 
|---|
|  |  |  | trafficService.trigger(segment); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //    @PostConstruct | 
|---|
|  |  |  | @EventListener(ApplicationReadyEvent.class) | 
|---|
|  |  |  | public void init() { | 
|---|
|  |  |  | try { Thread.sleep(1200); } catch (InterruptedException ignore) {} | 
|---|
|  |  |  | // traffic calculate | 
|---|
|  |  |  | this.trafficCalcThread = new Thread(() -> { | 
|---|
|  |  |  | while (!Thread.currentThread().isInterrupted()) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Thread.sleep(CORE_SCAN_FREQUENCY_MILLISECOND); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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(CORE_SCAN_FREQUENCY_MILLISECOND); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<String> actionGroupIds = actionService.selectGroupNo(ActionStsType.PREPARE); | 
|---|
|  |  |  | for (String actionGroupId : actionGroupIds) { | 
|---|
|  |  |  | mainService.publishAction(actionGroupId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } 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) { | 
|---|
|  |  |  | mainService.publishAction(actionGroupId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //    @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 ---------------------------------------------------------------------------------------------- | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PreDestroy | 
|---|
|  |  |  | public void shutDown(){ | 
|---|
|  |  |  | if (this.trafficCalcThread != null) { | 
|---|
|  |  |  | this.trafficCalcThread .interrupt(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (this.actionPublicThread != null) { | 
|---|
|  |  |  | this.actionPublicThread .interrupt(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|