Xiaojh 4 tahun lalu
induk
melakukan
40ae8eb78a

+ 32 - 0
smart-city-platform/src/main/java/com/bz/smart_city/quartz/job/EstimatedDayJob.java

@@ -0,0 +1,32 @@
+package com.bz.smart_city.quartz.job;
+
+import com.bz.smart_city.quartz.service.EstimatedDayService;
+import lombok.extern.slf4j.Slf4j;
+import org.quartz.Job;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.io.Serializable;
+
+/**
+ * @description
+ * @auto xjh
+ * @data 2021-01-12 15:32
+ */
+@Slf4j
+@Component
+public class EstimatedDayJob implements Job, Serializable {
+
+    @Autowired
+    private EstimatedDayService estimatedDayService;
+
+    @Override
+    public void execute(JobExecutionContext context) throws JobExecutionException {
+        // 2,调用推送方法
+        log.info("invoke EstimatedDayJob");
+        estimatedDayService.start();
+        log.info("invoked EstimatedDayJob");
+    }
+}

+ 10 - 0
smart-city-platform/src/main/java/com/bz/smart_city/quartz/service/EstimatedDayService.java

@@ -0,0 +1,10 @@
+package com.bz.smart_city.quartz.service;
+
+/**
+ * @description
+ * @auto xjh
+ * @data 2021-01-12 15:32
+ */
+public interface EstimatedDayService {
+    void start();
+}

+ 64 - 0
smart-city-platform/src/main/java/com/bz/smart_city/quartz/service/impl/EstimatedDayServiceImpl.java

@@ -0,0 +1,64 @@
+package com.bz.smart_city.quartz.service.impl;
+
+import com.bz.smart_city.quartz.entity.QuartzEntity;
+import com.bz.smart_city.quartz.job.EstimatedDayJob;
+import com.bz.smart_city.quartz.service.EstimatedDayService;
+import com.bz.smart_city.quartz.service.JobAndTriggerService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+
+/**
+ * @description
+ * @auto xjh
+ * @data 2021-01-12 15:32
+ */
+@Slf4j
+@Component
+public class EstimatedDayServiceImpl implements EstimatedDayService, InitializingBean {
+
+    @Resource
+    private JobAndTriggerService jobAndTriggerService;
+
+    @Override
+    public void afterPropertiesSet() {
+        saveQrtzTask();
+    }
+
+
+    public void saveQrtzTask() {
+        // 1,查询需要批量推送的配置项目并构建定时任务
+        // 2,若对应定时任务不存在则创建
+        QuartzEntity entity = new QuartzEntity();
+        entity.setJobGroup("预计费每天统计");
+        entity.setJobName("EstimatedDayJob" );
+        entity.setDescription("EstimatedDayJob" );
+        // modify by pengdi ,判断定时任务是否存在,不存在才进行新增
+
+        boolean exists = jobAndTriggerService.isExists(entity);
+        if(!exists) {
+            String cron = "0 */1 * * * ?";
+            log.info("预计费每天统计:" + cron);
+            entity.setCronExpression(cron);
+            entity.setJobClassName(EstimatedDayJob.class.getName());
+            jobAndTriggerService.save(entity);
+        }
+    }
+
+
+    public void deleteQrtzTask() {
+        // 1,查询需要批量推送的配置项目并构建定时任务
+        QuartzEntity entity = new QuartzEntity();
+        entity.setJobGroup("预计费每天统计");
+        entity.setJobName("EstimatedDayJob");
+        if (jobAndTriggerService.isExists(entity)) {
+            jobAndTriggerService.deleteJob(entity);
+        }
+    }
+
+    public void start(){
+        log.info("================================================>预计费每天统计<=====================================");
+    }
+}