Browse Source

migrate cal jobs with quartz

Lloyd 3 years ago
parent
commit
67401354c4

+ 4 - 1
pom.xml

@@ -83,7 +83,10 @@
83 83
             <groupId>org.postgresql</groupId>
84 84
             <artifactId>postgresql</artifactId>
85 85
         </dependency>
86
-
86
+        <dependency>
87
+            <groupId>org.springframework.boot</groupId>
88
+            <artifactId>spring-boot-starter-quartz</artifactId>
89
+        </dependency>
87 90
         <!--文件上传相关-->
88 91
         <dependency>
89 92
             <groupId>commons-io</groupId>

+ 0 - 52
src/main/java/com/gct/aoid/config/AsyncTaskConfig.java

@@ -1,52 +0,0 @@
1
-package com.gct.aoid.config;
2
-
3
-import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
4
-import org.springframework.context.annotation.Bean;
5
-import org.springframework.context.annotation.Configuration;
6
-import org.springframework.scheduling.annotation.AsyncConfigurer;
7
-import org.springframework.scheduling.annotation.EnableAsync;
8
-import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
9
-
10
-import java.util.concurrent.Executor;
11
-
12
-/**
13
- * 异步任务配置
14
- * @author: NewMeanning
15
- * @create: 2021-03-08 15:57
16
- **/
17
-@Configuration
18
-@EnableAsync
19
-public class AsyncTaskConfig implements AsyncConfigurer {
20
-
21
-    // ThredPoolTaskExcutor的处理流程
22
-    // 当池子大小小于corePoolSize,就新建线程,并处理请求
23
-    // 当池子大小等于corePoolSize,把请求放入workQueue中,池子里的空闲线程就去workQueue中取任务并处理
24
-    // 当workQueue放不下任务时,就新建线程入池,并处理请求,如果池子大小撑到了maximumPoolSize,就用RejectedExecutionHandler来做拒绝处理
25
-    // 当池子的线程数大于corePoolSize时,多余的线程会等待keepAliveTime长时间,如果无请求可处理就自行销毁
26
-
27
-    @Override
28
-    @Bean("dgnsAsyncExecutor")
29
-    public Executor getAsyncExecutor() {
30
-        ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
31
-        //设置核心线程数
32
-        threadPool.setCorePoolSize(10);
33
-        //设置最大线程数
34
-        threadPool.setMaxPoolSize(50);
35
-        //线程池所使用的缓冲队列
36
-        threadPool.setQueueCapacity(10);
37
-        //等待任务在关机时完成--表明等待所有线程执行完
38
-        threadPool.setWaitForTasksToCompleteOnShutdown(true);
39
-        // 等待时间 (默认为0,此时立即停止),并没等待xx秒后强制停止
40
-        threadPool.setAwaitTerminationSeconds(60);
41
-        //  线程名称前缀
42
-        threadPool.setThreadNamePrefix("Derry-Async-");
43
-        // 初始化线程
44
-        threadPool.initialize();
45
-        return threadPool;
46
-    }
47
-
48
-    @Override
49
-    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
50
-        return null;
51
-    }
52
-}

+ 60 - 0
src/main/java/com/gct/aoid/config/QuartzTaskConfig.java

