Sfoglia il codice sorgente

集成延迟任务

wenhaoyu@mangguoyun.com 3 anni fa
parent
commit
f5dd9c4b4f

BIN
lib/smart-redisson-spring-boot-starter.jar


+ 10 - 0
pom.xml

@@ -155,6 +155,15 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-redis</artifactId>
         </dependency>
+
+        <dependency>
+            <groupId>riven</groupId>
+            <artifactId>smart-redisson-spring-boot-starter</artifactId>
+            <version>1.1.6</version>
+            <scope>system</scope>
+            <systemPath>${basedir}/lib/smart-redisson-spring-boot-starter.jar</systemPath>
+        </dependency>
+
         <!--<dependency>-->
         <!--    <groupId>cn.hutool</groupId>-->
         <!--    <artifactId>hutool-all</artifactId>-->
@@ -170,6 +179,7 @@
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
                 <configuration>
+                    <includeSystemScope>true</includeSystemScope>
                     <excludes>
                         <exclude>
                             <groupId>org.projectlombok</groupId>

+ 47 - 0
src/main/java/com/td/boss/config/delay/RedissonDelayTask.java

@@ -0,0 +1,47 @@
+package com.td.boss.config.delay;
+
+import com.alibaba.fastjson.JSONObject;
+import com.riven.redisson.annotation.EnableRedisson;
+import com.riven.redisson.annotation.RedissonListener;
+import com.riven.redisson.config.RedissonQueue;
+import com.riven.redisson.exception.MessageConversionException;
+import com.riven.redisson.message.*;
+import com.td.boss.config.delay.pojo.PlayerGoods;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.messaging.handler.annotation.Header;
+import org.springframework.messaging.handler.annotation.Payload;
+
+import java.util.Map;
+
+/**
+ * redisson延迟任务
+ *
+ * @author why
+ * @date 2022/03/22
+ */
+@Configuration
+@EnableRedisson
+public class RedissonDelayTask {
+    @Bean
+    public RedissonQueue redissonQueue() {
+        return new RedissonQueue("playerGoods", true, null, playerGoodsMessageConverter());
+    }
+    @Bean("playerGoodsMessageConverter")
+    public MessageConverter playerGoodsMessageConverter() {
+        return new MessageConverter() {
+            @Override
+            public QueueMessage<?> toMessage(Object object, Map<String, Object> headers) throws MessageConversionException {
+                //do something you want, eg:
+                headers.put("my_header", "my_header_value");
+                return QueueMessageBuilder.withPayload(object).headers(headers).build();
+            }
+
+            @Override
+            public Object fromMessage(RedissonMessage redissonMessage) throws MessageConversionException {
+                Object payload = redissonMessage.getPayload();
+                return JSONObject.parseObject(payload.toString(), PlayerGoods.class);
+            }
+        };
+    }
+}

+ 20 - 0
src/main/java/com/td/boss/config/delay/pojo/PlayerGoods.java

@@ -0,0 +1,20 @@
+package com.td.boss.config.delay.pojo;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * 仓库断电损耗延迟队列
+ *
+ * @author why
+ * @date 2022/03/22
+ */
+@Data
+public class PlayerGoods implements Serializable {
+    /**
+     * 用户id
+     */
+    private String userId;
+
+}

+ 19 - 0
src/main/java/com/td/boss/config/delay/pojo/Withered.java

@@ -0,0 +1,19 @@
+package com.td.boss.config.delay.pojo;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * 枯萎延迟队列
+ *
+ * @author why
+ * @date 2022/03/22
+ */
+@Data
+public class Withered implements Serializable {
+    /**
+     * 用户id
+     */
+    private String userId;
+}

+ 49 - 3
src/main/java/com/td/boss/game/complayergoods/service/ComPlayerGoodsServiceImpl.java

@@ -1,6 +1,9 @@
 package com.td.boss.game.complayergoods.service;
 
+import com.riven.redisson.annotation.RedissonListener;
+import com.riven.redisson.message.RedissonHeaders;
 import com.td.boss.common.service.*;
+import com.td.boss.config.delay.pojo.PlayerGoods;
 import com.td.boss.game.complayergoods.pojo.ComPlayerGoods;
 import com.td.boss.game.complayergoods.vo.ComPlayerGoodsSimpleVo;
 import com.td.boss.game.complayergoods.vo.ComPlayerGoodsTypeSumVo;
@@ -8,8 +11,11 @@ import com.td.boss.game.complayergoods.vo.ComPlayerGoodsVo;
 import com.td.boss.game.complayergoods.repository.ComPlayerGoodsRepository;
 import com.td.boss.util.CopyUtil;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.messaging.handler.annotation.Header;
+import org.springframework.messaging.handler.annotation.Payload;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
 import java.util.List;
@@ -17,7 +23,7 @@ import java.util.Optional;
 
 @Service
 @Transactional
