diff --git a/baogutang-admin/src/main/java/top/baogutang/admin/schedule/EdgeXNoticeSchedule.java b/baogutang-admin/src/main/java/top/baogutang/admin/schedule/EdgeXNoticeSchedule.java index 3a28028..1ee174c 100644 --- a/baogutang-admin/src/main/java/top/baogutang/admin/schedule/EdgeXNoticeSchedule.java +++ b/baogutang-admin/src/main/java/top/baogutang/admin/schedule/EdgeXNoticeSchedule.java @@ -6,6 +6,7 @@ import com.zjiecode.wxpusher.client.bean.Message; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import top.baogutang.admin.domain.EdgeXAnnouncementsDto; @@ -17,6 +18,9 @@ import top.baogutang.common.utils.HttpUtils; import javax.annotation.Resource; import java.util.Objects; +import java.util.concurrent.TimeUnit; + +import static top.baogutang.common.constants.CacheConstant.MSG_PUSH_PREFIX_KEY; /** * @description: EDGEX公告 @@ -34,12 +38,17 @@ public class EdgeXNoticeSchedule { @Resource private WxMsgPushProperties wxMsgPushProperties; + @Resource + private RedisTemplate redisTemplate; + + private static final String REQUEST_URL = "https://art-api.edge-x.cn/api/v1/art/announcements?pageNum=1&pageSize=1"; + /** * 每分钟查询edgeX公告 */ @Scheduled(cron = "0 0/1 * * * ? ") public void edgeXNotice() { - Results> results = HttpUtils.get("https://art-api.edge-x.cn/api/v1/art/announcements?pageNum=1&pageSize=1", new TypeReference>>() { + Results> results = HttpUtils.get(REQUEST_URL, new TypeReference>>() { }); log.info(">>>>>>>>>>请求获取edgeX公告返回数据:{}<<<<<<<<<<", JSON.toJSONString(results)); if (Objects.isNull(results)) { @@ -52,7 +61,10 @@ public class EdgeXNoticeSchedule { return; } EdgeXAnnouncementsDto announcementsDto = results.getData().getList().get(0); - if (Math.abs(announcementsDto.getCreatedAt().getTime() - System.currentTimeMillis()) <= 60000) { + String cacheKey = String.format(MSG_PUSH_PREFIX_KEY, announcementsDto.getId()); + + Boolean result = redisTemplate.opsForValue().setIfAbsent(cacheKey, 1, 5, TimeUnit.DAYS); + if (Boolean.TRUE.equals(result)) { wxMsgPushService.msgPush(Message.CONTENT_TYPE_HTML, announcementsDto.getTitle(), announcementsDto.getContent(), wxMsgPushProperties.getTopicIds()); } } diff --git a/baogutang-common/src/main/java/top/baogutang/common/constants/CacheConstant.java b/baogutang-common/src/main/java/top/baogutang/common/constants/CacheConstant.java index 56a0edb..65bb54a 100644 --- a/baogutang-common/src/main/java/top/baogutang/common/constants/CacheConstant.java +++ b/baogutang-common/src/main/java/top/baogutang/common/constants/CacheConstant.java @@ -27,5 +27,11 @@ public class CacheConstant { public static final String SYS_TOKEN = "top:baogutang:sys:user:token:"; + /** + * 消息推送 + */ + public static final String MSG_PUSH_PREFIX_KEY = "top:baogutang:msg:push:%d:"; + + public static final Integer ENABLE = 0; }