From 65f8735d28a53d9c74ce2e831f71c413de809a4f Mon Sep 17 00:00:00 2001 From: zhongzb <972627721@qq.com> Date: Thu, 6 Jul 2023 23:08:06 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=BE=AE=E4=BF=A1=E6=8E=A8=E9=80=81?= =?UTF-8?q?=E6=8E=A5=E5=85=A5=E9=A2=91=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractFrequencyControlService.java | 11 ++++++ .../FrequencyControlUtil.java | 8 +++++ .../impl/WeChatMsgOperationServiceImpl.java | 34 ++++++++++++++----- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/service/frequencycontrol/AbstractFrequencyControlService.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/service/frequencycontrol/AbstractFrequencyControlService.java index 5b884c5..211f9f8 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/common/service/frequencycontrol/AbstractFrequencyControlService.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/service/frequencycontrol/AbstractFrequencyControlService.java @@ -88,6 +88,17 @@ public abstract class AbstractFrequencyControlService void executeWithFrequencyControl(String strategyName, K frequencyControl, AbstractFrequencyControlService.Executor executor) throws Throwable { + AbstractFrequencyControlService frequencyController = FrequencyControlStrategyFactory.getFrequencyControllerByName(strategyName); + frequencyController.executeWithFrequencyControl(frequencyControl, () -> { + executor.execute(); + return null; + }); + } + /** * 多限流策略的编程式调用方法调用方法 diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/WeChatMsgOperationServiceImpl.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/WeChatMsgOperationServiceImpl.java index 10347a6..640d58a 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/WeChatMsgOperationServiceImpl.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/WeChatMsgOperationServiceImpl.java @@ -1,7 +1,10 @@ package com.abin.mallchat.custom.chat.service.impl; import cn.hutool.core.thread.NamedThreadFactory; +import com.abin.mallchat.common.common.domain.dto.FrequencyControlDTO; +import com.abin.mallchat.common.common.exception.FrequencyControlException; import com.abin.mallchat.common.common.handler.GlobalUncaughtExceptionHandler; +import com.abin.mallchat.common.common.service.frequencycontrol.FrequencyControlUtil; import com.abin.mallchat.common.common.utils.JsonUtils; import com.abin.mallchat.common.user.domain.entity.User; import com.abin.mallchat.common.user.service.cache.UserCache; @@ -15,20 +18,17 @@ import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; +import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import static com.abin.mallchat.common.common.service.frequencycontrol.FrequencyControlStrategyFactory.TOTAL_COUNT_WITH_IN_FIX_TIME_FREQUENCY_CONTROLLER; + @Slf4j @Component -public class WeChatMsgOperationServiceImpl implements WeChatMsgOperationService { +public class WeChatMsgOperationServiceImpl implements WeChatMsgOperationService { private static final ExecutorService executor = new ThreadPoolExecutor(1, 10, 3000L, TimeUnit.MILLISECONDS, @@ -54,15 +54,31 @@ public class WeChatMsgOperationServiceImpl implements WeChatMsgOperationService uidSet.addAll(receiverUidList); Map userMap = userCache.getUserInfoBatch(uidSet); userMap.values().forEach(user -> { - if (Objects.nonNull(user.getOpenId()) && user.isPublishChatToWechatSwitch()) { + if (Objects.nonNull(user.getOpenId())) { executor.execute(() -> { WxMpTemplateMessage msgTemplate = getAtMsgTemplate(sender, user.getOpenId(), msg); - publishTemplateMsg(msgTemplate); + publishTemplateMsgCheckLimit(msgTemplate); }); } }); } + private void publishTemplateMsgCheckLimit(WxMpTemplateMessage msgTemplate) { + try { + FrequencyControlDTO frequencyControlDTO = new FrequencyControlDTO(); + frequencyControlDTO.setKey("TemplateMsg:" + msgTemplate.getToUser()); + frequencyControlDTO.setUnit(TimeUnit.HOURS); + frequencyControlDTO.setCount(1); + frequencyControlDTO.setTime(1); + FrequencyControlUtil.executeWithFrequencyControl(TOTAL_COUNT_WITH_IN_FIX_TIME_FREQUENCY_CONTROLLER, frequencyControlDTO, + () -> publishTemplateMsg(msgTemplate)); + } catch (FrequencyControlException e) { + log.info("wx push limit openid:{}", msgTemplate.getToUser()); + } catch (Throwable e) { + log.error("wx push error openid:{}", msgTemplate.getToUser()); + } + } + /* * 构造微信模板消息 */