-public class ComPlayerGoodsServiceImpl extends CommonServiceImpl<ComPlayerGoodsVo, ComPlayerGoods, String> implements ComPlayerGoodsService{
+public class ComPlayerGoodsServiceImpl extends CommonServiceImpl<ComPlayerGoodsVo, ComPlayerGoods, String> implements ComPlayerGoodsService {
 
     @PersistenceContext
     private EntityManager em;
@@ -26,8 +32,8 @@ public class ComPlayerGoodsServiceImpl extends CommonServiceImpl<ComPlayerGoodsV
 
 
     @Override
-    public ComPlayerGoods findByUserIdAndIndexAndType(String userId,Integer index, Integer type) {
-        Optional<ComPlayerGoods>  comPlayerGoods = comPlayerGoodsRepository.findByUserIdAndGoodsIndexAndGoodsType(userId,index,type);
+    public ComPlayerGoods findByUserIdAndIndexAndType(String userId, Integer index, Integer type) {
+        Optional<ComPlayerGoods> comPlayerGoods = comPlayerGoodsRepository.findByUserIdAndGoodsIndexAndGoodsType(userId, index, type);
         return comPlayerGoods.orElse(null);
     }
 
@@ -41,4 +47,44 @@ public class ComPlayerGoodsServiceImpl extends CommonServiceImpl<ComPlayerGoodsV
     public List<ComPlayerGoodsTypeSumVo> findAllGroupByGoodsType() {
         return comPlayerGoodsRepository.findAmountSumByGoodsType();
     }
+
+    /**
+     * 处理仓库断电损耗 延迟任务
+     *
+     * @param messageId           消息id
+     * @param queue               队列
+     * @param sendTimestamp       发送时间戳
+     * @param expectedDelayMillis 预计延迟
+     * @param myHeader            头
+     * @param playerGoods         玩家物品
+     */
+    @RedissonListener(queues = "playerGoods", messageConverter = "playerGoodsMessageConverter")
+    public void handler(@Header(value = RedissonHeaders.MESSAGE_ID, required = false) String messageId,
+                        @Header(RedissonHeaders.DELIVERY_QUEUE_NAME) String queue,
+                        @Header(RedissonHeaders.SEND_TIMESTAMP) long sendTimestamp,
+                        @Header(RedissonHeaders.EXPECTED_DELAY_MILLIS) long expectedDelayMillis,
+                        @Header(value = "my_header", required = false, defaultValue = "test") String myHeader,
+                        @Payload PlayerGoods playerGoods) {
+        /**
+         *         1.注入
+         *         @Autowired
+         *         private RedissonTemplate redissonTemplate;
+         *
+         *         2.像队列中添加延迟任务
+         *         PlayerGoods playerGoods = new PlayerGoods();
+         *         playerGoods.setUserId("1002");
+         *         redissonTemplate.sendWithDelay("playerGoods", playerGoods, 10000);
+         */
+
+        System.out.println(messageId);
+        System.out.println(queue);
+        System.out.println(myHeader);
+        long actualDelay = System.currentTimeMillis() - (sendTimestamp + expectedDelayMillis);
+        System.out.println("receive " + playerGoods + ", delayed " + actualDelay + " millis");
+
+        List<ComPlayerGoods> allByUserId = comPlayerGoodsRepository.findAllByUserId(playerGoods.getUserId());
+        System.out.println("查一下:" + allByUserId);
+        System.out.println(allByUserId);
+
+    }
 }

+ 8 - 0
src/main/resources/application.yml

@@ -72,6 +72,14 @@ spring:
         repositories:
           enabled: false
 
+  # smart redisson
+  smart-redisson:
+    database: 12
+    server-address: 123.57.252.53:6379
+    password: heihei
+
+
+
 #是否需要输入验证码
 captcha:
   enable: false

+ 14 - 0
src/test/java/com/td/boss/ComPlayerLuckyTests.java

@@ -2,7 +2,9 @@ package com.td.boss;
 
 import cn.hutool.json.JSONUtil;
 import com.td.boss.common.pojo.Result;
+import com.td.boss.common.service.ComConfigService;
 import com.td.boss.game.complayersattri.controller.ComPlayersLuckyController;
+import com.td.boss.game.complayersattri.vo.ComPlayersLuckyLandLevelUpVo;
 import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -10,6 +12,8 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
+import java.util.List;
+
 /**
  * 工具模块
  */
@@ -23,6 +27,9 @@ public class ComPlayerLuckyTests {
     @Autowired
     ComPlayersLuckyController comPlayersLuckyController;
 
+    @Autowired
+    private ComConfigService configService;
+
     @Test
     public void getLandTools() {
         Result<?> landTools = comPlayersLuckyController.getLandTools();
@@ -100,4 +107,11 @@ public class ComPlayerLuckyTests {
         Result<?> result = comPlayersLuckyController.toSNB(userId, 50 * 20);
         System.out.println(JSONUtil.toJsonStr(result));
     }
+
+    @Test
+    public void getConfig(){
+        List<ComPlayersLuckyLandLevelUpVo> list = configService.selectLandLevel();
+        String json = JSONUtil.toJsonStr(list);
+        System.out.println(json);
+    }
 }