@@ -0,0 +1,60 @@
1
+package com.gct.aoid.config;
2
+
3
+import com.gct.aoid.job.CalLiqDailyJobDetail;
4
+import com.gct.aoid.job.JobDataKey;
5
+import com.gct.aoid.job.TriggerConfigProperties;
6
+import org.quartz.*;
7
+import org.slf4j.LoggerFactory;
8
+import org.springframework.beans.factory.annotation.Autowired;
9
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
10
+import org.springframework.context.annotation.Bean;
11
+import org.springframework.context.annotation.Configuration;
12
+
13
+import java.util.Date;
14
+
15
+/**
16
+ * class name: QuartzTaskConfig
17
+ *
18
+ * @author lloyd
19
+ * @version 1.0
20
+ * @since 2021/5/21 上午10:21
21
+ */
22
+@Configuration
23
+@EnableConfigurationProperties(TriggerConfigProperties.class)
24
+public class QuartzTaskConfig {
25
+
26
+    @Autowired
27
+    private TriggerConfigProperties triggerConfigProperties;
28
+
29
+    @Bean
30
+    public JobDetail jobDetail1() {
31
+        try {
32
+            return JobBuilder.newJob(CalLiqDailyJobDetail.class)
33
+                    .withIdentity(triggerConfigProperties.getJt1().getJobName(), triggerConfigProperties.getJt1().getJobGroup())
34
+                    .usingJobData(JobDataKey.JobName.getDec(), triggerConfigProperties.getJt1().getJobName())
35
+                    .usingJobData(JobDataKey.JobGroup.getDec(), triggerConfigProperties.getJt1().getJobGroup())
36
+                    .storeDurably()
37
+                    .build();
38
+        } catch (Exception e) {
39
+            LoggerFactory.getLogger(this.getClass()).error("Create JobDetail-1 failed:{}", e.getMessage());
40
+            throw e;
41
+        }
42
+    }
43
+
44
+    @Bean
45
+    public Trigger trigger1() {
46
+        try {
47
+            SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
48
+                    .withIntervalInSeconds(triggerConfigProperties.getJt1().getIntervalInSeconds());
49
+            if (triggerConfigProperties.getJt1().getIsRepeatable())
50
+                scheduleBuilder.repeatForever();
51
+            return TriggerBuilder.newTrigger()
52
+                    .forJob(triggerConfigProperties.getJt1().getJobName(), triggerConfigProperties.getJt1().getJobGroup())
53
+                    .startAt(new Date(new Date().getTime() + triggerConfigProperties.getJt1().getDelayMilliseconds()))
54
+                    .withSchedule(scheduleBuilder).build();
55
+        } catch (Exception e) {
56
+            LoggerFactory.getLogger(this.getClass()).error("Create Trigger-1 failed:{}", e.getMessage());
57
+            throw e;
58
+        }
59
+    }
60
+}

+ 0 - 20
src/main/java/com/gct/aoid/config/SumDataTaskConfig.java

@@ -1,20 +0,0 @@
1
-
2
-package com.gct.aoid.config;
3
-
4
-import com.gct.aoid.service.impl.SumDataTaskServiceImpl;
5
-import org.springframework.context.annotation.Bean;
6
-import org.springframework.context.annotation.Configuration;
7
-/**
8
- * aoid_singlegt_yield -> aoid_daily_yield 数据合并
9
- * @author: gxt
10
- * @create: 2020-11-06 11:55
11
- **/
12
-
13
-@Configuration
14
-public class SumDataTaskConfig {
15
-
16
-    @Bean(initMethod = "doService")
17
-    public SumDataTaskServiceImpl SumDataTaskServiceImpl(){
18
-        return new SumDataTaskServiceImpl();
19
-    }
20
-}

+ 52 - 0
src/main/java/com/gct/aoid/job/CalLiqDailyJobDetail.java

