add xxl-job

This commit is contained in:
JiyangTang 2024-06-14 18:02:46 +08:00
parent 3b4fe6810a
commit 30e8b57530
4 changed files with 194 additions and 85 deletions

View File

@ -109,6 +109,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
<build>
<resources>

View File

@ -0,0 +1,36 @@
package top.baogutang.admin.config;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @description:
* @author: nikooh
* @date: 2024/06/14 : 15:29
*/
@Slf4j
@Configuration
public class XxlJobConfig {
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.executor.appName}")
private String appName;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
log.info(">>>>>>>>>>> xxl-job config init start <<<<<<<<<<");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppname(appName);
log.info(">>>>>>>>>>> xxl-job config init success <<<<<<<<<<");
return xxlJobSpringExecutor;
}
}

View File

@ -1,85 +1,85 @@
package top.baogutang.admin.schedule;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
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.dao.entity.NoticeLogEntity;
import top.baogutang.admin.domain.AnnouncementsDto;
import top.baogutang.admin.services.INoticeLogService;
import top.baogutang.admin.services.IWxMsgPushService;
import top.baogutang.admin.utils.DingTalkMsgPushUtils;
import top.baogutang.common.constants.NoticeTypeEnum;
import top.baogutang.common.domain.PageUtil;
import top.baogutang.common.domain.Results;
import top.baogutang.common.properties.WxMsgPushProperties;
import top.baogutang.common.utils.OkHttpUtil;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* @description: EDGEX公告
* @author: nikooh
* @date: 2023/06/16 : 11:41
*/
@Slf4j
@Component
@RefreshScope
public class NoticeSchedule {
@Resource
private IWxMsgPushService wxMsgPushService;
@Resource
private WxMsgPushProperties wxMsgPushProperties;
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Resource
private DingTalkMsgPushUtils dingTalkMsgPushUtils;
@Resource
private INoticeLogService noticeLogService;
/**
* 每分钟查询edge公告
*/
@Scheduled(cron = "0 0/1 * * * ? ")
public void edgeNotice() {
Arrays.stream(NoticeTypeEnum.values())
.forEach(noticeType -> {
Results<PageUtil<AnnouncementsDto>> results = OkHttpUtil.get(noticeType.getListUrl(), null, null, new TypeReference<Results<PageUtil<AnnouncementsDto>>>() {
});
log.info(">>>>>>>>>>请求获取:{}公告返回数据:{}<<<<<<<<<<", noticeType.getDesc(), JSON.toJSONString(results));
if (Objects.isNull(results)) {
return;
}
if (!Boolean.TRUE.equals(results.isSuccess())) {
return;
}
if (Objects.isNull(results.getData()) || CollectionUtils.isEmpty(results.getData().getList())) {
return;
}
AnnouncementsDto announcementsDto = results.getData().getList().get(0);
List<NoticeLogEntity> noticeLogList = noticeLogService.queryLogByConditions(announcementsDto.getId(), noticeType);
if (CollectionUtils.isNotEmpty(noticeLogList)) {
log.info(">>>>>>>>>>notice :{} has already pushed<<<<<<<<<<", announcementsDto.getId());
return;
}
String content = noticeType.getTitle() + announcementsDto.getTitle() + "\n\n![](" + announcementsDto.getCover() + ")\n\n>";
dingTalkMsgPushUtils.robotMarkdownMsgPush(announcementsDto.getTitle(), "查看详情", noticeType.getDetailUrl() + announcementsDto.getId(), content);
noticeLogService.saveNotice(noticeType, announcementsDto.getId(), announcementsDto.getType(), announcementsDto.getTitle(), announcementsDto.getCover(), noticeType.getDetailUrl() + announcementsDto.getId(), announcementsDto.getCreator(), announcementsDto.getCreatedAt());
});
}
}
//package top.baogutang.admin.schedule;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.TypeReference;
//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.dao.entity.NoticeLogEntity;
//import top.baogutang.admin.domain.AnnouncementsDto;
//import top.baogutang.admin.services.INoticeLogService;
//import top.baogutang.admin.services.IWxMsgPushService;
//import top.baogutang.admin.utils.DingTalkMsgPushUtils;
//import top.baogutang.common.constants.NoticeTypeEnum;
//import top.baogutang.common.domain.PageUtil;
//import top.baogutang.common.domain.Results;
//import top.baogutang.common.properties.WxMsgPushProperties;
//import top.baogutang.common.utils.OkHttpUtil;
//
//import javax.annotation.Resource;
//import java.util.Arrays;
//import java.util.List;
//import java.util.Objects;
//
//
///**
// * @description: EDGEX公告
// * @author: nikooh
// * @date: 2023/06/16 : 11:41
// */
//@Slf4j
//@Component
//@RefreshScope
//public class NoticeSchedule {
//
// @Resource
// private IWxMsgPushService wxMsgPushService;
//
// @Resource
// private WxMsgPushProperties wxMsgPushProperties;
//
// @Resource
// private RedisTemplate<String, Object> redisTemplate;
//
// @Resource
// private DingTalkMsgPushUtils dingTalkMsgPushUtils;
//
// @Resource
// private INoticeLogService noticeLogService;
//
//
// /**
// * 每分钟查询edge公告
// */
// @Scheduled(cron = "0 0/1 * * * ? ")
// public void edgeNotice() {
// Arrays.stream(NoticeTypeEnum.values())
// .forEach(noticeType -> {
// Results<PageUtil<AnnouncementsDto>> results = OkHttpUtil.get(noticeType.getListUrl(), null, null, new TypeReference<Results<PageUtil<AnnouncementsDto>>>() {
// });
// log.info(">>>>>>>>>>请求获取:{}公告返回数据:{}<<<<<<<<<<", noticeType.getDesc(), JSON.toJSONString(results));
// if (Objects.isNull(results)) {
// return;
// }
// if (!Boolean.TRUE.equals(results.isSuccess())) {
// return;
// }
// if (Objects.isNull(results.getData()) || CollectionUtils.isEmpty(results.getData().getList())) {
// return;
// }
// AnnouncementsDto announcementsDto = results.getData().getList().get(0);
// List<NoticeLogEntity> noticeLogList = noticeLogService.queryLogByConditions(announcementsDto.getId(), noticeType);
// if (CollectionUtils.isNotEmpty(noticeLogList)) {
// log.info(">>>>>>>>>>notice :{} has already pushed<<<<<<<<<<", announcementsDto.getId());
// return;
// }
// String content = noticeType.getTitle() + announcementsDto.getTitle() + "\n\n![](" + announcementsDto.getCover() + ")\n\n>";
// dingTalkMsgPushUtils.robotMarkdownMsgPush(announcementsDto.getTitle(), "查看详情", noticeType.getDetailUrl() + announcementsDto.getId(), content);
// noticeLogService.saveNotice(noticeType, announcementsDto.getId(), announcementsDto.getType(), announcementsDto.getTitle(), announcementsDto.getCover(), noticeType.getDetailUrl() + announcementsDto.getId(), announcementsDto.getCreator(), announcementsDto.getCreatedAt());
// });
//
// }
//}

