Compare commits

..

5 Commits

11 changed files with 46 additions and 24 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -4,7 +4,7 @@
<groupId>com.hxuanyu</groupId>
<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>
<description>MonitorPushingParent</description>
<packaging>pom</packaging>
@ -19,7 +19,7 @@
<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>
<url>https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter</url>
<tag>v1.0.5</tag>
<tag>v1.0.0</tag>
</scm>
<developers>
<developer>

View File

@ -3,7 +3,7 @@
<parent>
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
<groupId>com.hxuanyu</groupId>
<version>1.0.5</version>
<version>1.0.7-SNAPSHOT</version>
</parent>
<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.monitor.base.BaseMonitorItem;
import com.hxuanyu.monitor.common.CheckResult;
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 javax.annotation.Resource;
@ -21,6 +22,7 @@ import java.util.Map;
@RequestMapping("/monitor")
@RestController
public class MonitorTestController {
private final Logger logger = LoggerFactory.getLogger(MonitorTestController.class);
@Resource
MonitorItemBeanManager monitorItemBeanManager;
@ -49,12 +51,7 @@ public class MonitorTestController {
@PostMapping("/")
public Msg<String> addTaskList() {
return monitorItemBeanManager.addMonitorTask(new BaseMonitorItem("CustomBean", "0/10 * * * * *") {
@Override
public CheckResult check() {
return CheckResult.triggered("动态新增通知", NotifyType.TYPE_LOG);
}
});
return monitorItemBeanManager.addMonitorTask(TestMonitorItem.class, "0/5 * * * * *");
}

View File

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

View File

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