Compare commits
2 Commits
0c074c046f
...
95b41916dc
Author | SHA1 | Date |
---|---|---|
hanxuanyu | 95b41916dc | |
hanxuanyu | cb418ba35a |
|
@ -0,0 +1,58 @@
|
||||||
|
## common-spring-boot-starter
|
||||||
|
|
||||||
|
### 简介
|
||||||
|
|
||||||
|
本模块封装了常用的工具类,对外提供基础的功能,当前包含:
|
||||||
|
|
||||||
|
- Msg:统一消息对象
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 引入
|
||||||
|
|
||||||
|
```java
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.hxuanyu</groupId>
|
||||||
|
<artifactId>common-spring-boot-starter</artifactId>
|
||||||
|
<version>1.0.4</version>
|
||||||
|
</dependency>
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 功能
|
||||||
|
|
||||||
|
#### Msg
|
||||||
|
|
||||||
|
用于规范方法间调用返回的结果,即方法无论调用成功还是失败,都以Msg对象作为返回值,实际的返回值封装在msg中,调用者可根据Msg的状态码进行状态判断,成功则从Msg对象中取值,失败则做相应的失败处理,避免了直接调用时可能出现的空指针问题。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
使用方式:
|
||||||
|
|
||||||
|
```java
|
||||||
|
public void test() {
|
||||||
|
Msg<String> msg = doSomeThing("args");
|
||||||
|
if (msg.isSuccess()) {
|
||||||
|
// do some things
|
||||||
|
} else if (msg.isFailed()) {
|
||||||
|
// do some things
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Msg<String> doSomeThing(String args) {
|
||||||
|
if (args != null) {
|
||||||
|
return Msg.success("your success msg", "your data");
|
||||||
|
} else {
|
||||||
|
return Msg.failed("your failed msg");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
- 如果方法调用成功,则在返回值中传入成功消息以及可选的返回对象,该对象为泛型,可以在声明方法时指定
|
||||||
|
- 如果方法调用失败,则在返回值中传入失败原因,**注意,失败时不可设置data字段,只能传入失败消息**
|
||||||
|
- 调用者可以根据错误码或者直接调用`msg.isSuccess()`方法判断是否调用成功,并对结果进行相应处理
|
||||||
|
- 在链式调用时,位于中间的方法不建议直接将上游请求到的msg结果传递给下游,应重新创建新的msg对象
|
|
@ -0,0 +1,111 @@
|
||||||
|
## monitor-spring-boot-starter
|
||||||
|
|
||||||
|
### 简介
|
||||||
|
|
||||||
|
- 本模块实现了一个定时监控并推送通知的服务,适用于处理一些定时监控并实时通知的场景,举个栗子,可以用来监控火车票,当检测到有票之后触发通知,通过配置好的通知方式告知我们,目前内置了邮件通知、日志输出方式,以及自定义通知。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 引入
|
||||||
|
|
||||||
|
```java
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.hxuanyu</groupId>
|
||||||
|
<artifactId>monitor-spring-boot-starter</artifactId>
|
||||||
|
<version>1.0.4</version>
|
||||||
|
</dependency>
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 使用
|
||||||
|
|
||||||
|
- 创建监控项
|
||||||
|
|
||||||
|
```java
|
||||||
|
@MonitorItem(cron = "0/20 * * * * *")
|
||||||
|
public class TestMonitorItem extends BaseMonitorItem {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(TestMonitorItem.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CheckResult check() {
|
||||||
|
|
||||||
|
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(new NotifyService.CustomNotify() {
|
||||||
|
@Override
|
||||||
|
public void onNotify() {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
> MonitorManager会在启动时扫描标注了`@MonitorItem`的类,并创建对应的实例,定时执行类中实现的`check()`方法,当方法返回`CheckResult`的`triggered`字段为`true`时,会自动执行`CheckResult`中传入的通知。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
- 新增任务
|
||||||
|
|
||||||
|
```java
|
||||||
|
Msg<String> msg = monitorItemBeanManager.addMonitorTask(new BaseMonitorItem("CustomBean", "0/10 * * * * *") {
|
||||||
|
@Override
|
||||||
|
public CheckResult check() {
|
||||||
|
return CheckResult.triggered("动态新增通知", NotifyType.TYPE_LOG);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
- 定时任务管理器:用于修改监控间隔、删除任务或者查看当前所有任务
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Resource
|
||||||
|
MonitorItemBeanManager monitorItemBeanManager;
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
- 查看任务列表
|
||||||
|
|
||||||
|
```java
|
||||||
|
Map<String, BaseMonitorItem> monitorItemMap = monitorItemBeanManager.getMonitorItemMap();
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
- 设置任务监控频率
|
||||||
|
|
||||||
|
```java
|
||||||
|
Msg<String> msg = monitorItemBeanManager.setMonitorTaskCron(taskId, taskCron);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
- 移除定时任务
|
||||||
|
|
||||||
|
```java
|
||||||
|
Msg<String> msg = monitorItemBeanManager.deleteMonitorTask(taskId);
|
||||||
|
```
|
||||||
|
|
|
@ -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,18 +50,30 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通知触发,需要传入通知信息
|
* 通知触发,需要传入通知信息
|
||||||
*
|
*
|
||||||
* @param notifyContent 通知内容
|
* @param notifyContent 通知内容
|
||||||
* @param notifyType 通知类型
|
* @param notifyType 通知类型
|
||||||
* @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) {
|
||||||
MONITOR_ITEM_MAP.remove(taskId);
|
if (MONITOR_ITEM_MAP.containsKey(taskId)) {
|
||||||
schedulingConfigurer.cancelTriggerTask(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) {
|
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()) {
|
||||||
logger.info("定时任务[{}]触发成功,发送通知:[{}]", taskId, checkResult.getNotifyContent());
|
if (NotifyType.TYPE_CUSTOM.equals(checkResult.getNotifyType())){
|
||||||
notifyService.notify(checkResult.getNotifyContent(), NotifyType.TYPE_MAIL);
|
logger.info("定时任务[{}]触发成功,执行自定义通知", taskId);
|
||||||
|
notifyService.notify(checkResult.getCustomNotify());
|
||||||
|
} else {
|
||||||
|
logger.info("定时任务[{}]触发成功,发送通知:[{}]", taskId, checkResult.getNotifyContent());
|
||||||
|
notifyService.notify(checkResult.getNotifyContent(), checkResult.getNotifyType());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, new CronTrigger(cron)));
|
}, new CronTrigger(cron)));
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
## network-spring-boot-starter
|
||||||
|
|
||||||
|
### 简介
|
||||||
|
|
||||||
|
本模块对`HttpClient`进行了封装,实现了GET、POST、PUT、DELETE等Http请求的同步和异步方法
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 引入
|
||||||
|
|
||||||
|
```java
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.hxuanyu</groupId>
|
||||||
|
<artifactId>network-spring-boot-starter</artifactId>
|
||||||
|
<version>1</version>
|
||||||
|
</dependency>
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 使用
|
||||||
|
|
||||||
|
- 引入`HttpService`对象
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Resource
|
||||||
|
HttpService httpService;
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
- 调用相关方法
|
||||||
|
|
||||||
|
```java
|
||||||
|
Msg<String> msg = httpService.doGet("https://baidu.com");
|
||||||
|
if (msg.isSuccess()) {
|
||||||
|
logger.info(msg.toString());
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
- 本模块依赖了common-spring-boot-starter模块,引入本模块后会自动引入common模块下的相关类,同时`HttpService`的方法返回值使用了common模块下的`Msg`统一封装
|
|
@ -0,0 +1,103 @@
|
||||||
|
## notify-spring-boot-starter
|
||||||
|
|
||||||
|
### 简介
|
||||||
|
|
||||||
|
本模块用于对用户进行通知,目前支持邮件通知,后续会加入短信等更多类型
|
||||||
|
|
||||||
|
- MAIL:对springboot的mail模块进行了封装,实现了一个邮件发送队列,并支持html作为邮件内容
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 引入
|
||||||
|
|
||||||
|
```java
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.hxuanyu</groupId>
|
||||||
|
<artifactId>notify-spring-boot-starter</artifactId>
|
||||||
|
<version>1.0.4</version>
|
||||||
|
</dependency>
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### 使用
|
||||||
|
|
||||||
|
#### Mail
|
||||||
|
|
||||||
|
- 引入`NotifyService`对象
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Resource
|
||||||
|
NotifyService notifyService;
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
- 添加Mail配置
|
||||||
|
|
||||||
|
```java
|
||||||
|
notify:
|
||||||
|
mail:
|
||||||
|
host: your mail host
|
||||||
|
protocol: smtp
|
||||||
|
default-encoding: UTF-8
|
||||||
|
password: your passwd
|
||||||
|
username: your account
|
||||||
|
port: 587
|
||||||
|
properties:
|
||||||
|
mail:
|
||||||
|
debug: false
|
||||||
|
stmp:
|
||||||
|
socketFactory:
|
||||||
|
class: javax.net.ssl.SSLSocketFactory
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
- 调用相关方法
|
||||||
|
|
||||||
|
```java
|
||||||
|
notifyService.notify(new Mail("2252193204@qq.com", "test subject", "test success"), NotifyType.TYPE_MAIL);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 短信和日志
|
||||||
|
|
||||||
|
- 引入`NotifyService`对象
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Resource
|
||||||
|
NotifyService notifyService;
|
||||||
|
```
|
||||||
|
|
||||||
|
- 调用相关方法
|
||||||
|
|
||||||
|
```java
|
||||||
|
notifyService.notify("短信通知方式", NotifyType.TYPE_MSG);
|
||||||
|
notifyService.notify("日志输出方式", NotifyType.TYPE_LOG);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 自定义
|
||||||
|
|
||||||
|
- 引入`NotifyService`对象
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Resource
|
||||||
|
NotifyService notifyService;
|
||||||
|
```
|
||||||
|
|
||||||
|
- 调用方法
|
||||||
|
|
||||||
|
```java
|
||||||
|
notifyService.notify(new NotifyService.CustomNotify() {
|
||||||
|
@Override
|
||||||
|
public void onNotify() {
|
||||||
|
// do some things
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
|
@ -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…
Reference in New Issue