|  |  | 
 |  |  | 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.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 java.util.concurrent.ScheduledExecutorService; | 
 |  |  | import java.util.concurrent.ScheduledThreadPoolExecutor; | 
 |  |  |  | 
 |  |  | @Configuration | 
 |  |  | @EnableScheduling | 
 |  |  | public class DynamicSchedulingConfig implements SchedulingConfigurer { | 
 |  |  |  | 
 |  |  |     @Resource | 
 |  |  |     private ConfigApi configApi; | 
 |  |  |  | 
 |  |  |     @Resource | 
 |  |  |     private QueueService queueService; | 
 |  |  |     public static ScheduledTaskRegistrar static_scheduledTaskRegistrar; | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { | 
 |  |  |         taskRegistrar.setScheduler(Executors.newScheduledThreadPool(1)); | 
 |  |  |         DynamicSchedulingConfig.static_scheduledTaskRegistrar = taskRegistrar; | 
 |  |  |  | 
 |  |  |         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()); | 
 |  |  |  | 
 |  |  |         taskRegistrar.addCronTask(() -> { | 
 |  |  |             System.out.println("Opening Task executed at: " + System.currentTimeMillis()); | 
 |  |  |             queueService.startBiz(); | 
 |  |  |         }, openCronExpression); | 
 |  |  |  | 
 |  |  |         taskRegistrar.addCronTask(() -> { | 
 |  |  |             System.out.println("Close Task executed at: " + System.currentTimeMillis()); | 
 |  |  |             queueService.closeBiz(); | 
 |  |  |         }, closeCronExpression); | 
 |  |  |         ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1); | 
 |  |  |         scheduledThreadPoolExecutor.setRemoveOnCancelPolicy(true); | 
 |  |  |         taskRegistrar.setScheduler(scheduledThreadPoolExecutor); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |