新增自定义通知类型以及通知bug修复
This commit is contained in:
@@ -16,11 +16,6 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<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;
|
||||
|
||||
import com.hxuanyu.common.message.Msg;
|
||||
import com.hxuanyu.monitor.manager.MonitorItemBeanManager;
|
||||
import com.hxuanyu.notify.model.Mail;
|
||||
import com.hxuanyu.notify.service.MailService;
|
||||
import com.hxuanyu.notify.service.NotifyService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@@ -19,28 +18,9 @@ import javax.annotation.Resource;
|
||||
*/
|
||||
|
||||
@RestController
|
||||
public class TestController {
|
||||
|
||||
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);
|
||||
}
|
||||
public class NetworkTestController {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(NetworkTestController.class);
|
||||
|
||||
@ResponseBody
|
||||
@GetMapping("/")
|
@@ -4,17 +4,43 @@ import com.hxuanyu.monitor.annotation.MonitorItem;
|
||||
import com.hxuanyu.monitor.base.BaseMonitorItem;
|
||||
import com.hxuanyu.monitor.common.CheckResult;
|
||||
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
|
||||
*/
|
||||
@MonitorItem(cron = "0/5 * * * * *")
|
||||
@MonitorItem(cron = "0/20 * * * * *")
|
||||
public class TestMonitorItem extends BaseMonitorItem {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(TestMonitorItem.class);
|
||||
|
||||
@Override
|
||||
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);
|
||||
notifyService.notify("短信通知方式", NotifyType.TYPE_MSG);
|
||||
notifyService.notify("日志输出方式", NotifyType.TYPE_LOG);
|
||||
notifyService.notify(() -> {
|
||||
// do some things
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user