@@ -0,0 +1,52 @@
1
+package com.gct.aoid.job;
2
+
3
+import com.gct.aoid.mapper.SumDataTaskMapper;
4
+import lombok.extern.slf4j.Slf4j;
5
+import org.quartz.Job;
6
+import org.quartz.JobDetail;
7
+import org.quartz.JobExecutionContext;
8
+import org.quartz.JobExecutionException;
9
+import org.springframework.beans.factory.annotation.Autowired;
10
+import org.springframework.scheduling.quartz.JobDetailFactoryBean;
11
+import org.springframework.scheduling.quartz.QuartzJobBean;
12
+import org.springframework.stereotype.Component;
13
+
14
+import java.time.LocalDateTime;
15
+import java.time.format.DateTimeFormatter;
16
+import java.time.format.DateTimeFormatterBuilder;
17
+import java.util.concurrent.Executors;
18
+import java.util.concurrent.ScheduledExecutorService;
19
+import java.util.concurrent.TimeUnit;
20
+
21
+/**
22
+ * class name: CalLiqDailyJobDetail
23
+ *
24
+ * @author lloyd
25
+ * @version 1.0
26
+ * @since 2021/5/21 上午10:24
27
+ */
28
+@Slf4j
29
+public class CalLiqDailyJobDetail extends QuartzJobBean {
30
+    @Autowired
31
+    public SumDataTaskMapper sumDataTaskMapper;
32
+
33
+    @Override
34
+    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
35
+        //sumDataTaskMapper= (SumDataTaskMapper)jobExecutionContext.getJobDetail().getJobDataMap().get("dao");
36
+        String jobName = jobExecutionContext.getJobDetail().getJobDataMap().getString(JobDataKey.JobName.getDec());
37
+        String jobGroup = jobExecutionContext.getJobDetail().getJobDataMap().getString(JobDataKey.JobGroup.getDec());
38
+        LocalDateTime localDateTime = LocalDateTime.now();
39
+        log.info("*===>CalLiqDailyJobDetail--JobName:{}--JobGroup:{},Time:{}", jobName, jobGroup, localDateTime.format(DateTimeFormatter.ISO_DATE_TIME));
40
+        String date = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(localDateTime);
41
+        String dateTime = date + " 00:00:00";
42
+        try {
43
+            int int1 = sumDataTaskMapper.sumData(date, dateTime);//main logic
44
+        } catch (Exception e) {
45
+            e.printStackTrace();
46
+            throw new RuntimeException("insert error");
47
+        } finally {
48
+            log.info("<===*calLiqDailyJobDetail,Time:{}", localDateTime.format(DateTimeFormatter.ISO_DATE_TIME));
49
+        }
50
+    }
51
+
52
+}

+ 23 - 0
src/main/java/com/gct/aoid/job/JobDataKey.java

@@ -0,0 +1,23 @@
1
+package com.gct.aoid.job;
2
+
3
+/**
4
+ * enum name: JobDataKey
5
+ *
6
+ * @author lloyd
7
+ * @version 1.0
8
+ * @since 2021/5/21 下午12:42
9
+ */
10
+public enum JobDataKey {
11
+    JobName("job-name"),
12
+    JobGroup("job-group");
13
+
14
+    private String dec;
15
+
16
+    JobDataKey(String dec) {
17
+        this.dec = dec;
18
+    }
19
+
20
+    public String getDec() {
21
+        return dec;
22
+    }
23
+}

+ 71 - 0
src/main/java/com/gct/aoid/job/TriggerConfigProperties.java

@@ -0,0 +1,71 @@
1
+package com.gct.aoid.job;
2
+
3
+import org.springframework.boot.context.properties.ConfigurationProperties;
4
+
5
+/**
6
+ * class name: CalLiqDailyJobTriggerConfigProperties
7
+ *
8
+ * @author lloyd
9
+ * @version 1.0
10
+ * @since 2021/5/21 上午10:35
11
+ */
12
+@ConfigurationProperties(prefix = "spring.quartz.triggers")
13
+public class TriggerConfigProperties {
14
+    private CalTaskTrigger jt1 = new CalTaskTrigger();
15
+
16
+    public CalTaskTrigger getJt1() {
17
+        return jt1;
18
+    }
19
+
20
+    public void setJt1(CalTaskTrigger jt1) {
21
+        this.jt1 = jt1;
22
+    }
23
+
24
+    public static class CalTaskTrigger {
25
+        private int IntervalInSeconds = 1;
26
+        private String JobName = "DEFAULT";
27
+        private String JobGroup = "DEFAULT_GROUP";
28
+        private long DelayMilliseconds = 0;
29
+        private boolean IsRepeatable = false;
30
+
31
+        public int getIntervalInSeconds() {
32
+            return IntervalInSeconds;
33
+        }
34
+
35
+        public void setIntervalInSeconds(int intervalInSeconds) {
36
+            IntervalInSeconds = intervalInSeconds;
37
+        }
38
+
39
+        public String getJobName() {
40
+            return JobName;
41
+        }
42
+
43
+        public void setJobName(String jobName) {
44
+            JobName = jobName;
45
+        }
46
+
47
+        public String getJobGroup() {
48
+            return JobGroup;
49
+        }
50
+
51
+        public void setJobGroup(String jobGroup) {
52
+            JobGroup = jobGroup;
53
+        }
54
+
55
+        public long getDelayMilliseconds() {
56
+            return DelayMilliseconds;
57
+        }
58
+
59
+        public void setDelayMilliseconds(long delayMilliseconds) {
60
+            DelayMilliseconds = delayMilliseconds;
61
+        }
62
+
63
+        public boolean getIsRepeatable() {
64
+            return IsRepeatable;
65
+        }
66
+
67
+        public void setIsRepeatable(boolean repeatable) {
68
+            IsRepeatable = repeatable;
69
+        }
70
+    }
71
+}

