Compare commits

...

5 Commits

11 changed files with 46 additions and 24 deletions

View File

@ -3,7 +3,7 @@
<parent> <parent>
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId> <artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
<groupId>com.hxuanyu</groupId> <groupId>com.hxuanyu</groupId>
<version>1.0.5</version> <version>1.0.7-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -3,7 +3,7 @@
<parent> <parent>
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId> <artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
<groupId>com.hxuanyu</groupId> <groupId>com.hxuanyu</groupId>
<version>1.0.5</version> <version>1.0.7-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -10,10 +10,11 @@ import com.hxuanyu.monitor.common.CheckResult;
* @version 1.0 * @version 1.0
*/ */
public abstract class BaseMonitorItem { public abstract class BaseMonitorItem {
private String monitorItemName; protected String monitorItemName;
private String cron; protected String cron;
public BaseMonitorItem() { public BaseMonitorItem() {
} }
public BaseMonitorItem(String monitorItemName, String cron) { public BaseMonitorItem(String monitorItemName, String cron) {
@ -21,6 +22,11 @@ public abstract class BaseMonitorItem {
this.cron = cron; this.cron = cron;
} }
public BaseMonitorItem(String cron) {
this.monitorItemName = getClass().getSimpleName();
this.cron = cron;
}
public String getMonitorItemName() { public String getMonitorItemName() {
return monitorItemName; return monitorItemName;
} }

View File

@ -1,6 +1,7 @@
package com.hxuanyu.monitor.manager; package com.hxuanyu.monitor.manager;
import com.hxuanyu.common.message.Msg; import com.hxuanyu.common.message.Msg;
import com.hxuanyu.common.spring.SpringContextUtil;
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;
@ -68,7 +69,7 @@ public class MonitorItemBeanManager implements ApplicationListener<ContextRefres
} }
public Msg<String> addMonitorTask(BaseMonitorItem item) { public Msg<String> addMonitorTask(BaseMonitorItem item) {
String taskId = "ScheduledTask-" + item.getMonitorItemName(); String taskId = item.getMonitorItemName();
if (MONITOR_ITEM_MAP.containsKey(taskId)) { if (MONITOR_ITEM_MAP.containsKey(taskId)) {
return Msg.failed("任务已经存在,请执行修改操作"); return Msg.failed("任务已经存在,请执行修改操作");
} }
@ -78,13 +79,22 @@ public class MonitorItemBeanManager implements ApplicationListener<ContextRefres
return Msg.success("添加成功"); return Msg.success("添加成功");
} }
public Msg<String> addMonitorTask(String itemName, Class<? extends BaseMonitorItem> clazz, Object... args) {
BaseMonitorItem item = SpringContextUtil.registerBean(itemName, clazz, args);
return addMonitorTask(item);
}
public Msg<String> addMonitorTask(Class<? extends BaseMonitorItem> clazz, Object... args) {
return addMonitorTask(clazz.getSimpleName(), clazz, args);
}
public Msg<String> 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); 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()); logger.info("修改定时任务:{}, 执行周期:{}", taskId,item.getCron());
return Msg.success("修改成功"); return Msg.success("修改成功");
} else { } else {
return Msg.failed("修改失败,该任务不存在"); return Msg.failed("修改失败,该任务不存在");
@ -107,7 +117,7 @@ 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())){ if (NotifyType.TYPE_CUSTOM.equals(checkResult.getNotifyType())) {
logger.info("定时任务[{}]触发成功,执行自定义通知", taskId); logger.info("定时任务[{}]触发成功,执行自定义通知", taskId);
notifyService.notify(checkResult.getCustomNotify()); notifyService.notify(checkResult.getCustomNotify());
} else { } else {

View File

@ -3,7 +3,7 @@
<parent> <parent>
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId> <artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
<groupId>com.hxuanyu</groupId> <groupId>com.hxuanyu</groupId>
<version>1.0.5</version> <version>1.0.7-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -3,7 +3,7 @@
<parent> <parent>
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId> <artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
<groupId>com.hxuanyu</groupId> <groupId>com.hxuanyu</groupId>
<version>1.0.5</version> <version>1.0.7-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -4,7 +4,7 @@
<groupId>com.hxuanyu</groupId> <groupId>com.hxuanyu</groupId>
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId> <artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
<version>1.0.5</version> <version>1.0.7-SNAPSHOT</version>
<name>hxuanyu-spring-boot-starter-parent</name> <name>hxuanyu-spring-boot-starter-parent</name>
<description>MonitorPushingParent</description> <description>MonitorPushingParent</description>
<packaging>pom</packaging> <packaging>pom</packaging>
@ -19,7 +19,7 @@
<connection>scm:git:https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter.git</connection> <connection>scm:git:https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter.git</connection>
<developerConnection>scm:git:https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter.git</developerConnection> <developerConnection>scm:git:https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter.git</developerConnection>
<url>https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter</url> <url>https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter</url>
<tag>v1.0.5</tag> <tag>v1.0.0</tag>
</scm> </scm>
<developers> <developers>
<developer> <developer>

View File

@ -3,7 +3,7 @@
<parent> <parent>
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId> <artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
<groupId>com.hxuanyu</groupId> <groupId>com.hxuanyu</groupId>
<version>1.0.5</version> <version>1.0.7-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -2,9 +2,10 @@ package com.hxuanyu.test.controller;
import com.hxuanyu.common.message.Msg; import com.hxuanyu.common.message.Msg;
import com.hxuanyu.monitor.base.BaseMonitorItem; import com.hxuanyu.monitor.base.BaseMonitorItem;
import com.hxuanyu.monitor.common.CheckResult;
import com.hxuanyu.monitor.manager.MonitorItemBeanManager; import com.hxuanyu.monitor.manager.MonitorItemBeanManager;
import com.hxuanyu.notify.enums.NotifyType; import com.hxuanyu.test.monitor.TestMonitorItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -21,6 +22,7 @@ import java.util.Map;
@RequestMapping("/monitor") @RequestMapping("/monitor")
@RestController @RestController
public class MonitorTestController { public class MonitorTestController {
private final Logger logger = LoggerFactory.getLogger(MonitorTestController.class);
@Resource @Resource
MonitorItemBeanManager monitorItemBeanManager; MonitorItemBeanManager monitorItemBeanManager;
@ -49,12 +51,7 @@ public class MonitorTestController {
@PostMapping("/") @PostMapping("/")
public Msg<String> addTaskList() { public Msg<String> addTaskList() {
return monitorItemBeanManager.addMonitorTask(new BaseMonitorItem("CustomBean", "0/10 * * * * *") { return monitorItemBeanManager.addMonitorTask(TestMonitorItem.class, "0/5 * * * * *");
@Override
public CheckResult check() {
return CheckResult.triggered("动态新增通知", NotifyType.TYPE_LOG);
}
});
} }

View File

@ -14,14 +14,21 @@ import org.slf4j.LoggerFactory;
* *
* @author hxuanyu * @author hxuanyu
*/ */
@MonitorItem(cron = "0/20 * * * * *")
public class TestMonitorItem extends BaseMonitorItem { public class TestMonitorItem extends BaseMonitorItem {
private final Logger logger = LoggerFactory.getLogger(TestMonitorItem.class); private final Logger logger = LoggerFactory.getLogger(TestMonitorItem.class);
public TestMonitorItem(String monitorItemName, String cron) {
super(monitorItemName, cron);
}
public TestMonitorItem(String cron) {
super(cron);
}
@Override @Override
public CheckResult check() { public CheckResult check() {
double random = Math.random(); double random = Math.random();
int result = (int) (random * NotifyType.values().length); int result = (int) (random * NotifyType.values().length);
logger.info("随机索引值:{}", result); logger.info("随机索引值:{}", result);

View File

@ -19,7 +19,9 @@ public class MonitorTest {
@Test @Test
public void testMonitorItemScan() { public void testMonitorItemScan() {
BaseMonitorItem testMonitorItem = monitorItemBeanManager.getMonitorItemMap().get("ScheduledTask-TestMonitorItem"); BaseMonitorItem testMonitorItem = monitorItemBeanManager.getMonitorItemMap().get("ScheduledTask-TestMonitorItem");
assert testMonitorItem.getMonitorItemName() != null; if (testMonitorItem != null){
logger.info("获取到的Bean{}", testMonitorItem.toString()); assert testMonitorItem.getMonitorItemName() != null;
logger.info("获取到的Bean{}", testMonitorItem.toString());
}
} }
} }