张延森 4 years ago
parent
commit
f5d71d912b

+ 36
- 0
bin/start.sh View File

@@ -0,0 +1,36 @@
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
+
13
+if [[ -n $PID ]]; then
14
+    echo "Origin pid: $PID"
15
+    kill -9 $PID
16
+    echo "${APPLICATION} stopped successfully"
17
+    echo
18
+fi
19
+
20
+BASE_PATH=$(cd `dirname $0`; cd ..; pwd)
21
+APPLICATION_FILE="${BASE_PATH}/$1"
22
+echo "Ready to start ${APPLICATION_FILE}"
23
+
24
+nohup java -jar $APPLICATION_FILE >> server.log 2>&1 &
25
+
26
+newPID=$(ps -ef|grep ${PROCESS_SIGN}|grep -v grep|awk '{ print $2 }')
27
+
28
+if [[ -z $newPID ]]; then
29
+    echo "${APPLICATION} start fail"
30
+else
31
+    echo "${APPLICATION} start success. PID: $newPID"
32
+fi
33
+
34
+echo
35
+
36
+tail -f server.log

+ 19
- 0
bin/stop.sh View File

@@ -0,0 +1,19 @@
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

+ 21
- 2
pom.xml View File

@@ -10,8 +10,8 @@
10 10
 	</parent>
11 11
 	<groupId>com.xiaoniu.niucai</groupId>
12 12
 	<artifactId>task</artifactId>
13
-	<version>0.0.1-SNAPSHOT</version>
14
-	<name>task</name>
13
+	<version>0.0.1</version>
14
+	<name>niucai-task</name>
15 15
 	<description>Task of niucai</description>
16 16
 
17 17
 	<properties>
@@ -62,6 +62,25 @@
62 62
 				<artifactId>spring-boot-maven-plugin</artifactId>
63 63
 			</plugin>
64 64
 		</plugins>
65
+
66
+		<!-- 打包的时候放开 -->
67
+		<resources>
68
+			<resource>
69
+				<directory>src/main/resources</directory>
70
+				<filtering>true</filtering>
71
+				<excludes>
72
+					<exclude>application.yml</exclude>
73
+				</excludes>
74
+			</resource>
75
+			<resource>
76
+				<directory>src/main/resources</directory>
77
+				<filtering>true</filtering>
78
+				<includes>
79
+					<include>application.yml</include>
80
+				</includes>
81
+				<targetPath>${project.build.directory}/config</targetPath>
82
+			</resource>
83
+		</resources>
65 84
 	</build>
66 85
 
67 86
 	<repositories>

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

@@ -0,0 +1,20 @@
1
+package com.xiaoniu.niucai.config;
2
+
3
+import lombok.Data;
4
+import org.springframework.boot.context.properties.ConfigurationProperties;
5
+import org.springframework.stereotype.Component;
6
+
7
+@Data
8
+@Component
9
+@ConfigurationProperties(prefix = "job-cron")
10
+public class JobCronConfig {
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
+}

+ 57
- 0
src/main/java/com/xiaoniu/niucai/config/ScheduledConfig.java View File

@@ -0,0 +1,57 @@
1
+package com.xiaoniu.niucai.config;
2
+
3
+import com.xiaoniu.niucai.task.*;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.context.annotation.Configuration;
6
+import org.springframework.scheduling.annotation.SchedulingConfigurer;
7
+import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
8
+import org.springframework.scheduling.config.ScheduledTaskRegistrar;
9
+
10
+@Configuration
11
+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
+    @Autowired
32
+    SportPrize sportPrize;
33
+
34
+    @Autowired
35
+    LotteryPrize lotteryPrize;
36
+
37
+    @Autowired
38
+    MatchException matchException;
39
+
40
+    @Override
41
+    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
42
+        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
43
+        // 10 个线程
44
+        taskScheduler.setPoolSize(jobCronConfig.getPoolSize());
45
+        taskScheduler.initialize();
46
+        scheduledTaskRegistrar.setTaskScheduler(taskScheduler);
47
+
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());
56
+    }
57
+}

