Selaa lähdekoodia

分布式锁自动续期,异步给被助力人发消息

cup 2 vuotta sitten
vanhempi
commit
c71c4abcbf

+ 2 - 0
mp-admin/src/main/java/com/qs/mp/MpApplication.java

@@ -9,12 +9,14 @@ import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 import org.springframework.context.annotation.Bean;
 import org.springframework.http.converter.HttpMessageConverter;
+import org.springframework.scheduling.annotation.EnableAsync;
 
 /**
  * 启动程序
  * 
  * @author ygp
  */
+@EnableAsync
 @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
 public class MpApplication
 {

+ 10 - 2
mp-admin/src/main/java/com/qs/mp/web/controller/api/user/MarketingController.java

@@ -21,6 +21,7 @@ import com.qs.mp.common.utils.DateUtils;
 import com.qs.mp.common.utils.StringUtils;
 import com.qs.mp.framework.redis.RedisLockKey;
 import com.qs.mp.framework.security.handle.HostHolder;
+import com.qs.mp.framework.service.IWxSubscribeMessage;
 import com.qs.mp.system.domain.SysUser;
 import com.qs.mp.system.service.ISysUserService;
 import com.qs.mp.user.domain.MarketingHitPrize;
@@ -86,6 +87,9 @@ public class MarketingController extends BaseApiController {
     @Autowired
     private ISysUserService sysUserService;
 
+    @Autowired
+    private IWxSubscribeMessage wxSubscribeMessage;
+
     @PostMapping("/generateCode/{marketingId}")
     @ApiOperation("立即获取抽奖码")
     public AjaxResult generateCode(@PathVariable("marketingId") Long marketingId) {
@@ -117,7 +121,8 @@ public class MarketingController extends BaseApiController {
         }
 
         String lockKey = RedisLockKey.build(RedisLockKey.MARKETING_REAL_NUM_LOCK, marketingId);
-        if (!distributedLocker.tryLock(lockKey)) {
+        // 自动续期,等待3秒
+        if (!distributedLocker.tryLock(lockKey, 3, -1, TimeUnit.SECONDS)) {
             return AjaxResult.error("活动太火爆了,请稍后重试!");
         }
 
@@ -213,7 +218,8 @@ public class MarketingController extends BaseApiController {
 
         String lockKey = RedisLockKey.build(RedisLockKey.MARKETING_REAL_NUM_LOCK, marketingHelpParam.getMarketingId());
 
-        if (!distributedLocker.tryLock(lockKey)) {
+        // 自动续期,等待3秒
+        if (!distributedLocker.tryLock(lockKey, 3, -1, TimeUnit.SECONDS)) {
             return AjaxResult.error("活动太火爆了,请稍后重试!");
         }
 
@@ -222,6 +228,8 @@ public class MarketingController extends BaseApiController {
         }finally {
             distributedLocker.unlock(lockKey);
         }
+        // 异步给被助力人发送成功消息
+        wxSubscribeMessage.sendMarketingHelp(helpedUserId, marketing);
 
 
         return AjaxResult.success("助力成功");

+ 0 - 3
mp-service/src/main/java/com/qs/mp/admin/service/impl/MarketingServiceImpl.java

@@ -479,9 +479,6 @@ public class MarketingServiceImpl extends ServiceImpl<MarketingMapper, Marketing
         boolean rtn = this.updateById(marketing);
         Assert.isTrue(rtn, "更新活动参与人数异常。marketingId:" + marketing.getId());
 
-        // 给被助力人发送成功消息
-        wxSubscribeMessage.sendMarketingHelp(helpedUserId, marketing);
-
     }
 
     /**

+ 2 - 0
mp-service/src/main/java/com/qs/mp/framework/service/impl/WxSubscribeMessageImpl.java

@@ -13,6 +13,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 
 /**
@@ -50,6 +51,7 @@ public class WxSubscribeMessageImpl implements IWxSubscribeMessage {
   }
 
   @Override
+  @Async("threadPoolTaskExecutor")
   public boolean sendMarketingHelp(Long userId, Marketing marketing) {
     String openId = getOpenIdByUserId(userId);
     WxSubscribeMessage wxSubscribeMessage = new WxSubscribeMessage(userAppId, openId, state);