浏览代码

游戏异步socket

kk 2 月之前
父节点
当前提交
12b79398e9

+ 31 - 17
game-business/src/main/java/com/game/business/task/AppGameBettingTask.java

@@ -10,6 +10,7 @@ import com.game.business.service.*;
 import com.game.business.util.Common;
 import com.game.business.util.im.TencentCloudImUtil;
 import com.game.business.vo.AppGameItemBettingVO;
+import com.game.common.constant.CacheConstants;
 import com.game.common.constant.finance.FinTranType1;
 import com.game.common.constant.finance.FinTranType2;
 import com.game.common.constant.finance.FinTranType3;
@@ -60,6 +61,7 @@ public class AppGameBettingTask {
     @Autowired
     private IAppGameItemMultipleService appGameItemMultipleService;
 
+    @Async("asyncExecutor")
     public void gameDataTask(String message, String gameCodeFinal) throws Exception {
 
         JSONArray dataArry = JSONArray.parseArray(message);
@@ -281,29 +283,41 @@ public class AppGameBettingTask {
                 continue;
             }
 
-            long count = appGameLotteryService.selectCount(appGame.getClassifyId(), appGame.getId(), gameDate);
-
-            if(count > 0){
-//                System.out.println("游戏socket推送开奖,当前期已开过将,游戏编码:" + gameCode + ",期号:" + gameDate);
+            String redisKey = CacheConstants.GAME_LOTTERY_TASK.concat(appGame.getId() + ":" + gameCode);
+            if(!redisCache.redisTemplate.opsForValue().setIfAbsent(redisKey, 1)){
                 continue;
             }
 
-            // 保存开奖记录
-            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);
-
-            lotteryTask(appGame, appGameClassify, gameLotterySucc, gameDate);
-        }
+            try {
+
+                long count = appGameLotteryService.selectCount(appGame.getClassifyId(), appGame.getId(), gameDate);
+
+                if(count > 0){
+                    redisCache.redisTemplate.delete(redisKey);
+//                System.out.println("游戏socket推送开奖,当前期已开过将,游戏编码:" + gameCode + ",期号:" + gameDate);
+                    continue;
+                }
 
+                // 保存开奖记录
+                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);
+
+                lotteryTask(appGame, appGameClassify, gameLotterySucc, gameDate);
+
+            }catch (Exception e){
+                e.printStackTrace();
+            }finally {
+                redisCache.redisTemplate.delete(redisKey);
+            }
+        }
     }
 
-    @Async("asyncExecutor")
     public void lotteryTask(AppGame appGame, AppGameClassify appGameClassify, String gameLotterySucc, String gameDate) throws Exception {
 
         this.updateItme(appGame.getId(), gameLotterySucc);

+ 5 - 0
game-common/src/main/java/com/game/common/constant/CacheConstants.java

@@ -57,4 +57,9 @@ public class CacheConstants
      */
     public static final String USER_STATICS_COUNT = "user_statics_count:";
 
+    /**
+     * 开奖任务锁
+     */
+    public static final String GAME_LOTTERY_TASK = "game_lottery_task:";
+
 }