eight
2024-09-04 680d7e06f93253f28edf9a9326e880e07fd01782
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package cn.lihu.jh.module.ecg.service.config;
 
import cn.lihu.jh.module.ecg.Utils;
import cn.lihu.jh.module.ecg.config.DynamicSchedulingConfig;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.config.ScheduledTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import javax.annotation.Resource;
import java.time.LocalTime;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
 
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;
 
@Service
@Validated
@Slf4j
public class EcgConfigServiceImpl implements EcgConfigService{
 
    @Resource
    private ConfigApi configApi;
 
    @Resource
    private RoomService roomService;
 
    @Resource
    private QueueService queueService;
 
    ScheduledTask startBizTask = null;
    ScheduledTask closeBizTask = null;
    ScheduledTask resetRoomTask = null;
 
    @Override
    public void resetScheduler() {
        ScheduledTaskRegistrar taskRegistrar = DynamicSchedulingConfig.static_scheduledTaskRegistrar;
 
        if (null != startBizTask) startBizTask.cancel();
        if (null != closeBizTask) closeBizTask.cancel();
        if (null != resetRoomTask) resetRoomTask.cancel();
 
        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());
 
        startBizTask = taskRegistrar.scheduleCronTask(new CronTask(() -> {
            System.out.println("Opening Task executed at: " + System.currentTimeMillis());
            queueService.startBiz();
        }, openCronExpression));
 
        closeBizTask = taskRegistrar.scheduleCronTask(new CronTask(() -> {
            System.out.println("Close Task executed at: " + System.currentTimeMillis());
            queueService.closeBiz();
        }, closeCronExpression));
 
        resetRoomTask = taskRegistrar.scheduleCronTask(new CronTask(() -> {
            System.out.println("Room Reset Task executed at: " + System.currentTimeMillis());
            roomService.resetRoom();
        }, roomResetCronExpression));
 
        taskRegistrar.afterPropertiesSet();
    }
 
}