动态加载cron表达式
```java@Component
@Lazy(false)
public class EnterpriseNotification implements SchedulingConfigurer {
//每月5号 0 0 0 5 * ?
private final static String DEFAULT_CRON = "0 0 0 5 * ?";
@Resource
private EnterpriseUserService enterpriseUserService;
@Resource
private NoticeService noticeService;
@Resource
private NoticeUserService noticeUserService;
@Resource
private ConfigService configService;
//@Scheduled(cron = "${enterprise.push}")
private void noticePush() {
Config configContent = configService.getConfigContent("enterprise.nameList", Config.Type.All);
String[] enterpriseIds = null;
if (configContent != null) {
enterpriseIds = configContent.getValue().split(",");
}
Date now = new Date();
String generate = UUIDUtils.generate();
Notice notice = new Notice();
notice.setId(generate);
notice.setIsDeleted(false);
notice.setCreateDate(now);
notice.setEditDate(now);
Config title = configService.getConfigContent("enterprise.title", Config.Type.All);
notice.setTitle(title == null ? "......" : title.getValue());
Config content = configService.getConfigContent("enterprise.content", Config.Type.All);
notice.setContent(content == null ? "......" : content.getValue());
notice.setType(Notice.Type.guide);
notice.setNoticeDate(now);
notice.setCount(1);
noticeService.saveNotice(notice);
if (enterpriseIds != null) {
for (String enterpriseId : enterpriseIds) {
List<EnterpriseUser> enterpriseUsers = enterpriseUserService.findByEnterprise(enterpriseId);
for (EnterpriseUser eu : enterpriseUsers) {
NoticeUser noticeUser = new NoticeUser();
noticeUser.setIsDeleted(false);
noticeUser.setCreateDate(now);
noticeUser.setEditDate(now);
noticeUser.setCreator("sys_job");
noticeUser.setEditor("sys_job");
noticeUser.setEnterprise(eu.getEnterprise());
noticeUser.setNoticeId(generate);
noticeUser.setUserId(eu.getId());
noticeUser.setStatus("1");
noticeUser.setId(UUIDUtils.generate());
noticeUser.setNoticeUserType(NoticeUser.NoticeUserType.guide);
noticeUserService.saveNoticeUser(noticeUser);
}
}
}
}
private String getCron() {
Config configContent = configService.getConfigContent("enterprise.push", Config.Type.All);
if (configContent == null || StringUtils.isBlank(configContent.getValue())) {
return DEFAULT_CRON;
}
return configContent.getValue();
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(() -> noticePush(), triggerContext -> {
String cron = getCron();
CronTrigger trigger = new CronTrigger(cron);
Date next = trigger.nextExecutionTime(triggerContext);
return next;
});
}
}
``` 还不够动态吧,这是写死的, 那几个service都没给 还有待改进 Lin5520 发表于 2020-7-27 19:29
还不够动态吧,这是写死的,
写死的是默认值, CRON 是从数据库拿到的。 E式丶男孩 发表于 2020-7-27 19:34
那几个service都没给
service 只是为了获取到 数据库保存的 CRON 没有实际意义
页:
[1]