张延森 4 years ago
parent
commit
8eed429de7

+ 4
- 3
bin/start.sh View File

@@ -1,5 +1,6 @@
1 1
 #! /bin/sh
2 2
 #
3
+# 本文件放置到任务进程的根目录下
3 4
 #
4 5
 
5 6
 # 项目名称
@@ -8,7 +9,7 @@ APPLICATION="niucai-task"
8 9
 # 项目启动jar包名称
9 10
 PROCESS_SIGN="java -jar .*${APPLICATION}.*.jar"
10 11
 
11
-PID=$(ps -ef | grep ${PROCESS_SIGN} | grep -v grep | awk '{ print $2 }')
12
+PID=$(ps -ef | grep "${PROCESS_SIGN}"| grep -v grep | awk '{ print $2 }')
12 13
 
13 14
 if [[ -n $PID ]]; then
14 15
     echo "Origin pid: $PID"
@@ -17,13 +18,13 @@ if [[ -n $PID ]]; then
17 18
     echo
18 19
 fi
19 20
 
20
-BASE_PATH=$(cd `dirname $0`; cd ..; pwd)
21
+BASE_PATH=$(cd `dirname $0`; pwd)
21 22
 APPLICATION_FILE="${BASE_PATH}/$1"
22 23
 echo "Ready to start ${APPLICATION_FILE}"
23 24
 
24 25
 nohup java -jar $APPLICATION_FILE >> server.log 2>&1 &
25 26
 
26
-newPID=$(ps -ef|grep ${PROCESS_SIGN}|grep -v grep|awk '{ print $2 }')
27
+newPID=$(ps -ef|grep "${PROCESS_SIGN}"|grep -v grep|awk '{ print $2 }')
27 28
 
28 29
 if [[ -z $newPID ]]; then
29 30
     echo "${APPLICATION} start fail"

+ 0
- 19
bin/stop.sh View File

@@ -1,19 +0,0 @@
1
-#! /bin/sh
2
-#
3
-#
4
-
5
-# 项目名称
6
-APPLICATION="niucai-task"
7
-
8
-# 项目启动jar包名称
9
-PROCESS_SIGN="java -jar .*${APPLICATION}.*.jar"
10
-
11
-PID=$(ps -ef | grep ${PROCESS_SIGN} | grep -v grep | awk '{ print $2 }')
12
-if [ -z "$PID" ]
13
-then
14
-    echo ${APPLICATION} is already stopped
15
-else
16
-    echo kill  ${PID}
17
-    kill -9 ${PID}
18
-    echo ${APPLICATION} stopped successfully
19
-fi

+ 2
- 2
pom.xml View File

@@ -9,8 +9,8 @@
9 9
 		<relativePath/> <!-- lookup parent from repository -->
10 10
 	</parent>
11 11
 	<groupId>com.xiaoniu.niucai</groupId>
12
-	<artifactId>task</artifactId>
13
-	<version>0.0.1</version>
12
+	<artifactId>niucai-task</artifactId>
13
+	<version>1.0.0</version>
14 14
 	<name>niucai-task</name>
15 15
 	<description>Task of niucai</description>
16 16
 

+ 0
- 8
src/main/java/com/xiaoniu/niucai/config/JobCronConfig.java View File

@@ -9,12 +9,4 @@ import org.springframework.stereotype.Component;
9 9
 @ConfigurationProperties(prefix = "job-cron")
10 10
 public class JobCronConfig {
11 11
     private Integer poolSize;
12
-    private String crawlFootball;
13
-    private String crawlBasketball;
14
-    private String crawlLottery;
15
-    private String cleanCrawlSpam;
16
-    private String orderExpire;
17
-    private String sportPrize;
18
-    private String lotteryPrize;
19
-    private String matchException;
20 12
 }

+ 22
- 31
src/main/java/com/xiaoniu/niucai/config/ScheduledConfig.java View File

@@ -1,41 +1,28 @@
1 1
 package com.xiaoniu.niucai.config;
2 2
 
3
-import com.xiaoniu.niucai.task.*;
3
+import com.xiaoniu.niucai.entity.SysCrontab;
4
+import com.xiaoniu.niucai.service.ISysCrontabService;
5
+import lombok.extern.slf4j.Slf4j;
4 6
 import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.context.ApplicationContext;
5 8
 import org.springframework.context.annotation.Configuration;
6 9
 import org.springframework.scheduling.annotation.SchedulingConfigurer;
7 10
 import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
8 11
 import org.springframework.scheduling.config.ScheduledTaskRegistrar;
9 12
 
