This commit is contained in:
JiyangTang 2023-06-26 17:19:21 +08:00
parent 127c7ed8ab
commit 03a70aeb53
2 changed files with 20 additions and 2 deletions

View File

@ -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<String, Object> 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<Page<EdgeXAnnouncementsDto>> results = HttpUtils.get("https://art-api.edge-x.cn/api/v1/art/announcements?pageNum=1&pageSize=1", new TypeReference<Results<Page<EdgeXAnnouncementsDto>>>() {
Results<Page<EdgeXAnnouncementsDto>> results = HttpUtils.get(REQUEST_URL, new TypeReference<Results<Page<EdgeXAnnouncementsDto>>>() {
});
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());
}
}

View File

@ -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;
}