|
@@ -0,0 +1,72 @@
|
|
|
+package com.diagbot.task;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.diagbot.entity.SysTaskCron;
|
|
|
+import com.diagbot.enums.IsDeleteEnum;
|
|
|
+import com.diagbot.facade.BehospitalInfoFacade;
|
|
|
+import com.diagbot.facade.SysTaskCronFacade;
|
|
|
+import com.diagbot.util.StringUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.scheduling.Trigger;
|
|
|
+import org.springframework.scheduling.TriggerContext;
|
|
|
+import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
+import org.springframework.scheduling.annotation.SchedulingConfigurer;
|
|
|
+import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
|
|
+import org.springframework.scheduling.support.CronTrigger;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description:
|
|
|
+ * @author: gaodm
|
|
|
+ * @time: 2020/4/15 17:45
|
|
|
+ */
|
|
|
+@Configuration //1.主要用于标记配置类,兼备Component的效果。
|
|
|
+@EnableScheduling // 2.开启定时任务
|
|
|
+@Slf4j
|
|
|
+public class TASK_CX implements SchedulingConfigurer {
|
|
|
+ @Autowired
|
|
|
+ private SysTaskCronFacade sysTaskCronFacade;
|
|
|
+
|
|
|
+ private SysTaskCron task = new SysTaskCron();
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BehospitalInfoFacade behospitalInfoFacade;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行定时任务.
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
|
|
|
+ taskRegistrar.addTriggerTask(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ //1.添加任务内容(Runnable)
|
|
|
+ if (null != task
|
|
|
+ && task.getIsDeleted().equals(IsDeleteEnum.N.getKey())
|
|
|
+ && task.getIsUsed().equals(1)) {
|
|
|
+ log.info("执行动态定时任务: " + LocalDateTime.now().toLocalTime());
|
|
|
+ behospitalInfoFacade.execute2();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, new Trigger() {
|
|
|
+ @Override
|
|
|
+ public Date nextExecutionTime(TriggerContext triggerContext) {
|
|
|
+ //2.1 从数据库获取执行周期
|
|
|
+ task = sysTaskCronFacade.getOne(new QueryWrapper<SysTaskCron>()
|
|
|
+ .eq("cron_code", "TASK_CX")
|
|
|
+ );
|
|
|
+ String cron = "0/5 * * * * ?";
|
|
|
+ //2.2 合法性校验.
|
|
|
+ if (null != task && StringUtil.isNotBlank(task.getCron())) {
|
|
|
+ cron = task.getCron();
|
|
|
+ }
|
|
|
+ CronTrigger trigger = new CronTrigger(cron);
|
|
|
+ return trigger.nextExecutionTime(triggerContext);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|