13
+import java.util.List;
14
+
15
+@Slf4j
10 16
 @Configuration
11 17
 public class ScheduledConfig implements SchedulingConfigurer {
12
-
13
-    @Autowired
14
-    JobCronConfig jobCronConfig;
15
-
16
-    @Autowired
17
-    CrawlFootball crawlFootball;
18
-
19
-    @Autowired
20
-    CrawlBasketball crawlBasketball;
21
-
22
-    @Autowired
23
-    CrawlLottery crawlLottery;
24
-
25
-    @Autowired
26
-    CleanCrawlSpam cleanCrawlSpam;
27
-
28
-    @Autowired
29
-    OrderExpire orderExpire;
30
-
31 18
     @Autowired
32
-    SportPrize sportPrize;
19
+    private ApplicationContext context;
33 20
 
34 21
     @Autowired
35
-    LotteryPrize lotteryPrize;
22
+    JobCronConfig jobCronConfig;
36 23
 
37 24
     @Autowired
38
-    MatchException matchException;
25
+    ISysCrontabService iSysCrontabService;
39 26
 
40 27
     @Override
41 28
     public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
@@ -45,13 +32,17 @@ public class ScheduledConfig implements SchedulingConfigurer {
45 32
         taskScheduler.initialize();
46 33
         scheduledTaskRegistrar.setTaskScheduler(taskScheduler);
47 34
 
48
-        scheduledTaskRegistrar.addCronTask(crawlFootball, jobCronConfig.getCrawlFootball());
49
-        scheduledTaskRegistrar.addCronTask(crawlBasketball, jobCronConfig.getCrawlBasketball());
50
-        scheduledTaskRegistrar.addCronTask(crawlLottery, jobCronConfig.getCrawlLottery());
51
-        scheduledTaskRegistrar.addCronTask(cleanCrawlSpam, jobCronConfig.getCleanCrawlSpam());
52
-        scheduledTaskRegistrar.addCronTask(orderExpire, jobCronConfig.getOrderExpire());
53
-        scheduledTaskRegistrar.addCronTask(sportPrize, jobCronConfig.getSportPrize());
54
-        scheduledTaskRegistrar.addCronTask(lotteryPrize, jobCronConfig.getLotteryPrize());
55
-        scheduledTaskRegistrar.addCronTask(matchException, jobCronConfig.getMatchException());
35
+        List<SysCrontab> jobList = iSysCrontabService.list();
36
+        for (SysCrontab job : jobList) {
37
+            try {
38
+                Runnable task = (Runnable) context.getBean(Class.forName(job.getClassName()));
39
+                scheduledTaskRegistrar.addCronTask(task, job.getCronSetting().trim());
40
+
41
+                log.info("任务【{}】: 启动成功", job.getTitle());
42
+            } catch (ClassNotFoundException e) {
43
+                log.error("{} 任务类不存在", job.getClassName());
44
+                e.printStackTrace();
45
+            }
46
+        }
56 47
     }
57 48
 }

+ 40
- 0
src/main/java/com/xiaoniu/niucai/entity/SysCrontab.java View File

@@ -0,0 +1,40 @@
1
+package com.xiaoniu.niucai.entity;
2
+
3
+import com.baomidou.mybatisplus.annotation.IdType;
4
+import com.baomidou.mybatisplus.annotation.TableField;
5
+import com.baomidou.mybatisplus.annotation.TableId;
6
+import lombok.Data;
7
+import lombok.EqualsAndHashCode;
8
+import lombok.experimental.Accessors;
9
+
10
+import java.io.Serializable;
11
+import java.time.LocalDateTime;
12
+import java.util.List;
13
+
14
+/**
15
+ * <p>
16
+ * 投注方案
17
+ * </p>
18
+ *
19
+ * @author admin
20
+ * @since 2020-09-08
21
+ */
22
+@Data
23
+@EqualsAndHashCode(callSuper = false)
24
+@Accessors(chain = true)
25
+public class SysCrontab implements Serializable {
26
+
27
+    private static final long serialVersionUID = 1L;
28
+
29
+    @TableId(value = "serial_no", type = IdType.AUTO)
30
+    private Integer serialNo;
31
+
32
+    private String title;
33
+
34
+    private String className;
35
+
36
+    private String cronSetting;
37
+
38
+    private LocalDateTime createDate;
39
+
40
+}

+ 18
- 0
src/main/java/com/xiaoniu/niucai/mapper/SysCrontabMapper.java View File