+ 3
- 0
src/main/java/com/xiaoniu/niucai/mapper/TaMatchMapper.java View File

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4 4
 import com.xiaoniu.niucai.entity.TaMatch;
5 5
 import org.apache.ibatis.annotations.Mapper;
6 6
 import org.apache.ibatis.annotations.Param;
7
+import org.apache.ibatis.annotations.Select;
7 8
 
8 9
 import java.util.List;
9 10
 
@@ -20,4 +21,6 @@ public interface TaMatchMapper extends BaseMapper<TaMatch> {
20 21
     List<TaMatch> getLastOpenAndUndoMatch(@Param("days") int days) throws Exception;
21 22
 
22 23
     List<TaMatch> getLastExcAndUndoMatch(@Param("hours") int hours) throws Exception;
24
+
25
+    void callProcedure(@Param("procedureName") String procedureName);
23 26
 }

+ 2
- 0
src/main/java/com/xiaoniu/niucai/service/ITaMatchService.java View File

@@ -15,4 +15,6 @@ public interface ITaMatchService extends IService<TaMatch> {
15 15
     void checkAndSetSportPrize() throws Exception;
16 16
 
17 17
     void checkExcMatches() throws Exception;
18
+
19
+    void callProcedure(String procedureName);
18 20
 }

+ 5
- 0
src/main/java/com/xiaoniu/niucai/service/impl/TaMatchServiceImpl.java View File

@@ -108,6 +108,11 @@ public class TaMatchServiceImpl extends ServiceImpl<TaMatchMapper, TaMatch> impl
108 108
         }
109 109
     }
110 110
 
111
+    @Override
112
+    public void callProcedure(String procedureName) {
113
+        taMatchMapper.callProcedure(procedureName);
114
+    }
115
+
111 116
     @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
