新增自定义通知类型以及通知bug修复
This commit is contained in:
@@ -19,6 +19,10 @@
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<artifactId>notify-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<artifactId>common-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@@ -1,6 +1,7 @@
|
||||
package com.hxuanyu.monitor.common;
|
||||
|
||||
import com.hxuanyu.notify.enums.NotifyType;
|
||||
import com.hxuanyu.notify.service.NotifyService;
|
||||
|
||||
/**
|
||||
* 触发器通知
|
||||
@@ -13,21 +14,33 @@ public class CheckResult {
|
||||
private boolean triggered;
|
||||
private Object notifyContent;
|
||||
private NotifyType notifyType;
|
||||
private NotifyService.CustomNotify customNotify;
|
||||
|
||||
public CheckResult() {
|
||||
}
|
||||
|
||||
public CheckResult(boolean triggered, Object notifyContent, NotifyType notifyType) {
|
||||
private CheckResult(boolean triggered, Object notifyContent, NotifyType notifyType, NotifyService.CustomNotify customNotify) {
|
||||
this.triggered = triggered;
|
||||
this.notifyContent = notifyContent;
|
||||
this.notifyType = notifyType;
|
||||
this.customNotify = customNotify;
|
||||
}
|
||||
|
||||
public NotifyService.CustomNotify getCustomNotify() {
|
||||
return customNotify;
|
||||
}
|
||||
|
||||
public void setCustomNotify(NotifyService.CustomNotify customNotify) {
|
||||
this.customNotify = customNotify;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Notify{" +
|
||||
return "CheckResult{" +
|
||||
"triggered=" + triggered +
|
||||
", notifyContent='" + notifyContent + '\'' +
|
||||
", notifyContent=" + notifyContent +
|
||||
", notifyType=" + notifyType +
|
||||
", customNotify=" + customNotify +
|
||||
'}';
|
||||
}
|
||||
|
||||
@@ -37,18 +50,30 @@ public class CheckResult {
|
||||
* @return 通知对象
|
||||
*/
|
||||
public static CheckResult nonTriggered() {
|
||||
return new CheckResult(false, null, null);
|
||||
return new CheckResult(false, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通知触发,需要传入通知信息
|
||||
*
|
||||
* @param notifyContent 通知内容
|
||||
* @param notifyType 通知类型
|
||||
* @param notifyType 通知类型
|
||||
* @return 返回结果
|
||||
*/
|
||||
public static CheckResult triggered(Object notifyContent, NotifyType notifyType) {
|
||||
return new CheckResult(true, notifyContent, notifyType);
|
||||
return new CheckResult(true, notifyContent, notifyType, null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 自定义通知类型触发
|
||||
*
|
||||
* @param customNotify 自定义通知
|
||||
* @return 返回结果
|
||||
*/
|
||||
public static CheckResult triggered(NotifyService.CustomNotify customNotify) {
|
||||
return new CheckResult(true, null, NotifyType.TYPE_CUSTOM, customNotify);
|
||||
}
|
||||
|
||||
public boolean isTriggered() {
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package com.hxuanyu.monitor.manager;
|
||||
|
||||
import com.hxuanyu.common.message.Msg;
|
||||
import com.hxuanyu.monitor.annotation.MonitorItem;
|
||||
import com.hxuanyu.monitor.base.BaseMonitorItem;
|
||||
import com.hxuanyu.monitor.common.CheckResult;
|
||||
@@ -58,31 +59,47 @@ public class MonitorItemBeanManager implements ApplicationListener<ContextRefres
|
||||
item.setMonitorItemName(name);
|
||||
item.setCron(cron);
|
||||
logger.info("获取到的Bean:{}", item);
|
||||
addMonitorTask(item);
|
||||
Msg<String> msg = addMonitorTask(item);
|
||||
logger.info("添加成功:{}", msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addMonitorTask(BaseMonitorItem item) {
|
||||
public Msg<String> addMonitorTask(BaseMonitorItem item) {
|
||||
String taskId = "ScheduledTask-" + item.getMonitorItemName();
|
||||
if (MONITOR_ITEM_MAP.containsKey(taskId)) {
|
||||
return Msg.failed("任务已经存在,请执行修改操作");
|
||||
}
|
||||
MONITOR_ITEM_MAP.put(taskId, item);
|
||||
logger.info("添加定时任务:{}, 执行周期:{}", taskId, item.getCron());
|
||||
addTask(taskId, item);
|
||||
return Msg.success("添加成功");
|
||||
}
|
||||
|
||||
public void setMonitorTaskCron(String taskId, String cron) {
|
||||
public Msg<String> setMonitorTaskCron(String taskId, String cron) {
|
||||
if (MONITOR_ITEM_MAP.containsKey(taskId)) {
|
||||
schedulingConfigurer.cancelTriggerTask(taskId);
|
||||
BaseMonitorItem item = MONITOR_ITEM_MAP.get(taskId);
|
||||
item.setCron(cron);
|
||||
addTask(taskId, item);
|
||||
logger.info("修改定时任务:{}, 执行周期:{}", taskId, item.getCron());
|
||||
return Msg.success("修改成功");
|
||||
} else {
|
||||
return Msg.failed("修改失败,该任务不存在");
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteMonitorTask(String taskId) {
|
||||
MONITOR_ITEM_MAP.remove(taskId);
|
||||
schedulingConfigurer.cancelTriggerTask(taskId);
|
||||
public Msg<String> deleteMonitorTask(String taskId) {
|
||||
if (MONITOR_ITEM_MAP.containsKey(taskId)) {
|
||||
MONITOR_ITEM_MAP.remove(taskId);
|
||||
schedulingConfigurer.cancelTriggerTask(taskId);
|
||||
logger.info("删除定时任务:{}", taskId);
|
||||
return Msg.success("删除任务成功");
|
||||
} else {
|
||||
return Msg.failed("任务不存在");
|
||||
}
|
||||
}
|
||||
|
||||
private void addTask(String taskId, BaseMonitorItem item) {
|
||||
@@ -90,8 +107,13 @@ public class MonitorItemBeanManager implements ApplicationListener<ContextRefres
|
||||
schedulingConfigurer.resetTriggerTask(taskId, new TriggerTask(() -> {
|
||||
CheckResult checkResult = item.check();
|
||||
if (checkResult.isTriggered()) {
|
||||
logger.info("定时任务[{}]触发成功,发送通知:[{}]", taskId, checkResult.getNotifyContent());
|
||||
notifyService.notify(checkResult.getNotifyContent(), NotifyType.TYPE_MAIL);
|
||||
if (NotifyType.TYPE_CUSTOM.equals(checkResult.getNotifyType())){
|
||||
logger.info("定时任务[{}]触发成功,执行自定义通知", taskId);
|
||||
notifyService.notify(checkResult.getCustomNotify());
|
||||
} else {
|
||||
logger.info("定时任务[{}]触发成功,发送通知:[{}]", taskId, checkResult.getNotifyContent());
|
||||
notifyService.notify(checkResult.getNotifyContent(), checkResult.getNotifyType());
|
||||
}
|
||||
}
|
||||
}, new CronTrigger(cron)));
|
||||
}
|
||||
|
Reference in New Issue
Block a user