From c338aa60df58bee9f4c25afedf94fe6930a119c1 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期三, 29 七月 2020 19:01:56 +0800 Subject: [PATCH] SpringContext中的循环任务优化,添加业务监控 --- fanli/src/main/java/com/yeshi/fanli/service/impl/push/HWPushServiceImpl.java | 146 +++++++++++++++++++++++------------------------- 1 files changed, 70 insertions(+), 76 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/push/HWPushServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/push/HWPushServiceImpl.java index 6441c25..304eb66 100644 --- a/fanli/src/main/java/com/yeshi/fanli/service/impl/push/HWPushServiceImpl.java +++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/push/HWPushServiceImpl.java @@ -1,20 +1,10 @@ package com.yeshi.fanli.service.impl.push; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import javax.annotation.Resource; - import com.yeshi.fanli.dto.push.PushBaseContent; -import com.yeshi.fanli.entity.SystemEnum; -import org.springframework.scheduling.annotation.Async; -import org.springframework.stereotype.Service; - +import com.yeshi.fanli.dto.push.PushContentDetailDTO; import com.yeshi.fanli.dto.push.PushTypeEnum; import com.yeshi.fanli.entity.AppVersionInfo; +import com.yeshi.fanli.entity.SystemEnum; import com.yeshi.fanli.entity.bus.user.UserCustomSettings; import com.yeshi.fanli.entity.bus.user.UserCustomSettings.UserSettingTypeEnum; import com.yeshi.fanli.entity.push.DeviceTokenHW; @@ -24,8 +14,14 @@ import com.yeshi.fanli.service.inter.push.DeviceTokenHWService; import com.yeshi.fanli.service.inter.push.HWPushService; import com.yeshi.fanli.service.inter.user.UserCustomSettingsService; +import com.yeshi.fanli.util.Constant; import com.yeshi.fanli.util.StringUtil; +import com.yeshi.fanli.util.mq.cmq.PushCMQManager; import com.yeshi.fanli.util.push.HWPushUtil; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.*; @Service public class HWPushServiceImpl implements HWPushService { @@ -42,7 +38,7 @@ private List<Integer> getVersionCodeList(List<Integer> versionCodeList, SystemEnum system) { List<Integer> versionList = new ArrayList<>(); if (versionCodeList == null) {// 鍏ㄦ帹 - List<AppVersionInfo> appInfoList = appVersionService.listByPlatformAndMinVersionCode("android", 46,system); + List<AppVersionInfo> appInfoList = appVersionService.listByPlatformAndMinVersionCode("android", 46, system); if (appInfoList != null) for (AppVersionInfo version : appInfoList) { versionList.add(version.getVersionCode()); @@ -58,52 +54,46 @@ return versionList; } - @Async("pushExecutor") @Override public void pushGoods(Long uid, Long auctionId, PushBaseContent baseContent) throws PushException { - push(PushTypeEnum.goodsdetail, uid, null, null, auctionId, baseContent); + addPushQueue(PushTypeEnum.goodsdetail, uid, null, null, auctionId, baseContent); } - @Async("pushExecutor") @Override public void pushUrl(Long uid, String url, PushBaseContent baseContent) throws PushException { - push(PushTypeEnum.url, uid, null, url, null, baseContent); + addPushQueue(PushTypeEnum.url, uid, null, url, null, baseContent); } - @Async("pushExecutor") @Override public void pushZNX(Long uid, PushBaseContent baseContent) throws PushException { - push(PushTypeEnum.ZNX, uid, null, null, null, baseContent); + addPushQueue(PushTypeEnum.ZNX, uid, null, null, null, baseContent); } - @Async("pushExecutor") @Override public void pushWEEX(Long uid, String weexUrl, PushBaseContent baseContent) throws PushException { - push(PushTypeEnum.weex, uid, weexUrl, null, null, baseContent); + addPushQueue(PushTypeEnum.weex, uid, weexUrl, null, null, baseContent); } - @Async("pushExecutor") @Override public void pushBaiChuanUrl(Long uid, String url, PushBaseContent baseContent) throws PushException { - push(PushTypeEnum.baichuan, uid, null, url, null, baseContent); + addPushQueue(PushTypeEnum.baichuan, uid, null, url, null, baseContent); } - @Async("pushExecutor") @Override public void pushWelfareCenter(Long uid, PushBaseContent baseContent) throws PushException { - push(PushTypeEnum.welfare, uid, null, null, null, baseContent); + addPushQueue(PushTypeEnum.welfare, uid, null, null, null, baseContent); } @Override public void pushUserSignInNotification(Long uid, PushBaseContent baseContent) throws PushException { - push(PushTypeEnum.signin, uid, null, null, null, baseContent); + addPushQueue(PushTypeEnum.signin, uid, null, null, null, baseContent); } private List<DeviceTokenHW> filterDeviceToken(List<DeviceTokenHW> hwDeviceList, int hour) { @@ -136,58 +126,62 @@ return hwDeviceList; } + //娣诲姞鍒版帹閫侀槦鍒� + private void addPushQueue(PushTypeEnum type, Long uid, String url, String webUrl, + Long goodsId, PushBaseContent content) { + //娣诲姞鍒版帹閫侀槦鍒� + PushContentDetailDTO dto = new PushContentDetailDTO(type, uid, url, webUrl, goodsId, content); + PushCMQManager.getInstance().addHWPush(dto); + } + /** - * 鎺ㄩ�� - * - * @param type - * @param uid - * @param content - * @param url - * @param webUrl + * @param dto + * @throws PushException */ - private void push(PushTypeEnum type, Long uid, String url, String webUrl, - Long goodsId, PushBaseContent content) throws PushException { + public void push(PushContentDetailDTO dto) throws PushException { int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); - List<Integer> newVersionList = getVersionCodeList(content.getVersionCodeList(),content.getSystem()); + List<Integer> newVersionList = getVersionCodeList(dto.getContent().getVersionCodeList(), dto.getContent().getSystem()); // 1.6.5鍚庡紑濮嬫帹閫� for (int i = 0; i < newVersionList.size(); i++) { if (newVersionList.get(i) < 47) newVersionList.remove(i--); } - if (uid == null || uid == 0) { - long deviceCount = deviceTokenHWService.countDeviceToken(newVersionList); + if (dto.getUid() == null || dto.getUid() == 0) { + //鍙帹閫佽繎30澶╃殑娲昏穬 + Date minUpdateTime = new Date(System.currentTimeMillis() - Constant.PUSH_MIN_TIME); + long deviceCount = deviceTokenHWService.countDeviceToken(newVersionList, minUpdateTime); int page = (int) (deviceCount % 100 == 0 ? deviceCount / 100 : deviceCount / 100 + 1); for (int p = 1; p <= page; p++) { - List<DeviceTokenHW> hwDeviceList = deviceTokenHWService.getDeviceTokenList(p, 100, newVersionList); + List<DeviceTokenHW> hwDeviceList = deviceTokenHWService.getDeviceTokenList(p, 100, newVersionList, minUpdateTime); hwDeviceList = filterDeviceToken(hwDeviceList, hour); List<String> tokenList = new ArrayList<>(); for (DeviceTokenHW token : hwDeviceList) tokenList.add(token.getDeviceToken()); try { - if (type == PushTypeEnum.goodsdetail) {// 鍟嗗搧璇︽儏 - if (goodsId == null) + if (dto.getType() == PushTypeEnum.goodsdetail) {// 鍟嗗搧璇︽儏 + if (dto.getGoodsId() == null) throw new PushException(2, "鍙傛暟涓嶅畬鏁�"); - HWPushUtil.pushGoods(tokenList, content.getTitle(), content.getContent(), 1, goodsId); - } else if (type == PushTypeEnum.url) {// 閾炬帴 - if (StringUtil.isNullOrEmpty(webUrl)) + HWPushUtil.pushGoods(tokenList, dto.getContent().getTitle(), dto.getContent().getContent(), 1, dto.getGoodsId()); + } else if (dto.getType() == PushTypeEnum.url) {// 閾炬帴 + if (StringUtil.isNullOrEmpty(dto.getWebUrl())) throw new PushException(2, "鍙傛暟涓嶅畬鏁�"); - HWPushUtil.pushUrl(tokenList, content.getTitle(), content.getContent(), webUrl); - } else if (type == PushTypeEnum.ZNX) {// 绔欏唴淇� - HWPushUtil.pushZNX(tokenList, content.getTitle(), content.getContent()); - } else if (type == PushTypeEnum.weex) {// weex - if (StringUtil.isNullOrEmpty(url)) + HWPushUtil.pushUrl(tokenList, dto.getContent().getTitle(), dto.getContent().getContent(), dto.getWebUrl()); + } else if (dto.getType() == PushTypeEnum.ZNX) {// 绔欏唴淇� + HWPushUtil.pushZNX(tokenList, dto.getContent().getTitle(), dto.getContent().getContent()); + } else if (dto.getType() == PushTypeEnum.weex) {// weex + if (StringUtil.isNullOrEmpty(dto.getUrl())) throw new PushException(2, "鍙傛暟涓嶅畬鏁�"); - HWPushUtil.pushWEEX(tokenList, content.getTitle(), content.getContent(), url); - } else if (type == PushTypeEnum.baichuan) {// 鐧惧窛閾炬帴 - if (StringUtil.isNullOrEmpty(webUrl)) + HWPushUtil.pushWEEX(tokenList, dto.getContent().getTitle(), dto.getContent().getContent(), dto.getUrl()); + } else if (dto.getType() == PushTypeEnum.baichuan) {// 鐧惧窛閾炬帴 + if (StringUtil.isNullOrEmpty(dto.getWebUrl())) throw new PushException(2, "鍙傛暟涓嶅畬鏁�"); - HWPushUtil.pushBaiChuanUrl(tokenList, content.getTitle(), content.getContent(), webUrl); - } else if (type == PushTypeEnum.welfare) {// 绂忓埄涓績 - HWPushUtil.pushWelfareCenter(tokenList, content.getTitle(), content.getContent()); - } else if (type == PushTypeEnum.signin) {// 绛惧埌 - HWPushUtil.pushUserSignInNotification(tokenList, content.getTitle(), content.getContent()); + HWPushUtil.pushBaiChuanUrl(tokenList, dto.getContent().getTitle(), dto.getContent().getContent(), dto.getWebUrl()); + } else if (dto.getType() == PushTypeEnum.welfare) {// 绂忓埄涓績 + HWPushUtil.pushWelfareCenter(tokenList, dto.getContent().getTitle(), dto.getContent().getContent()); + } else if (dto.getType() == PushTypeEnum.signin) {// 绛惧埌 + HWPushUtil.pushUserSignInNotification(tokenList, dto.getContent().getTitle(), dto.getContent().getContent()); } } catch (Exception e) { PushLogHelper.hwError(e); @@ -196,7 +190,7 @@ } } else { // 鍗庝负鍗曟帹 - List<DeviceTokenHW> hwDeviceList = deviceTokenHWService.getDeviceTokenByUid(uid, newVersionList); + List<DeviceTokenHW> hwDeviceList = deviceTokenHWService.getDeviceTokenByUid(dto.getUid(), newVersionList); List<String> tokenList = new ArrayList<>(); if (hwDeviceList != null) { hwDeviceList = filterDeviceToken(hwDeviceList, hour); @@ -207,28 +201,28 @@ return; try { - if (type == PushTypeEnum.goodsdetail) {// 鍟嗗搧璇︽儏 - if (goodsId == null) + if (dto.getType() == PushTypeEnum.goodsdetail) {// 鍟嗗搧璇︽儏 + if (dto.getGoodsId() == null) throw new PushException(2, "鍙傛暟涓嶅畬鏁�"); - HWPushUtil.pushGoods(tokenList, content.getTitle(), content.getContent(), 1, goodsId); - } else if (type == PushTypeEnum.url) {// 閾炬帴 - if (StringUtil.isNullOrEmpty(webUrl)) + HWPushUtil.pushGoods(tokenList, dto.getContent().getTitle(), dto.getContent().getContent(), 1, dto.getGoodsId()); + } else if (dto.getType() == PushTypeEnum.url) {// 閾炬帴 + if (StringUtil.isNullOrEmpty(dto.getWebUrl())) throw new PushException(2, "鍙傛暟涓嶅畬鏁�"); - HWPushUtil.pushUrl(tokenList, content.getTitle(), content.getContent(), webUrl); - } else if (type == PushTypeEnum.ZNX) {// 绔欏唴淇� - HWPushUtil.pushZNX(tokenList, content.getTitle(), content.getContent()); - } else if (type == PushTypeEnum.weex) {// weex - if (StringUtil.isNullOrEmpty(url)) + HWPushUtil.pushUrl(tokenList, dto.getContent().getTitle(), dto.getContent().getContent(), dto.getWebUrl()); + } else if (dto.getType() == PushTypeEnum.ZNX) {// 绔欏唴淇� + HWPushUtil.pushZNX(tokenList, dto.getContent().getTitle(), dto.getContent().getContent()); + } else if (dto.getType() == PushTypeEnum.weex) {// weex + if (StringUtil.isNullOrEmpty(dto.getUrl())) throw new PushException(2, "鍙傛暟涓嶅畬鏁�"); - HWPushUtil.pushWEEX(tokenList, content.getTitle(), content.getContent(), url); - } else if (type == PushTypeEnum.baichuan) {// 鐧惧窛閾炬帴 - if (StringUtil.isNullOrEmpty(webUrl)) + HWPushUtil.pushWEEX(tokenList, dto.getContent().getTitle(), dto.getContent().getContent(), dto.getUrl()); + } else if (dto.getType() == PushTypeEnum.baichuan) {// 鐧惧窛閾炬帴 + if (StringUtil.isNullOrEmpty(dto.getWebUrl())) throw new PushException(2, "鍙傛暟涓嶅畬鏁�"); - HWPushUtil.pushBaiChuanUrl(tokenList, content.getTitle(), content.getContent(), webUrl); - } else if (type == PushTypeEnum.welfare) {// 绂忓埄涓績 - HWPushUtil.pushWelfareCenter(tokenList, content.getTitle(), content.getContent()); - } else if (type == PushTypeEnum.signin) {// 绛惧埌 - HWPushUtil.pushUserSignInNotification(tokenList, content.getTitle(), content.getContent()); + HWPushUtil.pushBaiChuanUrl(tokenList, dto.getContent().getTitle(), dto.getContent().getContent(), dto.getWebUrl()); + } else if (dto.getType() == PushTypeEnum.welfare) {// 绂忓埄涓績 + HWPushUtil.pushWelfareCenter(tokenList, dto.getContent().getTitle(), dto.getContent().getContent()); + } else if (dto.getType() == PushTypeEnum.signin) {// 绛惧埌 + HWPushUtil.pushUserSignInNotification(tokenList, dto.getContent().getTitle(), dto.getContent().getContent()); } } catch (Exception e) { PushLogHelper.hwError(e); -- Gitblit v1.8.0