@@ -0,0 +1,18 @@
1
+package com.xiaoniu.niucai.mapper;
2
+
3
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4
+import com.xiaoniu.niucai.entity.SysCrontab;
5
+import org.apache.ibatis.annotations.Mapper;
6
+
7
+/**
8
+ * <p>
9
+ * 方案条目 Mapper 接口
10
+ * </p>
11
+ *
12
+ * @author admin
13
+ * @since 2020-09-08
14
+ */
15
+@Mapper
16
+public interface SysCrontabMapper extends BaseMapper<SysCrontab> {
17
+
18
+}

+ 16
- 0
src/main/java/com/xiaoniu/niucai/service/ISysCrontabService.java View File

@@ -0,0 +1,16 @@
1
+package com.xiaoniu.niucai.service;
2
+
3
+import com.baomidou.mybatisplus.extension.service.IService;
4
+import com.xiaoniu.niucai.entity.SysCrontab;
5
+
6
+/**
7
+ * <p>
8
+ * 方案条目 服务类
9
+ * </p>
10
+ *
11
+ * @author admin
12
+ * @since 2020-09-08
13
+ */
14
+public interface ISysCrontabService extends IService<SysCrontab> {
15
+
16
+}

+ 20
- 0
src/main/java/com/xiaoniu/niucai/service/impl/SysCrontabServiceImpl.java View File

@@ -0,0 +1,20 @@
1
+package com.xiaoniu.niucai.service.impl;
2
+
3
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
4
+import com.xiaoniu.niucai.entity.SysCrontab;
5
+import com.xiaoniu.niucai.mapper.SysCrontabMapper;
6
+import com.xiaoniu.niucai.service.ISysCrontabService;
7
+import org.springframework.stereotype.Service;
8
+
9
+/**
10
+ * <p>
11
+ * 方案条目 服务实现类
12
+ * </p>
13
+ *
14
+ * @author admin
15
+ * @since 2020-09-08
16
+ */
17
+@Service
18
+public class SysCrontabServiceImpl extends ServiceImpl<SysCrontabMapper, SysCrontab> implements ISysCrontabService {
19
+
20
+}

+ 22
- 0
src/main/java/com/xiaoniu/niucai/task/CloseMatch.java View File

@@ -0,0 +1,22 @@
1
+package com.xiaoniu.niucai.task;
2
+
3
+import com.xiaoniu.niucai.service.ITaMatchService;
4
+import lombok.extern.slf4j.Slf4j;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.stereotype.Component;
7
+
8
+@Slf4j
9
+@Component
10
+public class CloseMatch implements Runnable {
11
+
12
+    @Autowired
13
+    ITaMatchService iTaMatchService;
14
+
15
+    @Override
16
+    public void run() {
17
+        log.info("开始处理体彩关闭场次");
18
+
19
+        // 体彩中奖
20
+        iTaMatchService.callProcedure("pro_close_match");
21
+    }
22
+}

+ 22
- 0
src/main/java/com/xiaoniu/niucai/task/OpenMatch.java View File

@@ -0,0 +1,22 @@
1
+package com.xiaoniu.niucai.task;
2
+
3
+import com.xiaoniu.niucai.service.ITaMatchService;
4
+import lombok.extern.slf4j.Slf4j;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.stereotype.Component;
7
+
8
+@Slf4j
9
+@Component
10
+public class OpenMatch implements Runnable {
11
+
12
+    @Autowired
13
+    ITaMatchService iTaMatchService;
14
+
15
+    @Override
16
+    public void run() {
17
+        log.info("开始处理体彩开赛场次");
18
+
19
+        // 体彩中奖
20
+        iTaMatchService.callProcedure("pro_open_match");
21
+    }
22
+}

+ 0
- 17
src/main/resources/application.yml View File

@@ -18,20 +18,3 @@ logging:
18 18
 job-cron:
19 19
   # 线程池
20 20
   pool-size: 10
21
-
22
-  # 解析足球爬虫结果
23
-  crawl-football: 0 0/10 * * * ?
24
-  # 解析篮球爬虫结果
25
-  crawl-basketball: 0 0/10 * * * ?
26
-  # 解析竞彩爬虫结果
27
-  crawl-lottery: 0 0 0/2 * * ?
28
-  # 爬虫垃圾数据
29
-  clean-crawl-spam: 0 0 0/4 * * ?
30
-  # 处理超时订单
31
-  order-expire: 0 0/15 * * * ?
32
-  # 体彩中奖
33
-  sport-prize: 0 0/3 * * * ?
34
-  # 数字彩中奖
35
-  lottery-prize: 0 0/3 * * * ?
36
-  # 异常比赛
37
-  match-exception: 0 0/5 * * * ?