+ 5 - 2
src/main/java/com/gct/aoid/service/impl/SumDataTaskServiceImpl.java

@@ -4,12 +4,10 @@ package com.gct.aoid.service.impl;
4 4
 import com.gct.aoid.mapper.SumDataTaskMapper;
5 5
 import lombok.extern.slf4j.Slf4j;
6 6
 import org.springframework.beans.factory.annotation.Autowired;
7
-import org.springframework.beans.factory.annotation.Qualifier;
8 7
 import org.springframework.stereotype.Service;
9 8
 
10 9
 import java.time.LocalDateTime;
11 10
 import java.time.format.DateTimeFormatter;
12
-import java.util.concurrent.Executor;
13 11
 import java.util.concurrent.Executors;
14 12
 import java.util.concurrent.ScheduledExecutorService;
15 13
 import java.util.concurrent.TimeUnit;
@@ -19,9 +17,14 @@ import java.util.concurrent.TimeUnit;
19 17
  * @author gxt
20 18
  * @version 1.0.0
21 19
  * @createTime 2021年04月25日 17:37:00
20
+ *
21
+ * migrate this to job package by lloyd at 2021.5.21 13:06
22
+ *
23
+ *
22 24
  */
23 25
 @Service
24 26
 @Slf4j
27
+@Deprecated
25 28
 public class SumDataTaskServiceImpl {
26 29
 
27 30
 

+ 9 - 0
src/main/resources/application-dev.yml

@@ -74,6 +74,15 @@ spring:
74 74
         - /v2/api-docs     #swagger -----------
75 75
         - /checkCode.jpg**
76 76
         - /actuator/**    #actuator
77
+  quartz:
78
+    scheduler-name: ProductionCalApplication-QuartzScheduler
79
+    triggers:
80
+      jt1:
81
+        IntervalInSeconds: 600
82
+        JobName: CalLiqDailyJobDetail
83
+        JobGroup: CAL_TASK
84
+        DelayMilliseconds: 1000
85
+        IsRepeatable: true
77 86
 # swagger 相关配置
78 87
 swagger:
79 88
   authorization:

+ 9 - 0
src/main/resources/application-pro.yml

@@ -74,6 +74,15 @@ spring:
74 74
         - /v2/api-docs     #swagger -----------
75 75
         - /checkCode.jpg**
76 76
         - /actuator/**    #actuator
77
+  quartz:
78
+    scheduler-name: ProductionCalApplication-QuartzScheduler
79
+    triggers:
80
+      jt1:
81
+        IntervalInSeconds: 600
82
+        JobName: CalLiqDailyJobDetail
83
+        JobGroup: CAL_TASK
84
+        DelayMilliseconds: 1000
85
+        IsRepeatable: true
77 86
 # swagger 相关配置
78 87
 swagger:
79 88
   authorization: