From c7c5da5943d8ffa0d4bc1c275a945487573bd275 Mon Sep 17 00:00:00 2001
From: eight <641137800@qq.com>
Date: 星期四, 29 八月 2024 21:34:32 +0800
Subject: [PATCH] update
---
jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/config/DynamicSchedulingConfig.java | 40 ++++------------------------------------
jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/service/queue/queueServiceImpl.java | 18 +++++++-----------
jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/config/MySpringEventListener.java | 2 ++
3 files changed, 13 insertions(+), 47 deletions(-)
diff --git a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/config/DynamicSchedulingConfig.java b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/config/DynamicSchedulingConfig.java
index 1eccc87..9babe6a 100644
--- a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/config/DynamicSchedulingConfig.java
+++ b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/config/DynamicSchedulingConfig.java
@@ -1,28 +1,21 @@
package cn.lihu.jh.module.ecg.config;
-import cn.lihu.jh.module.ecg.Utils;
import cn.lihu.jh.module.ecg.service.queue.QueueService;
import cn.lihu.jh.module.ecg.service.room.RoomService;
import cn.lihu.jh.module.infra.api.config.ConfigApi;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
-import org.springframework.scheduling.config.CronTask;
-import org.springframework.scheduling.config.ScheduledTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import javax.annotation.Resource;
-import java.time.LocalTime;
-import java.time.format.DateTimeFormatter;
-import java.util.List;
import java.util.concurrent.Executors;
-
-import static cn.lihu.jh.module.ecg.Constants.ECG_OPENING_TIME_KEY;
-import static cn.lihu.jh.module.ecg.Constants.ECG_ROOM_RESET_TIME_KEY;
@Configuration
@EnableScheduling
public class DynamicSchedulingConfig implements SchedulingConfigurer {
+
+ public static ScheduledTaskRegistrar static_scheduledTaskRegistrar;
@Resource
private ConfigApi configApi;
@@ -35,33 +28,8 @@
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
+ DynamicSchedulingConfig.static_scheduledTaskRegistrar = taskRegistrar;
+
taskRegistrar.setScheduler(Executors.newScheduledThreadPool(1));
-
- String strOpenCloseTime = configApi.getConfigValueByKey(ECG_OPENING_TIME_KEY);
- List<LocalTime> list = Utils.parseOpeningTime(strOpenCloseTime);
- LocalTime openingTime = list.get(0);
- LocalTime closeTime = list.get(1);
- String openCronExpression = String.format("0 %d %d * * ?", openingTime.getMinute(), openingTime.getHour());
- String closeCronExpression = String.format("0 %d %d * * ?", closeTime.getMinute(), closeTime.getHour());
-
- String strRoomResetTime = configApi.getConfigValueByKey(ECG_ROOM_RESET_TIME_KEY);
- LocalTime roomResetTime = Utils.parseTime(strRoomResetTime);
- String roomResetCronExpression = String.format("0 %d %d * * ?", roomResetTime.getMinute(), roomResetTime.getHour());
-
- taskRegistrar.scheduleCronTask(new CronTask(() -> {
- System.out.println("Opening Task executed at: " + System.currentTimeMillis());
- queueService.startBiz();
- }, openCronExpression));
-
- taskRegistrar.scheduleCronTask(new CronTask(() -> {
- System.out.println("Close Task executed at: " + System.currentTimeMillis());
- queueService.closeBiz();
- }, closeCronExpression));
-
- taskRegistrar.scheduleCronTask(new CronTask(() -> {
- System.out.println("Room Reset Task executed at: " + System.currentTimeMillis());
- roomService.resetRoom();
- }, roomResetCronExpression));
}
-
}
diff --git a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/config/MySpringEventListener.java b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/config/MySpringEventListener.java
index a97f08e..886aeaf 100644
--- a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/config/MySpringEventListener.java
+++ b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/config/MySpringEventListener.java
@@ -44,5 +44,7 @@
} else {
queueService.startBiz();
}
+
+ queueService.resetScheduler();
}
}
diff --git a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/service/queue/queueServiceImpl.java b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/service/queue/queueServiceImpl.java
index ef629f5..5b7e2ac 100644
--- a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/service/queue/queueServiceImpl.java
+++ b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/service/queue/queueServiceImpl.java
@@ -7,6 +7,7 @@
import javax.annotation.Resource;
import cn.lihu.jh.module.ecg.Utils;
+import cn.lihu.jh.module.ecg.config.DynamicSchedulingConfig;
import cn.lihu.jh.module.ecg.dal.dataobject.room.RoomDO;
import cn.lihu.jh.module.ecg.service.room.RoomService;
import org.springframework.scheduling.config.CronTask;
@@ -51,14 +52,7 @@
private ConfigApi configApi;
@Resource
- private ScheduledTaskRegistrar taskRegistrar;
-
- @Resource
- private QueueService queueService;
-
- @Resource
private RoomService roomService;
-
@Resource
private queueMapper queueMapper;
@@ -582,8 +576,8 @@
@Override
public void resetScheduler() {
- Set<ScheduledTask> taskList = taskRegistrar.getScheduledTasks();
- taskList.forEach((task)->task.cancel());
+ ScheduledTaskRegistrar taskRegistrar = DynamicSchedulingConfig.static_scheduledTaskRegistrar;
+ taskRegistrar.getScheduledTasks().forEach(ScheduledTask::cancel);
String strOpenCloseTime = configApi.getConfigValueByKey(ECG_OPENING_TIME_KEY);
List<LocalTime> list = Utils.parseOpeningTime(strOpenCloseTime);
@@ -598,18 +592,20 @@
taskRegistrar.scheduleCronTask(new CronTask(() -> {
System.out.println("Opening Task executed at: " + System.currentTimeMillis());
- queueService.startBiz();
+ startBiz();
}, openCronExpression));
taskRegistrar.scheduleCronTask(new CronTask(() -> {
System.out.println("Close Task executed at: " + System.currentTimeMillis());
- queueService.closeBiz();
+ closeBiz();
}, closeCronExpression));
taskRegistrar.scheduleCronTask(new CronTask(() -> {
System.out.println("Room Reset Task executed at: " + System.currentTimeMillis());
roomService.resetRoom();
}, roomResetCronExpression));
+
+ taskRegistrar.afterPropertiesSet();
}
@Override
--
Gitblit v1.9.3