新增自定义通知类型以及通知bug修复
This commit is contained in:
parent
0c074c046f
commit
cb418ba35a
@ -19,6 +19,10 @@
|
|||||||
<groupId>com.hxuanyu</groupId>
|
<groupId>com.hxuanyu</groupId>
|
||||||
<artifactId>notify-spring-boot-starter</artifactId>
|
<artifactId>notify-spring-boot-starter</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.hxuanyu</groupId>
|
||||||
|
<artifactId>common-spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -1,6 +1,7 @@
|
|||||||
package com.hxuanyu.monitor.common;
|
package com.hxuanyu.monitor.common;
|
||||||
|
|
||||||
import com.hxuanyu.notify.enums.NotifyType;
|
import com.hxuanyu.notify.enums.NotifyType;
|
||||||
|
import com.hxuanyu.notify.service.NotifyService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 触发器通知
|
* 触发器通知
|
||||||
@ -13,21 +14,33 @@ public class CheckResult {
|
|||||||
private boolean triggered;
|
private boolean triggered;
|
||||||
private Object notifyContent;
|
private Object notifyContent;
|
||||||
private NotifyType notifyType;
|
private NotifyType notifyType;
|
||||||
|
private NotifyService.CustomNotify customNotify;
|
||||||
|
|
||||||
public CheckResult() {
|
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.triggered = triggered;
|
||||||
this.notifyContent = notifyContent;
|
this.notifyContent = notifyContent;
|
||||||
this.notifyType = notifyType;
|
this.notifyType = notifyType;
|
||||||
|
this.customNotify = customNotify;
|
||||||
|
}
|
||||||
|
|
||||||
|
public NotifyService.CustomNotify getCustomNotify() {
|
||||||
|
return customNotify;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCustomNotify(NotifyService.CustomNotify customNotify) {
|
||||||
|
this.customNotify = customNotify;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Notify{" +
|
return "CheckResult{" +
|
||||||
"triggered=" + triggered +
|
"triggered=" + triggered +
|
||||||
", notifyContent='" + notifyContent + '\'' +
|
", notifyContent=" + notifyContent +
|
||||||
|
", notifyType=" + notifyType +
|
||||||
|
", customNotify=" + customNotify +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,7 +50,7 @@ public class CheckResult {
|
|||||||
* @return 通知对象
|
* @return 通知对象
|
||||||
*/
|
*/
|
||||||
public static CheckResult nonTriggered() {
|
public static CheckResult nonTriggered() {
|
||||||
return new CheckResult(false, null, null);
|
return new CheckResult(false, null, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -48,7 +61,19 @@ public class CheckResult {
|
|||||||
* @return 返回结果
|
* @return 返回结果
|
||||||
*/
|
*/
|
||||||
public static CheckResult triggered(Object notifyContent, NotifyType notifyType) {
|
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() {
|
public boolean isTriggered() {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.hxuanyu.monitor.manager;
|
package com.hxuanyu.monitor.manager;
|
||||||
|
|
||||||
|
import com.hxuanyu.common.message.Msg;
|
||||||
import com.hxuanyu.monitor.annotation.MonitorItem;
|
import com.hxuanyu.monitor.annotation.MonitorItem;
|
||||||
import com.hxuanyu.monitor.base.BaseMonitorItem;
|
import com.hxuanyu.monitor.base.BaseMonitorItem;
|
||||||
import com.hxuanyu.monitor.common.CheckResult;
|
import com.hxuanyu.monitor.common.CheckResult;
|
||||||
@ -58,31 +59,47 @@ public class MonitorItemBeanManager implements ApplicationListener<ContextRefres
|
|||||||
item.setMonitorItemName(name);
|
item.setMonitorItemName(name);
|
||||||
item.setCron(cron);
|
item.setCron(cron);
|
||||||
logger.info("获取到的Bean:{}", item);
|
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();
|
String taskId = "ScheduledTask-" + item.getMonitorItemName();
|
||||||
|
if (MONITOR_ITEM_MAP.containsKey(taskId)) {
|
||||||
|
return Msg.failed("任务已经存在,请执行修改操作");
|
||||||
|
}
|
||||||
MONITOR_ITEM_MAP.put(taskId, item);
|
MONITOR_ITEM_MAP.put(taskId, item);
|
||||||
logger.info("添加定时任务:{}, 执行周期:{}", taskId, item.getCron());
|
logger.info("添加定时任务:{}, 执行周期:{}", taskId, item.getCron());
|
||||||
addTask(taskId, item);
|
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)) {
|
if (MONITOR_ITEM_MAP.containsKey(taskId)) {
|
||||||
|
schedulingConfigurer.cancelTriggerTask(taskId);
|
||||||
BaseMonitorItem item = MONITOR_ITEM_MAP.get(taskId);
|
BaseMonitorItem item = MONITOR_ITEM_MAP.get(taskId);
|
||||||
item.setCron(cron);
|
item.setCron(cron);
|
||||||
addTask(taskId, item);
|
addTask(taskId, item);
|
||||||
|
logger.info("修改定时任务:{}, 执行周期:{}", taskId, item.getCron());
|
||||||
|
return Msg.success("修改成功");
|
||||||
|
} else {
|
||||||
|
return Msg.failed("修改失败,该任务不存在");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteMonitorTask(String taskId) {
|
public Msg<String> deleteMonitorTask(String taskId) {
|
||||||
|
if (MONITOR_ITEM_MAP.containsKey(taskId)) {
|
||||||
MONITOR_ITEM_MAP.remove(taskId);
|
MONITOR_ITEM_MAP.remove(taskId);
|
||||||
schedulingConfigurer.cancelTriggerTask(taskId);
|
schedulingConfigurer.cancelTriggerTask(taskId);
|
||||||
|
logger.info("删除定时任务:{}", taskId);
|
||||||
|
return Msg.success("删除任务成功");
|
||||||
|
} else {
|
||||||
|
return Msg.failed("任务不存在");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addTask(String taskId, BaseMonitorItem item) {
|
private void addTask(String taskId, BaseMonitorItem item) {
|
||||||
@ -90,8 +107,13 @@ public class MonitorItemBeanManager implements ApplicationListener<ContextRefres
|
|||||||
schedulingConfigurer.resetTriggerTask(taskId, new TriggerTask(() -> {
|
schedulingConfigurer.resetTriggerTask(taskId, new TriggerTask(() -> {
|
||||||
CheckResult checkResult = item.check();
|
CheckResult checkResult = item.check();
|
||||||
if (checkResult.isTriggered()) {
|
if (checkResult.isTriggered()) {
|
||||||
|
if (NotifyType.TYPE_CUSTOM.equals(checkResult.getNotifyType())){
|
||||||
|
logger.info("定时任务[{}]触发成功,执行自定义通知", taskId);
|
||||||
|
notifyService.notify(checkResult.getCustomNotify());
|
||||||
|
} else {
|
||||||
logger.info("定时任务[{}]触发成功,发送通知:[{}]", taskId, checkResult.getNotifyContent());
|
logger.info("定时任务[{}]触发成功,发送通知:[{}]", taskId, checkResult.getNotifyContent());
|
||||||
notifyService.notify(checkResult.getNotifyContent(), NotifyType.TYPE_MAIL);
|
notifyService.notify(checkResult.getNotifyContent(), checkResult.getNotifyType());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, new CronTrigger(cron)));
|
}, new CronTrigger(cron)));
|
||||||
}
|
}
|
||||||
|
@ -10,10 +10,11 @@ public enum NotifyType {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 邮件类型
|
* 通知类型
|
||||||
*/
|
*/
|
||||||
TYPE_LOG("日志输出"),
|
TYPE_LOG("日志输出"),
|
||||||
TYPE_MAIL("邮件"),
|
TYPE_MAIL("邮件"),
|
||||||
|
TYPE_CUSTOM("自定义"),
|
||||||
TYPE_MSG("短信");
|
TYPE_MSG("短信");
|
||||||
|
|
||||||
private final String typeName;
|
private final String typeName;
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package com.hxuanyu.notify.service;
|
package com.hxuanyu.notify.service;
|
||||||
|
|
||||||
import com.hxuanyu.notify.enums.NotifyType;
|
import com.hxuanyu.notify.enums.NotifyType;
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通知服务
|
* 通知服务
|
||||||
@ -17,4 +16,18 @@ public interface NotifyService {
|
|||||||
* @param notifyType 通知类型
|
* @param notifyType 通知类型
|
||||||
*/
|
*/
|
||||||
void notify(Object content, NotifyType notifyType);
|
void notify(Object content, NotifyType notifyType);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义通知
|
||||||
|
*
|
||||||
|
* @param customNotify 自定义通知接口,需手动实现
|
||||||
|
*/
|
||||||
|
void notify(CustomNotify customNotify);
|
||||||
|
|
||||||
|
interface CustomNotify {
|
||||||
|
/**
|
||||||
|
* 通知
|
||||||
|
*/
|
||||||
|
void onNotify();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,17 +25,26 @@ public class NotifyServiceImpl implements NotifyService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void notify(Object content, NotifyType notifyType) {
|
public void notify(Object content, NotifyType notifyType) {
|
||||||
|
logger.debug("通知内容:{},通知类型:{}", notifyType, notifyType);
|
||||||
if (NotifyType.TYPE_MAIL.equals(notifyType)) {
|
if (NotifyType.TYPE_MAIL.equals(notifyType)) {
|
||||||
sendMail(content);
|
sendMail(content);
|
||||||
} else if (NotifyType.TYPE_MSG.equals(notifyType)) {
|
} else if (NotifyType.TYPE_MSG.equals(notifyType)) {
|
||||||
sendSms(content);
|
sendSms(content);
|
||||||
} else if (NotifyType.TYPE_LOG.equals(notifyType)) {
|
} else if (NotifyType.TYPE_LOG.equals(notifyType)) {
|
||||||
logger.info("新通知:{}", content);
|
logger.info("新通知:{}", content);
|
||||||
|
} else if (NotifyType.TYPE_CUSTOM.equals(notifyType)) {
|
||||||
|
logger.warn("您选择了自定义通知,请实现CustomNotify接口并调用 notify(CustomNotify customNotify方法)");
|
||||||
} else {
|
} else {
|
||||||
logger.info("未匹配到通知类型:[{}]", content);
|
logger.info("未匹配到通知类型:[{}]", content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void notify(CustomNotify customNotify) {
|
||||||
|
logger.debug("执行自定义通知");
|
||||||
|
customNotify.onNotify();
|
||||||
|
}
|
||||||
|
|
||||||
private void sendSms(Object content) {
|
private void sendSms(Object content) {
|
||||||
logger.info("即将发送短信通知,通知内容:{}", content);
|
logger.info("即将发送短信通知,通知内容:{}", content);
|
||||||
}
|
}
|
||||||
|
@ -16,11 +16,6 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.hxuanyu</groupId>
|
<groupId>com.hxuanyu</groupId>
|
||||||
<artifactId>notify-spring-boot-starter</artifactId>
|
<artifactId>notify-spring-boot-starter</artifactId>
|
||||||
|
@ -0,0 +1,61 @@
|
|||||||
|
package com.hxuanyu.test.controller;
|
||||||
|
|
||||||
|
import com.hxuanyu.common.message.Msg;
|
||||||
|
import com.hxuanyu.monitor.base.BaseMonitorItem;
|
||||||
|
import com.hxuanyu.monitor.common.CheckResult;
|
||||||
|
import com.hxuanyu.monitor.manager.MonitorItemBeanManager;
|
||||||
|
import com.hxuanyu.notify.enums.NotifyType;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监控项测试控制器
|
||||||
|
*
|
||||||
|
* @author hanxuanyu
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
@RequestMapping("/monitor")
|
||||||
|
@RestController
|
||||||
|
public class MonitorTestController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
MonitorItemBeanManager monitorItemBeanManager;
|
||||||
|
|
||||||
|
@PutMapping("/")
|
||||||
|
public Msg<String> setCron(String taskId, String taskCron) {
|
||||||
|
if (taskId == null || taskCron == null) {
|
||||||
|
return Msg.failed("参数不匹配");
|
||||||
|
}
|
||||||
|
return monitorItemBeanManager.setMonitorTaskCron(taskId, taskCron);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/")
|
||||||
|
public Msg<String> deleteTask(String taskId) {
|
||||||
|
if (taskId == null) {
|
||||||
|
return Msg.failed("taskId 未填写");
|
||||||
|
}
|
||||||
|
return monitorItemBeanManager.deleteMonitorTask(taskId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/")
|
||||||
|
public Msg<Collection<BaseMonitorItem>> getTaskList() {
|
||||||
|
Map<String, BaseMonitorItem> monitorItemMap = monitorItemBeanManager.getMonitorItemMap();
|
||||||
|
return Msg.success("获取成功", monitorItemMap.values());
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/")
|
||||||
|
public Msg<String> addTaskList() {
|
||||||
|
return monitorItemBeanManager.addMonitorTask(new BaseMonitorItem("CustomBean", "0/10 * * * * *") {
|
||||||
|
@Override
|
||||||
|
public CheckResult check() {
|
||||||
|
return CheckResult.triggered("动态新增通知", NotifyType.TYPE_LOG);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,12 +1,11 @@
|
|||||||
package com.hxuanyu.test.controller;
|
package com.hxuanyu.test.controller;
|
||||||
|
|
||||||
import com.hxuanyu.common.message.Msg;
|
import com.hxuanyu.common.message.Msg;
|
||||||
|
import com.hxuanyu.monitor.manager.MonitorItemBeanManager;
|
||||||
import com.hxuanyu.notify.model.Mail;
|
import com.hxuanyu.notify.model.Mail;
|
||||||
import com.hxuanyu.notify.service.MailService;
|
import com.hxuanyu.notify.service.MailService;
|
||||||
import com.hxuanyu.notify.service.NotifyService;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Controller;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
@ -19,28 +18,9 @@ import javax.annotation.Resource;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class TestController {
|
public class NetworkTestController {
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(TestController.class);
|
|
||||||
@Resource
|
|
||||||
MailService mailService;
|
|
||||||
|
|
||||||
@RequestMapping("/testMail")
|
|
||||||
public Msg<Mail> testMail() {
|
|
||||||
Mail mail = new Mail();
|
|
||||||
mail.setContent("邮件内容");
|
|
||||||
mail.setSubject("邮件主题");
|
|
||||||
mail.setTo("2252193204@qq.com");
|
|
||||||
|
|
||||||
try {
|
|
||||||
mailService.sendMail(mail);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return Msg.success("发送邮件成功", mail);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(NetworkTestController.class);
|
||||||
|
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@GetMapping("/")
|
@GetMapping("/")
|
@ -4,17 +4,43 @@ import com.hxuanyu.monitor.annotation.MonitorItem;
|
|||||||
import com.hxuanyu.monitor.base.BaseMonitorItem;
|
import com.hxuanyu.monitor.base.BaseMonitorItem;
|
||||||
import com.hxuanyu.monitor.common.CheckResult;
|
import com.hxuanyu.monitor.common.CheckResult;
|
||||||
import com.hxuanyu.notify.enums.NotifyType;
|
import com.hxuanyu.notify.enums.NotifyType;
|
||||||
|
import com.hxuanyu.notify.model.Mail;
|
||||||
|
import com.hxuanyu.notify.service.NotifyService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 定时任务监控测试
|
* 定时任务监控测试
|
||||||
*
|
*
|
||||||
* @author hxuanyu
|
* @author hxuanyu
|
||||||
*/
|
*/
|
||||||
@MonitorItem(cron = "0/5 * * * * *")
|
@MonitorItem(cron = "0/20 * * * * *")
|
||||||
public class TestMonitorItem extends BaseMonitorItem {
|
public class TestMonitorItem extends BaseMonitorItem {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(TestMonitorItem.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CheckResult check() {
|
public CheckResult check() {
|
||||||
return CheckResult.triggered("Hello", NotifyType.TYPE_LOG);
|
|
||||||
|
double random = Math.random();
|
||||||
|
int result = (int) (random * NotifyType.values().length);
|
||||||
|
logger.info("随机索引值:{}", result);
|
||||||
|
if (NotifyType.values().length > 0) {
|
||||||
|
NotifyType notifyType = NotifyType.values()[result];
|
||||||
|
switch (notifyType) {
|
||||||
|
case TYPE_LOG:
|
||||||
|
return CheckResult.triggered("日志输出", NotifyType.TYPE_LOG);
|
||||||
|
case TYPE_CUSTOM:
|
||||||
|
return CheckResult.triggered(() -> logger.info("自定义通知"));
|
||||||
|
case TYPE_MAIL:
|
||||||
|
return CheckResult.triggered(new Mail("2252193204@qq.com", "测试邮件主题", "测试邮件内容"), NotifyType.TYPE_MAIL);
|
||||||
|
case TYPE_MSG:
|
||||||
|
return CheckResult.triggered("短信通知", NotifyType.TYPE_MSG);
|
||||||
|
default:
|
||||||
|
return CheckResult.triggered("默认通知", null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return CheckResult.nonTriggered();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,10 @@ public class NotifyTest {
|
|||||||
sleep(2000);
|
sleep(2000);
|
||||||
notifyService.notify("短信通知方式", NotifyType.TYPE_MSG);
|
notifyService.notify("短信通知方式", NotifyType.TYPE_MSG);
|
||||||
notifyService.notify("日志输出方式", NotifyType.TYPE_LOG);
|
notifyService.notify("日志输出方式", NotifyType.TYPE_LOG);
|
||||||
|
notifyService.notify(() -> {
|
||||||
|
// do some things
|
||||||
|
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user