View File

@ -0,0 +1,68 @@
package top.baogutang.admin.schedule;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import top.baogutang.admin.dao.entity.NoticeLogEntity;
import top.baogutang.admin.domain.AnnouncementsDto;
import top.baogutang.admin.services.INoticeLogService;
import top.baogutang.admin.utils.DingTalkMsgPushUtils;
import top.baogutang.common.constants.NoticeTypeEnum;
import top.baogutang.common.domain.PageUtil;
import top.baogutang.common.domain.Results;
import top.baogutang.common.utils.OkHttpUtil;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* @description:
* @author: nikooh
* @date: 2024/06/14 : 16:18
*/
@Slf4j
@Component
@RefreshScope
public class NoticeScheduleHandler {
@Resource
private DingTalkMsgPushUtils dingTalkMsgPushUtils;
@Resource
private INoticeLogService noticeLogService;
@XxlJob("NoticeScheduleHandler")
public void noticeScheduleHandler() throws Exception {
Arrays.stream(NoticeTypeEnum.values())
.forEach(noticeType -> {
Results<PageUtil<AnnouncementsDto>> results = OkHttpUtil.get(noticeType.getListUrl(), null, null, new TypeReference<Results<PageUtil<AnnouncementsDto>>>() {
});
log.info(">>>>>>>>>>请求获取:{}公告返回数据:{}<<<<<<<<<<", noticeType.getDesc(), JSON.toJSONString(results));
if (Objects.isNull(results)) {
return;
}
if (!Boolean.TRUE.equals(results.isSuccess())) {
return;
}
if (Objects.isNull(results.getData()) || CollectionUtils.isEmpty(results.getData().getList())) {
return;
}
AnnouncementsDto announcementsDto = results.getData().getList().get(0);
List<NoticeLogEntity> noticeLogList = noticeLogService.queryLogByConditions(announcementsDto.getId(), noticeType);
if (CollectionUtils.isNotEmpty(noticeLogList)) {
log.info(">>>>>>>>>>notice :{} has already pushed<<<<<<<<<<", announcementsDto.getId());
return;
}
String content = noticeType.getTitle() + announcementsDto.getTitle() + "\n\n![](" + announcementsDto.getCover() + ")\n\n>";
dingTalkMsgPushUtils.robotMarkdownMsgPush(announcementsDto.getTitle(), "查看详情", noticeType.getDetailUrl() + announcementsDto.getId(), content);
noticeLogService.saveNotice(noticeType, announcementsDto.getId(), announcementsDto.getType(), announcementsDto.getTitle(), announcementsDto.getCover(), noticeType.getDetailUrl() + announcementsDto.getId(), announcementsDto.getCreator(), announcementsDto.getCreatedAt());
});
}
}