kk 8 miesięcy temu
rodzic
commit
be104a4069

+ 28 - 0
game-business/src/main/java/com/game/business/config/AsyncConfig.java

@@ -0,0 +1,28 @@
+package com.game.business.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+
+import java.util.concurrent.Executor;
+import java.util.concurrent.ThreadPoolExecutor;
+
+@Configuration
+@EnableAsync
+public class AsyncConfig {
+
+    @Bean(name = "asyncExecutor")
+    public Executor asyncExecutor() {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        executor.setCorePoolSize(5); // 核心线程数
+        executor.setMaxPoolSize(10); // 最大线程数
+        executor.setQueueCapacity(25); // 队列大小
+        executor.setKeepAliveSeconds(60); // 线程空闲时的存活时间
+        executor.setThreadNamePrefix("aynTask-"); // 线程名前缀
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝策略
+        executor.initialize();
+        return executor;
+    }
+
+}

+ 195 - 6
game-business/src/main/java/com/game/business/task/AppGameBettingTask.java

@@ -1,16 +1,17 @@
 package com.game.business.task;
 
-import com.game.business.domain.AppGameBetting;
-import com.game.business.domain.AppUser;
-import com.game.business.domain.FinTranRecord;
-import com.game.business.service.IAppGameBettingService;
-import com.game.business.service.IAppUserService;
-import com.game.business.service.IFinTranRecordService;
+import com.alibaba.fastjson2.JSONArray;
+import com.alibaba.fastjson2.JSONObject;
+import com.game.business.domain.*;
+import com.game.business.service.*;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Component;
 
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 @Component
 public class AppGameBettingTask {
@@ -24,6 +25,194 @@ public class AppGameBettingTask {
     @Autowired
     private IFinTranRecordService finTranRecordService;
 
+    @Autowired
+    private IAppGameLotteryService appGameLotteryService;
+
+    @Autowired
+    private IAppGameService appGameService;
+
+    @Autowired
+    private IAppGameClassifyService appGameClassifyService;
+
+    @Async("asyncExecutor")
+    public void gameDataTask(String message){
+
+        JSONArray dataArry = JSONArray.parseArray(message);
+
+        Integer type = null;
+
+        for (int i = 0; i < dataArry.size(); i++) {
+            JSONObject jsonObject = dataArry.getJSONObject(i);
+            if(jsonObject.containsKey("type")){
+                type = jsonObject.getInteger("type");
+                break;
+            }
+        }
+
+        if(type == null){
+            System.out.println("游戏socket推送类型为空。");
+            return;
+        }
+
+        // 更新倒计时
+        if(type == 1){
+
+            JSONArray gameDataArry = null;
+            String time = null;
+            String classCode = null;
+            for (int i = 0; i < dataArry.size(); i++) {
+                JSONObject jsonObject = dataArry.getJSONObject(i);
+                if(jsonObject.containsKey("data")){
+                    gameDataArry = jsonObject.getJSONArray("data");
+                }
+                if(jsonObject.containsKey("time")){
+                    time = jsonObject.getString("time");
+                }
+                if(jsonObject.containsKey("id")){
+                    classCode = jsonObject.getString("id");
+                }
+            }
+
+            if(classCode == null){
+                System.out.println("游戏socket推送倒计时,平台ID为空。");
+                return;
+            }
+
+            if(gameDataArry == null){
+                System.out.println("游戏socket推送倒计时,data对象为空。");
+                return;
+            }
+
+            if(time == null || time.trim().isEmpty()){
+                System.out.println("游戏socket推送倒计时,time为空。");
+                return;
+            }
+
+            AppGameClassify appGameClassify = appGameClassifyService.getByCode(classCode);
+            if(appGameClassify == null){
+                System.out.println("开奖游戏平台不存在:" + classCode);
+                return;
+            }
+
+            List<AppGame> appGameList = appGameService.selectAppGameByClassId(appGameClassify.getId());
+            if(appGameList == null || appGameList.isEmpty()){
+                System.out.println("开奖游戏平台没有游戏:" + classCode);
+                return;
+            }
+            Map<String, List<AppGame>> codeGameMap = appGameList.stream().collect(Collectors.groupingBy(AppGame::getCode));
+
+            for (int i = 0; i < gameDataArry.size(); i++) {
+                JSONObject jsonObject = gameDataArry.getJSONObject(i);
+                String gameCode = jsonObject.getString("id");
+                if(gameCode == null){
+                    System.out.println("游戏socket推送倒计时,游戏id为空。");
+                    continue;
+                }
+                if(!codeGameMap.containsKey(gameCode)){
+                    System.out.println("开奖游戏不存在:" + gameCode);
+                    continue;
+                }
+                AppGame appGame = codeGameMap.get(gameCode).get(0);
+                appGame.setGameTime(time);
+                appGameService.updateAppGame(appGame);
+            }
+            return;
+        }
+
+        if(type != 2 && type != 3){
+            return;
+        }
+
+        JSONArray gameArry = null;
+        String classCode = null;
+        for (int i = 0; i < dataArry.size(); i++) {
+            JSONObject jsonObject = dataArry.getJSONObject(i);
+            if(jsonObject.containsKey("gameTypeSortArrayVOS")){
+                gameArry = jsonObject.getJSONArray("gameTypeSortArrayVOS");
+            }
+            if(jsonObject.containsKey("id")){
+                classCode = jsonObject.getString("id");
+            }
+        }
+
+        if(gameArry == null){
+            System.out.println("游戏socket推送期号或开奖,gameTypeSortArrayVOS对象为空。");
+            return;
+        }
+
+        if(classCode == null){
+            System.out.println("游戏socket推送期号或开奖,平台ID为空。");
+            return;
+        }
+
+        AppGameClassify appGameClassify = appGameClassifyService.getByCode(classCode);
+        if(appGameClassify == null){
+            System.out.println("开奖游戏平台不存在:" + classCode);
+        }
+
+        List<AppGame> appGameList = appGameService.selectAppGameByClassId(appGameClassify.getId());
+        if(appGameList == null || appGameList.isEmpty()){
+            System.out.println("开奖游戏平台没有游戏:" + classCode);
+            return;
+        }
+        Map<String, List<AppGame>> codeGameMap = appGameList.stream().collect(Collectors.groupingBy(AppGame::getCode));
+
+        for (int i = 0; i < gameArry.size(); i++) {
+            JSONObject gameLotteryObject = gameArry.getJSONObject(i);
+            String gameCode = gameLotteryObject.getString("id");
+
+            if(gameCode == null){
+                System.out.println("游戏socket推送期号或开奖,游戏ID为空。");
+                return;
+            }
+
+            if(!codeGameMap.containsKey(gameCode)){
+                System.out.println("开奖游戏不存在:" + gameCode);
+                continue;
+            }
+            AppGame appGame = codeGameMap.get(gameCode).get(0);
+
+            String gameDate = gameLotteryObject.getString("gameDate");
+            if(gameDate == null){
+                System.out.println("游戏socket推送期号或开奖,期号为空。");
+                return;
+            }
+
+            if(type == 3){
+                appGame.setGameDate(gameDate);
+                appGameService.updateAppGame(appGame);
+                continue;
+            }
+
+            long count = appGameLotteryService.selectCount(appGame.getClassifyId(), appGame.getId(), gameDate);
+
+            if(count > 0){
+                System.out.println("游戏socket推送开奖,当前期已开过将,游戏编码:" + gameCode + ",期号:" + gameDate);
+                continue;
+            }
+
+            Integer isLottery = gameLotteryObject.getInteger("isLottery");
+            String gameLotterySucc = gameLotteryObject.getString("gameLotterySucc");
+
+            // 保存开奖记录
+            AppGameLottery appGameLottery = new AppGameLottery();
+            appGameLottery.setGameId(appGame.getId());
+            appGameLottery.setClassId(appGame.getClassifyId());
+            appGameLottery.setGameDate(gameDate);
+            appGameLottery.setIsLottery(isLottery);
+            appGameLottery.setGameRecordDate(new Date());
+            appGameLottery.setGameLotterySucc(gameLotterySucc);
+            appGameLotteryService.save(appGameLottery);
+
+            if(isLottery != 1){
+                continue;
+            }
+
+            this.gameBettingTask(appGameClassify.getId(), appGame.getId(), gameDate, gameLotterySucc);
+        }
+
+    }
+
     public void gameBettingTask(Long classId, Long gameId, String gameDate, String bettingItem){
 
         List<AppGameBetting> list = appGameBettingService.getIsWinning(classId, gameId, gameDate, 0);

+ 2 - 186
game-business/src/main/java/com/game/business/websocket/client/GameOneClient.java

@@ -19,24 +19,12 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import javax.websocket.*;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
+
 
 @Component
 @ClientEndpoint
 public class GameOneClient {
 
-    @Autowired
-    private IAppGameLotteryService appGameLotteryService;
-
-    @Autowired
-    private IAppGameService appGameService;
-
-    @Autowired
-    private IAppGameClassifyService appGameClassifyService;
-
     @Autowired
     private AppGameBettingTask appGameBettingTask;
 
@@ -78,179 +66,7 @@ public class GameOneClient {
             }
 
             System.out.println("game one 接收数据" + message);
-            JSONArray dataArry = JSONArray.parseArray(message);
-
-            Integer type = null;
-
-            for (int i = 0; i < dataArry.size(); i++) {
-                JSONObject jsonObject = dataArry.getJSONObject(i);
-                if(jsonObject.containsKey("type")){
-                    type = jsonObject.getInteger("type");
-                    break;
-                }
-            }
-
-            if(type == null){
-                System.out.println("游戏socket推送类型为空。");
-                return;
-            }
-
-            // 更新倒计时
-            if(type == 1){
-
-                JSONArray gameDataArry = null;
-                String time = null;
-                String classCode = null;
-                for (int i = 0; i < dataArry.size(); i++) {
-                    JSONObject jsonObject = dataArry.getJSONObject(i);
-                    if(jsonObject.containsKey("data")){
-                        gameDataArry = jsonObject.getJSONArray("data");
-                    }
-                    if(jsonObject.containsKey("time")){
-                        time = jsonObject.getString("time");
-                    }
-                    if(jsonObject.containsKey("id")){
-                        classCode = jsonObject.getString("id");
-                    }
-                }
-
-                if(classCode == null){
-                    System.out.println("游戏socket推送倒计时,平台ID为空。");
-                    return;
-                }
-
-                if(gameDataArry == null){
-                    System.out.println("游戏socket推送倒计时,data对象为空。");
-                    return;
-                }
-
-                if(time == null || time.isEmpty()){
-                    System.out.println("游戏socket推送倒计时,time为空。");
-                    return;
-                }
-
-                AppGameClassify appGameClassify = appGameClassifyService.getByCode(classCode);
-                if(appGameClassify == null){
-                    System.out.println("开奖游戏平台不存在:" + classCode);
-                    return;
-                }
-
-                List<AppGame> appGameList = appGameService.selectAppGameByClassId(appGameClassify.getId());
-                if(appGameList == null || appGameList.isEmpty()){
-                    System.out.println("开奖游戏平台没有游戏:" + classCode);
-                    return;
-                }
-                Map<String, List<AppGame>> codeGameMap = appGameList.stream().collect(Collectors.groupingBy(AppGame::getCode));
-
-                for (int i = 0; i < gameDataArry.size(); i++) {
-                    JSONObject jsonObject = gameDataArry.getJSONObject(i);
-                    String gameCode = jsonObject.getString("id");
-                    if(gameCode == null){
-                        System.out.println("游戏socket推送倒计时,游戏id为空。");
-                        continue;
-                    }
-                    if(!codeGameMap.containsKey(gameCode)){
-                        System.out.println("开奖游戏不存在:" + gameCode);
-                        continue;
-                    }
-                    AppGame appGame = codeGameMap.get(gameCode).get(0);
-                    appGame.setGameTime(time);
-                    appGameService.updateAppGame(appGame);
-                }
-                return;
-            }
-
-            if(type != 2 && type != 3){
-                return;
-            }
-
-            JSONArray gameArry = null;
-            String classCode = null;
-            for (int i = 0; i < dataArry.size(); i++) {
-                JSONObject jsonObject = dataArry.getJSONObject(i);
-                if(jsonObject.containsKey("gameTypeSortArrayVOS")){
-                    gameArry = jsonObject.getJSONArray("gameTypeSortArrayVOS");
-                }
-                if(jsonObject.containsKey("id")){
-                    classCode = jsonObject.getString("id");
-                }
-            }
-
-            if(gameArry == null){
-                System.out.println("游戏socket推送期号或开奖,gameTypeSortArrayVOS对象为空。");
-                return;
-            }
-
-            if(classCode == null){
-                System.out.println("游戏socket推送期号或开奖,平台ID为空。");
-                return;
-            }
-
-            AppGameClassify appGameClassify = appGameClassifyService.getByCode(classCode);
-            if(appGameClassify == null){
-                System.out.println("开奖游戏平台不存在:" + classCode);
-            }
-
-            List<AppGame> appGameList = appGameService.selectAppGameByClassId(appGameClassify.getId());
-            if(appGameList == null || appGameList.isEmpty()){
-                System.out.println("开奖游戏平台没有游戏:" + classCode);
-                return;
-            }
-            Map<String, List<AppGame>> codeGameMap = appGameList.stream().collect(Collectors.groupingBy(AppGame::getCode));
-
-            for (int i = 0; i < gameArry.size(); i++) {
-                JSONObject gameLotteryObject = gameArry.getJSONObject(i);
-                String gameCode = gameLotteryObject.getString("id");
-
-                if(gameCode == null){
-                    System.out.println("游戏socket推送期号或开奖,游戏ID为空。");
-                    return;
-                }
-
-                if(!codeGameMap.containsKey(gameCode)){
-                    System.out.println("开奖游戏不存在:" + gameCode);
-                    continue;
-                }
-                AppGame appGame = codeGameMap.get(gameCode).get(0);
-
-                String gameDate = gameLotteryObject.getString("gameDate");
-                if(gameDate == null){
-                    System.out.println("游戏socket推送期号或开奖,期号为空。");
-                    return;
-                }
-
-                if(type == 3){
-                    appGame.setGameDate(gameDate);
-                    appGameService.updateAppGame(appGame);
-                    continue;
-                }
-
-                long count = appGameLotteryService.selectCount(appGame.getClassifyId(), appGame.getId(), gameDate);
-
-                if(count > 0){
-                    System.out.println("游戏socket推送开奖,当前期已开过将,游戏编码:" + gameCode + ",期号:" + gameDate);
-                    continue;
-                }
-
-                Integer isLottery = gameLotteryObject.getInteger("isLottery");
-                String gameLotterySucc = gameLotteryObject.getString("gameLotterySucc");
-
-                // 保存开奖记录
-                AppGameLottery appGameLottery = new AppGameLottery();
-                appGameLottery.setGameId(appGame.getId());
-                appGameLottery.setClassId(appGame.getClassifyId());
-                appGameLottery.setGameDate(gameDate);
-                appGameLottery.setIsLottery(isLottery);
-                appGameLottery.setGameRecordDate(new Date());
-                appGameLottery.setGameLotterySucc(gameLotterySucc);
-                appGameLotteryService.save(appGameLottery);
-
-                if(isLottery != 1){
-                    continue;
-                }
-
-                appGameBettingTask.gameBettingTask(appGameClassify.getId(), appGame.getId(), gameDate, gameLotterySucc);
-            }
+            appGameBettingTask.gameDataTask(message);
         }catch (Exception e){
             e.printStackTrace();
             System.out.println("game one 接收数据异常[" + e.getMessage() + "]");