112 117
     private void validateExcMatch(TaMatch taMatch) throws Exception {
113 118
         // 获取当前比赛的投注列表

+ 20
- 0
src/main/java/com/xiaoniu/niucai/task/CleanCrawlSpam.java View File

@@ -0,0 +1,20 @@
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 CleanCrawlSpam implements Runnable {
11
+
12
+    @Autowired
13
+    ITaMatchService iTaMatchService;
14
+
15
+    @Override
16
+    public void run() {
17
+        log.info("开始清理爬虫垃圾数据");
18
+        iTaMatchService.callProcedure("pro_clear_sport_crawl_spam");
19
+    }
20
+}

+ 20
- 0
src/main/java/com/xiaoniu/niucai/task/CrawlBasketball.java View File

@@ -0,0 +1,20 @@
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 CrawlBasketball implements Runnable {
11
+
12
+    @Autowired
13
+    ITaMatchService iTaMatchService;
14
+
15
+    @Override
16
+    public void run() {
17
+        log.info("开始执行篮球比赛解析过程");
18
+        iTaMatchService.callProcedure("pro_job_for_basketball");
19
+    }
20
+}

+ 20
- 0
src/main/java/com/xiaoniu/niucai/task/CrawlFootball.java View File

@@ -0,0 +1,20 @@
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 CrawlFootball implements Runnable {
11
+
12
+    @Autowired
13
+    ITaMatchService iTaMatchService;
14
+
15
+    @Override
16
+    public void run() {
17
+        log.info("开始执行足球比赛解析过程");
18
+        iTaMatchService.callProcedure("pro_job_for_football");
19
+    }
20
+}

+ 20
- 0
src/main/java/com/xiaoniu/niucai/task/CrawlLottery.java View File

@@ -0,0 +1,20 @@
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 CrawlLottery implements Runnable {
11
+
12
+    @Autowired
13
+    ITaMatchService iTaMatchService;
14
+
15
+    @Override
16
+    public void run() {
17
+        log.info("开始执行数字彩解析过程");
18
+        iTaMatchService.callProcedure("pro_job_for_lottery");
19
+    }
20
+}

+ 0
- 49
src/main/java/com/xiaoniu/niucai/task/Lottery.java View File

@@ -1,49 +0,0 @@
1
-package com.xiaoniu.niucai.task;
2
-
3
-import com.xiaoniu.niucai.service.ITaMatchService;
4
-import com.xiaoniu.niucai.service.ITaLotteryResultService;
5
-import lombok.extern.slf4j.Slf4j;
6
-import org.springframework.beans.factory.annotation.Autowired;
7
-import org.springframework.scheduling.annotation.Scheduled;
8
-import org.springframework.stereotype.Component;
9
-
10
-@Slf4j
11
-@Component
12
-public class Lottery {
13
-
14
-    @Autowired
15
-    ITaLotteryResultService iTaLotteryResultService;
16
-
17
-    @Autowired
18
-    ITaMatchService iTaMatchService;
19
-
20
-    /**
21
-     * 开奖轮询
22
-     */
23
-    @Scheduled(cron = "* 0/2 * * * ? ")
24
-    public void lotteryOpenCheck() {
25
-        // 数字彩
26
-        try {
27
-            iTaLotteryResultService.checkAndSetLotteryPrize();
28
-        } catch (Exception e) {
29
-            e.printStackTrace();
30
-            log.error("校验数字彩开奖结果错误: {}", e.getMessage());
31
-        }
32
-
33
-        // 体彩中奖
34
-        try {
35
-            iTaMatchService.checkAndSetSportPrize();
36
-        } catch (Exception e) {
37
-            e.printStackTrace();
38
-            log.error("校验体彩开奖结果错误: {}", e.getMessage());
39
-        }
40
-
41
-        // 体彩异常比赛
42
-        try {
43
-            iTaMatchService.checkExcMatches();
44
-        } catch (Exception e) {
45
-            e.printStackTrace();
46
-            log.error("校验异常比赛错误: {}", e.getMessage());
47
-        }
48
-    }
49
-}

+ 30
- 0
src/main/java/com/xiaoniu/niucai/task/LotteryPrize.java View File

@@ -0,0 +1,30 @@
1
+package com.xiaoniu.niucai.task;
2
+
3
+import com.xiaoniu.niucai.service.ITaLotteryResultService;
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 LotteryPrize implements Runnable {
11
+
12
+    @Autowired
13
+    ITaLotteryResultService iTaLotteryResultService;
14
+
15
+    /**
16
+     * 开奖轮询
17
+     */
18
+    @Override
19
+    public void run() {
20
+        log.info("开始处理数字彩中奖订单");
21
+
22
+        // 数字彩
23
+        try {
24
+            iTaLotteryResultService.checkAndSetLotteryPrize();
25
+        } catch (Exception e) {
26
+            e.printStackTrace();
27
+            log.error("校验数字彩开奖结果错误: {}", e.getMessage());
28
+        }
29
+    }
30
+}

+ 26
- 0
src/main/java/com/xiaoniu/niucai/task/MatchException.java View File

@@ -0,0 +1,26 @@
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 MatchException implements Runnable {
11
+
12
+    @Autowired
13
+    ITaMatchService iTaMatchService;
14
+
15
+    @Override
16
+    public void run() {
17
+        log.info("开始处理异常的比赛");
18
+
19
+        try {
20
+            iTaMatchService.checkExcMatches();
21
+        } catch (Exception e) {
22
+            e.printStackTrace();
23
+            log.error("校验异常比赛错误: {}", e.getMessage());
24
+        }
25
+    }
26
+}

src/main/java/com/xiaoniu/niucai/task/Order.java → src/main/java/com/xiaoniu/niucai/task/OrderExpire.java View File

@@ -3,6 +3,7 @@ package com.xiaoniu.niucai.task;
3 3
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4 4
 import com.xiaoniu.niucai.entity.TaOrder;
5 5
 import com.xiaoniu.niucai.service.ITaOrderService;
6
+import lombok.extern.slf4j.Slf4j;
6 7
 import org.springframework.beans.factory.annotation.Autowired;
7 8
 import org.springframework.scheduling.annotation.Scheduled;
8 9
 import org.springframework.stereotype.Component;
@@ -10,8 +11,9 @@ import org.springframework.stereotype.Component;
10 11
 import java.time.LocalDateTime;
11 12
 import java.util.List;
12 13
 
14
+@Slf4j
13 15
 @Component
14
-public class Order {
16
+public class OrderExpire implements Runnable {
15 17
 
16 18
     private final static Integer ORDER_PAYING = 2;
17 19
     private final static Integer ORDER_UNPAY = 0;
@@ -22,8 +24,9 @@ public class Order {
22 24
     /**
23 25
      * 改变支付中的状态为有效状态
24 26
      */
25
-    @Scheduled(cron = "* 0/30 * * * ? ")
26
-    public void changeOrderStatus(){
27
+    public void run(){
28
+        log.info("开始处理过期订单");
29
+
27 30
         QueryWrapper<TaOrder> taOrderQueryWrapper = new QueryWrapper<>();
28 31
         taOrderQueryWrapper.eq("pay_status", ORDER_PAYING);
29 32
         taOrderQueryWrapper.gt("update_date", LocalDateTime.now().plusMinutes(30));

+ 27
- 0
src/main/java/com/xiaoniu/niucai/task/SportPrize.java View File

@@ -0,0 +1,27 @@
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 SportPrize implements Runnable {
11
+
12
+    @Autowired
13
+    ITaMatchService iTaMatchService;
14
+
15
+    @Override
16
+    public void run() {
17
+        log.info("开始处理体彩中奖订单");
18
+
19
+        // 体彩中奖
20
+        try {
21
+            iTaMatchService.checkAndSetSportPrize();
22
+        } catch (Exception e) {
23
+            e.printStackTrace();
24
+            log.error("校验体彩开奖结果错误: {}", e.getMessage());
25
+        }
26
+    }
27
+}

+ 24
- 2
src/main/resources/application.yml View File

@@ -5,11 +5,33 @@ server:
5 5
 
6 6
 spring:
7 7
   datasource:
8
-    url: jdbc:mysql://rm-uf6z3z6jq11x653d77o.mysql.rds.aliyuncs.com:3306/niucai?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true
8
+    url: jdbc:mysql://rm-8vb8r44l60dc5ik05ao.mysql.zhangbei.rds.aliyuncs.com:3306/niucai2?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true
9 9
     username: niucai
10
-    password: 1qaz#EDC
10
+    password: sseTv!lSWgQFTZR3
11 11
 
12 12
 ###
13 13
 logging:
14 14
   level:
15 15
     root: info
16
+
17
+###
18
+job-cron:
19
+  # 线程池
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 * * * ?

+ 5
- 0
src/main/resources/mapper/TaMatchMapper.xml View File

@@ -27,4 +27,9 @@
27 27
         ORDER BY
28 28
             t.match_id ASC
29 29
     </select>
30
+
31
+    <select id="callProcedure" statementType="CALLABLE">
32
+        { call ${procedureName}() }
33
+    </select>
34
+
30 35
 </mapper>

+ 0
- 13
src/test/java/com/xiaoniu/niucai/TaskApplicationTests.java View File

@@ -1,13 +0,0 @@
1
-package com.xiaoniu.niucai;
2
-
3
-import org.junit.jupiter.api.Test;
4
-import org.springframework.boot.test.context.SpringBootTest;
5
-
6
-@SpringBootTest
7
-class TaskApplicationTests {
8
-
9
-	@Test
10
-	void contextLoads() {
11
-	}
12
-
13
-}