Compare commits
	
		
			12 Commits
		
	
	
		
			v1.0.2
			...
			95b41916dc
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 95b41916dc | |||
| cb418ba35a | |||
| 0c074c046f | |||
| ca65b5e8fc | |||
| baccb20c3c | |||
| 8c47f75673 | |||
| 16df08bbe8 | |||
| c100c89f5a | |||
| 45ab626751 | |||
| 58d2ce559d | |||
| 6febbb049d | |||
| dab1b9fa55 | 
							
								
								
									
										56
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										56
									
								
								README.md
									
									
									
									
									
								
							@@ -2,9 +2,63 @@
 | 
			
		||||
 | 
			
		||||
本项目基于springboot进行开发,实现了一系列的spring-boot-starter,可以作为开发中的工具包进行使用。
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 模块划分
 | 
			
		||||
 | 
			
		||||
- common-spring-boot-starter:常用的基础类,比如用作消息流转的`Msg`以及一些工具类
 | 
			
		||||
- monitor-spring-boot-starter:监控工具包,可以实现定时监控某些数据并在触发条件后实时通知
 | 
			
		||||
- network-spring-boot-starter:网络工具,实现了一系列http请求发送方法,引入后即可快速发送http请求
 | 
			
		||||
- notify-spring-boot-starter:通知工具,集成了邮件通知、短信通知等功能
 | 
			
		||||
- notify-spring-boot-starter:通知工具,集成了邮件通知、短信通知等功能
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## 快速开始
 | 
			
		||||
 | 
			
		||||
### 1. 引入maven依赖
 | 
			
		||||
 | 
			
		||||
本项目已经上传到中央仓库,使用时在pom文件中添加如下依赖即可:
 | 
			
		||||
 | 
			
		||||
- network-spring-boot-starter
 | 
			
		||||
 | 
			
		||||
```xml
 | 
			
		||||
<!--network-spring-boot-starter-->
 | 
			
		||||
<dependency>
 | 
			
		||||
    <groupId>com.hxuanyu</groupId>
 | 
			
		||||
    <artifactId>network-spring-boot-starter</artifactId>
 | 
			
		||||
    <version>1.0.1</version>
 | 
			
		||||
</dependency>
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
- notify-spring-boot-starter
 | 
			
		||||
 | 
			
		||||
```xml
 | 
			
		||||
<!--notify-spring-boot-starter-->
 | 
			
		||||
<dependency>
 | 
			
		||||
    <groupId>com.hxuanyu</groupId>
 | 
			
		||||
    <artifactId>notify-spring-boot-starter</artifactId>
 | 
			
		||||
    <version>1.0.1</version>
 | 
			
		||||
</dependency>
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
- monitor-spring-boot-starter
 | 
			
		||||
 | 
			
		||||
```xml
 | 
			
		||||
<!--monitor-spring-boot-starter-->
 | 
			
		||||
<dependency>
 | 
			
		||||
    <groupId>com.hxuanyu</groupId>
 | 
			
		||||
    <artifactId>monitor-spring-boot-starter</artifactId>
 | 
			
		||||
    <version>1.0.1</version>
 | 
			
		||||
</dependency>
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
- monitor-spring-boot-starter
 | 
			
		||||
 | 
			
		||||
```xml
 | 
			
		||||
<!--monitor-spring-boot-starter-->
 | 
			
		||||
<dependency>
 | 
			
		||||
    <groupId>com.hxuanyu</groupId>
 | 
			
		||||
    <artifactId>monitor-spring-boot-starter</artifactId>
 | 
			
		||||
    <version>1.0.1</version>
 | 
			
		||||
</dependency>
 | 
			
		||||
```
 | 
			
		||||
							
								
								
									
										58
									
								
								common-spring-boot-starter/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								common-spring-boot-starter/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -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对象
 | 
			
		||||
@@ -3,7 +3,7 @@
 | 
			
		||||
    <parent>
 | 
			
		||||
        <artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
 | 
			
		||||
        <groupId>com.hxuanyu</groupId>
 | 
			
		||||
        <version>1.0.2</version>
 | 
			
		||||
        <version>1.0.4-SNAPSHOT</version>
 | 
			
		||||
    </parent>
 | 
			
		||||
    <modelVersion>4.0.0</modelVersion>
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										111
									
								
								monitor-spring-boot-starter/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										111
									
								
								monitor-spring-boot-starter/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -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);
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
@@ -3,7 +3,7 @@
 | 
			
		||||
    <parent>
 | 
			
		||||
        <artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
 | 
			
		||||
        <groupId>com.hxuanyu</groupId>
 | 
			
		||||
        <version>1.0.2</version>
 | 
			
		||||
        <version>1.0.4-SNAPSHOT</version>
 | 
			
		||||
    </parent>
 | 
			
		||||
    <modelVersion>4.0.0</modelVersion>
 | 
			
		||||
 | 
			
		||||
@@ -19,6 +19,10 @@
 | 
			
		||||
            <groupId>com.hxuanyu</groupId>
 | 
			
		||||
            <artifactId>notify-spring-boot-starter</artifactId>
 | 
			
		||||
        </dependency>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>com.hxuanyu</groupId>
 | 
			
		||||
            <artifactId>common-spring-boot-starter</artifactId>
 | 
			
		||||
        </dependency>
 | 
			
		||||
    </dependencies>
 | 
			
		||||
 | 
			
		||||
</project>
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
package com.hxuanyu.monitor.common;
 | 
			
		||||
 | 
			
		||||
import com.hxuanyu.notify.enums.NotifyType;
 | 
			
		||||
import com.hxuanyu.notify.service.NotifyService;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 触发器通知
 | 
			
		||||
@@ -13,21 +14,33 @@ public class CheckResult {
 | 
			
		||||
    private boolean triggered;
 | 
			
		||||
    private Object notifyContent;
 | 
			
		||||
    private NotifyType notifyType;
 | 
			
		||||
    private NotifyService.CustomNotify customNotify;
 | 
			
		||||
 | 
			
		||||
    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.notifyContent = notifyContent;
 | 
			
		||||
        this.notifyType = notifyType;
 | 
			
		||||
        this.customNotify = customNotify;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public NotifyService.CustomNotify getCustomNotify() {
 | 
			
		||||
        return customNotify;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setCustomNotify(NotifyService.CustomNotify customNotify) {
 | 
			
		||||
        this.customNotify = customNotify;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String toString() {
 | 
			
		||||
        return "Notify{" +
 | 
			
		||||
        return "CheckResult{" +
 | 
			
		||||
                "triggered=" + triggered +
 | 
			
		||||
                ", notifyContent='" + notifyContent + '\'' +
 | 
			
		||||
                ", notifyContent=" + notifyContent +
 | 
			
		||||
                ", notifyType=" + notifyType +
 | 
			
		||||
                ", customNotify=" + customNotify +
 | 
			
		||||
                '}';
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -37,18 +50,30 @@ public class CheckResult {
 | 
			
		||||
     * @return 通知对象
 | 
			
		||||
     */
 | 
			
		||||
    public static CheckResult nonTriggered() {
 | 
			
		||||
        return new CheckResult(false, null, null);
 | 
			
		||||
        return new CheckResult(false, null, null, null);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 通知触发,需要传入通知信息
 | 
			
		||||
     *
 | 
			
		||||
     * @param notifyContent 通知内容
 | 
			
		||||
     * @param notifyType 通知类型
 | 
			
		||||
     * @param notifyType    通知类型
 | 
			
		||||
     * @return 返回结果
 | 
			
		||||
     */
 | 
			
		||||
    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() {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
package com.hxuanyu.monitor.manager;
 | 
			
		||||
 | 
			
		||||
import com.hxuanyu.common.message.Msg;
 | 
			
		||||
import com.hxuanyu.monitor.annotation.MonitorItem;
 | 
			
		||||
import com.hxuanyu.monitor.base.BaseMonitorItem;
 | 
			
		||||
import com.hxuanyu.monitor.common.CheckResult;
 | 
			
		||||
@@ -58,31 +59,47 @@ public class MonitorItemBeanManager implements ApplicationListener<ContextRefres
 | 
			
		||||
                        item.setMonitorItemName(name);
 | 
			
		||||
                        item.setCron(cron);
 | 
			
		||||
                        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();
 | 
			
		||||
        if (MONITOR_ITEM_MAP.containsKey(taskId)) {
 | 
			
		||||
            return Msg.failed("任务已经存在,请执行修改操作");
 | 
			
		||||
        }
 | 
			
		||||
        MONITOR_ITEM_MAP.put(taskId, item);
 | 
			
		||||
        logger.info("添加定时任务:{}, 执行周期:{}", taskId, item.getCron());
 | 
			
		||||
        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)) {
 | 
			
		||||
            schedulingConfigurer.cancelTriggerTask(taskId);
 | 
			
		||||
            BaseMonitorItem item = MONITOR_ITEM_MAP.get(taskId);
 | 
			
		||||
            item.setCron(cron);
 | 
			
		||||
            addTask(taskId, item);
 | 
			
		||||
            logger.info("修改定时任务:{}, 执行周期:{}", taskId, item.getCron());
 | 
			
		||||
            return Msg.success("修改成功");
 | 
			
		||||
        } else {
 | 
			
		||||
            return Msg.failed("修改失败,该任务不存在");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void deleteMonitorTask(String taskId) {
 | 
			
		||||
        MONITOR_ITEM_MAP.remove(taskId);
 | 
			
		||||
        schedulingConfigurer.cancelTriggerTask(taskId);
 | 
			
		||||
    public Msg<String> deleteMonitorTask(String taskId) {
 | 
			
		||||
        if (MONITOR_ITEM_MAP.containsKey(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) {
 | 
			
		||||
@@ -90,8 +107,13 @@ public class MonitorItemBeanManager implements ApplicationListener<ContextRefres
 | 
			
		||||
        schedulingConfigurer.resetTriggerTask(taskId, new TriggerTask(() -> {
 | 
			
		||||
            CheckResult checkResult = item.check();
 | 
			
		||||
            if (checkResult.isTriggered()) {
 | 
			
		||||
                logger.info("定时任务[{}]触发成功,发送通知:[{}]", taskId, checkResult.getNotifyContent());
 | 
			
		||||
                notifyService.notify(checkResult.getNotifyContent(), NotifyType.MAIL_TYPE);
 | 
			
		||||
                if (NotifyType.TYPE_CUSTOM.equals(checkResult.getNotifyType())){
 | 
			
		||||
                    logger.info("定时任务[{}]触发成功,执行自定义通知", taskId);
 | 
			
		||||
                    notifyService.notify(checkResult.getCustomNotify());
 | 
			
		||||
                } else {
 | 
			
		||||
                    logger.info("定时任务[{}]触发成功,发送通知:[{}]", taskId, checkResult.getNotifyContent());
 | 
			
		||||
                    notifyService.notify(checkResult.getNotifyContent(), checkResult.getNotifyType());
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }, new CronTrigger(cron)));
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										44
									
								
								network-spring-boot-starter/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								network-spring-boot-starter/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -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`统一封装
 | 
			
		||||
@@ -3,7 +3,7 @@
 | 
			
		||||
    <parent>
 | 
			
		||||
        <artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
 | 
			
		||||
        <groupId>com.hxuanyu</groupId>
 | 
			
		||||
        <version>1.0.2</version>
 | 
			
		||||
        <version>1.0.4-SNAPSHOT</version>
 | 
			
		||||
    </parent>
 | 
			
		||||
    <modelVersion>4.0.0</modelVersion>
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -277,18 +277,16 @@ public interface HttpService {
 | 
			
		||||
     *
 | 
			
		||||
     * @param url 请求地址
 | 
			
		||||
     * @return 统一返回报文
 | 
			
		||||
     * @throws Exception 异常
 | 
			
		||||
     */
 | 
			
		||||
    Msg<String> doDelete(String url) throws Exception;
 | 
			
		||||
    Msg<String> doDelete(String url);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 发送delete请求;不带请求参数
 | 
			
		||||
     *
 | 
			
		||||
     * @param url 请求地址
 | 
			
		||||
     * @return 统一返回报文
 | 
			
		||||
     * @throws Exception 异常
 | 
			
		||||
     */
 | 
			
		||||
    Msg<HttpEntity> doDeleteWithEntity(String url) throws Exception;
 | 
			
		||||
    Msg<HttpEntity> doDeleteWithEntity(String url);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 发送delete请求;带请求参数
 | 
			
		||||
@@ -296,9 +294,8 @@ public interface HttpService {
 | 
			
		||||
     * @param url    请求地址
 | 
			
		||||
     * @param params 请求参数
 | 
			
		||||
     * @return 统一返回报文
 | 
			
		||||
     * @throws Exception 异常
 | 
			
		||||
     */
 | 
			
		||||
    Msg<String> doDelete(String url, Map<String, String> params) throws Exception;
 | 
			
		||||
    Msg<String> doDelete(String url, Map<String, String> params);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -307,9 +304,8 @@ public interface HttpService {
 | 
			
		||||
     * @param url    请求地址
 | 
			
		||||
     * @param params 请求参数
 | 
			
		||||
     * @return 统一返回报文
 | 
			
		||||
     * @throws Exception 异常
 | 
			
		||||
     */
 | 
			
		||||
    Msg<HttpEntity> doDeleteWithEntity(String url, Map<String, String> params) throws Exception;
 | 
			
		||||
    Msg<HttpEntity> doDeleteWithEntity(String url, Map<String, String> params);
 | 
			
		||||
 | 
			
		||||
    interface NetWorkListener<T> {
 | 
			
		||||
        /**
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										103
									
								
								notify-spring-boot-starter/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										103
									
								
								notify-spring-boot-starter/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -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
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
});
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
@@ -3,7 +3,7 @@
 | 
			
		||||
    <parent>
 | 
			
		||||
        <artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
 | 
			
		||||
        <groupId>com.hxuanyu</groupId>
 | 
			
		||||
        <version>1.0.2</version>
 | 
			
		||||
        <version>1.0.4-SNAPSHOT</version>
 | 
			
		||||
    </parent>
 | 
			
		||||
    <modelVersion>4.0.0</modelVersion>
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,108 @@
 | 
			
		||||
package com.hxuanyu.notify.config;
 | 
			
		||||
 | 
			
		||||
import java.nio.charset.Charset;
 | 
			
		||||
import java.nio.charset.StandardCharsets;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 | 
			
		||||
import org.springframework.boot.context.properties.ConfigurationProperties;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author hxuanyu
 | 
			
		||||
 */
 | 
			
		||||
@ConfigurationProperties(prefix = "notify.mail")
 | 
			
		||||
@ConditionalOnProperty(prefix = "notify.mail", havingValue = "true")
 | 
			
		||||
public class MailProperties {
 | 
			
		||||
    private static final Charset DEFAULT_CHARSET;
 | 
			
		||||
    private String host;
 | 
			
		||||
    private Integer port;
 | 
			
		||||
    private String username;
 | 
			
		||||
    private String password;
 | 
			
		||||
    private String protocol = "smtp";
 | 
			
		||||
    private Charset defaultEncoding;
 | 
			
		||||
    private Map<String, String> properties;
 | 
			
		||||
    private String jndiName;
 | 
			
		||||
    private Integer interval = 10000;
 | 
			
		||||
 | 
			
		||||
    public MailProperties() {
 | 
			
		||||
        this.defaultEncoding = DEFAULT_CHARSET;
 | 
			
		||||
        this.properties = new HashMap();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setProperties(Map<String, String> properties) {
 | 
			
		||||
        this.properties = properties;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Integer getInterval() {
 | 
			
		||||
        return interval;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setInterval(Integer interval) {
 | 
			
		||||
        this.interval = interval;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getHost() {
 | 
			
		||||
        return this.host;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setHost(String host) {
 | 
			
		||||
        this.host = host;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Integer getPort() {
 | 
			
		||||
        return this.port;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setPort(Integer port) {
 | 
			
		||||
        this.port = port;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getUsername() {
 | 
			
		||||
        return this.username;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setUsername(String username) {
 | 
			
		||||
        this.username = username;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getPassword() {
 | 
			
		||||
        return this.password;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setPassword(String password) {
 | 
			
		||||
        this.password = password;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getProtocol() {
 | 
			
		||||
        return this.protocol;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setProtocol(String protocol) {
 | 
			
		||||
        this.protocol = protocol;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Charset getDefaultEncoding() {
 | 
			
		||||
        return this.defaultEncoding;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setDefaultEncoding(Charset defaultEncoding) {
 | 
			
		||||
        this.defaultEncoding = defaultEncoding;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Map<String, String> getProperties() {
 | 
			
		||||
        return this.properties;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setJndiName(String jndiName) {
 | 
			
		||||
        this.jndiName = jndiName;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getJndiName() {
 | 
			
		||||
        return this.jndiName;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static {
 | 
			
		||||
        DEFAULT_CHARSET = StandardCharsets.UTF_8;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,18 +1,57 @@
 | 
			
		||||
package com.hxuanyu.notify.config;
 | 
			
		||||
 | 
			
		||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
 | 
			
		||||
import org.springframework.context.annotation.Bean;
 | 
			
		||||
import org.springframework.context.annotation.ComponentScan;
 | 
			
		||||
import org.springframework.context.annotation.Configuration;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
 | 
			
		||||
import org.springframework.context.annotation.*;
 | 
			
		||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
 | 
			
		||||
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Properties;
 | 
			
		||||
import java.util.concurrent.*;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author hxuanyu
 | 
			
		||||
 */
 | 
			
		||||
@Configuration
 | 
			
		||||
@ComponentScan("com.hxuanyu.notify.service")
 | 
			
		||||
@ComponentScan("com.hxuanyu.notify")
 | 
			
		||||
@EnableConfigurationProperties(MailProperties.class)
 | 
			
		||||
public class NotifyConfiguration {
 | 
			
		||||
    @Autowired
 | 
			
		||||
    MailProperties mailProperties;
 | 
			
		||||
 | 
			
		||||
    @Bean
 | 
			
		||||
    JavaMailSenderImpl mailSender(MailProperties properties) {
 | 
			
		||||
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
 | 
			
		||||
        this.applyProperties(properties, sender);
 | 
			
		||||
        return sender;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void applyProperties(MailProperties properties, JavaMailSenderImpl sender) {
 | 
			
		||||
        sender.setHost(properties.getHost());
 | 
			
		||||
        if (properties.getPort() != null) {
 | 
			
		||||
            sender.setPort(properties.getPort());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        sender.setUsername(properties.getUsername());
 | 
			
		||||
        sender.setPassword(properties.getPassword());
 | 
			
		||||
        sender.setProtocol(properties.getProtocol());
 | 
			
		||||
        if (properties.getDefaultEncoding() != null) {
 | 
			
		||||
            sender.setDefaultEncoding(properties.getDefaultEncoding().name());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (!properties.getProperties().isEmpty()) {
 | 
			
		||||
            sender.setJavaMailProperties(this.asProperties(properties.getProperties()));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private Properties asProperties(Map<String, String> source) {
 | 
			
		||||
        Properties properties = new Properties();
 | 
			
		||||
        properties.putAll(source);
 | 
			
		||||
        return properties;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Bean(name = "mailExecutorService")
 | 
			
		||||
    public ExecutorService mailExecutorService() {
 | 
			
		||||
        // 使用 ThreadFactoryBuilder 创建自定义线程名称的 ThreadFactory
 | 
			
		||||
 
 | 
			
		||||
@@ -10,10 +10,12 @@ public enum NotifyType {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 邮件类型
 | 
			
		||||
     * 通知类型
 | 
			
		||||
     */
 | 
			
		||||
    MAIL_TYPE("邮件"),
 | 
			
		||||
    SMS_TYPE("短信");
 | 
			
		||||
    TYPE_LOG("日志输出"),
 | 
			
		||||
    TYPE_MAIL("邮件"),
 | 
			
		||||
    TYPE_CUSTOM("自定义"),
 | 
			
		||||
    TYPE_MSG("短信");
 | 
			
		||||
 | 
			
		||||
    private final String typeName;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,6 @@
 | 
			
		||||
package com.hxuanyu.notify.service;
 | 
			
		||||
 | 
			
		||||
import com.hxuanyu.notify.enums.NotifyType;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 通知服务
 | 
			
		||||
@@ -17,4 +16,18 @@ public interface NotifyService {
 | 
			
		||||
     * @param notifyType 通知类型
 | 
			
		||||
     */
 | 
			
		||||
    void notify(Object content, NotifyType notifyType);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 自定义通知
 | 
			
		||||
     *
 | 
			
		||||
     * @param customNotify 自定义通知接口,需手动实现
 | 
			
		||||
     */
 | 
			
		||||
    void notify(CustomNotify customNotify);
 | 
			
		||||
 | 
			
		||||
    interface CustomNotify {
 | 
			
		||||
        /**
 | 
			
		||||
         * 通知
 | 
			
		||||
         */
 | 
			
		||||
        void onNotify();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,18 +2,19 @@ package com.hxuanyu.notify.service.impl;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
import com.hxuanyu.notify.common.MailQueue;
 | 
			
		||||
import com.hxuanyu.notify.config.MailProperties;
 | 
			
		||||
import com.hxuanyu.notify.model.Mail;
 | 
			
		||||
import com.hxuanyu.notify.service.MailService;
 | 
			
		||||
import org.slf4j.Logger;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Qualifier;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Value;
 | 
			
		||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
 | 
			
		||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 | 
			
		||||
import org.springframework.mail.javamail.JavaMailSender;
 | 
			
		||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
 | 
			
		||||
import org.springframework.mail.javamail.MimeMessageHelper;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.thymeleaf.TemplateEngine;
 | 
			
		||||
import org.thymeleaf.context.Context;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.PostConstruct;
 | 
			
		||||
import javax.annotation.PreDestroy;
 | 
			
		||||
@@ -27,15 +28,14 @@ import java.util.concurrent.ExecutorService;
 | 
			
		||||
 */
 | 
			
		||||
@Service
 | 
			
		||||
public class MailServiceImpl implements MailService {
 | 
			
		||||
    @Resource
 | 
			
		||||
    MailProperties mailProperties;
 | 
			
		||||
    private static final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
 | 
			
		||||
    private static boolean isRunning = true;
 | 
			
		||||
    ExecutorService executor;
 | 
			
		||||
    @Resource
 | 
			
		||||
    private JavaMailSender javaMailSender;
 | 
			
		||||
 | 
			
		||||
    private static JavaMailSender javaMailSender;
 | 
			
		||||
    private static TemplateEngine templateEngine;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    private static String defaultFrom;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -45,7 +45,7 @@ public class MailServiceImpl implements MailService {
 | 
			
		||||
        executor.submit(new PollMail());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static class PollMail implements Runnable {
 | 
			
		||||
    class PollMail implements Runnable {
 | 
			
		||||
        @Override
 | 
			
		||||
        public void run() {
 | 
			
		||||
            while (isRunning) {
 | 
			
		||||
@@ -55,7 +55,7 @@ public class MailServiceImpl implements MailService {
 | 
			
		||||
                    if (mail != null) {
 | 
			
		||||
                        //可以设置延时 以及重复校验等等操作
 | 
			
		||||
                        sendMailSync(mail);
 | 
			
		||||
                        Thread.sleep(10000);
 | 
			
		||||
                        Thread.sleep(mailProperties.getInterval());
 | 
			
		||||
                    }
 | 
			
		||||
                } catch (Exception e) {
 | 
			
		||||
                    e.printStackTrace();
 | 
			
		||||
@@ -71,10 +71,14 @@ public class MailServiceImpl implements MailService {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    private static void sendMailSync(Mail mail) {
 | 
			
		||||
    private void sendMailSync(Mail mail) {
 | 
			
		||||
        if (mailProperties == null){
 | 
			
		||||
            logger.error("未配置邮件信息,请在配置文件中添加配置信息后再发送邮件");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        String from = mail.getFrom();
 | 
			
		||||
        if (from == null) {
 | 
			
		||||
            from = defaultFrom;
 | 
			
		||||
            from = mailProperties.getUsername();
 | 
			
		||||
            logger.info("未传入发件人,从配置中读取:{}", from);
 | 
			
		||||
        }
 | 
			
		||||
        MimeMessage mimeMessage;
 | 
			
		||||
@@ -87,12 +91,7 @@ public class MailServiceImpl implements MailService {
 | 
			
		||||
            mimeMessageHelper.setFrom(from);
 | 
			
		||||
            mimeMessageHelper.setTo(mail.getTo());
 | 
			
		||||
            mimeMessageHelper.setSubject(mail.getSubject());
 | 
			
		||||
            // 利用 Thymeleaf 引擎渲染 HTML
 | 
			
		||||
            Context context = new Context();
 | 
			
		||||
            // 设置注入的变量
 | 
			
		||||
            context.setVariable("templates/mail", mail);
 | 
			
		||||
            // 模板设置为 "mail"
 | 
			
		||||
            String content = templateEngine.process("templates/mail/mail", context);
 | 
			
		||||
            String content = mail.getContent();
 | 
			
		||||
            // 设置邮件内容
 | 
			
		||||
            // true 表示开启 html
 | 
			
		||||
            mimeMessageHelper.setText(content, true);
 | 
			
		||||
@@ -112,24 +111,9 @@ public class MailServiceImpl implements MailService {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Resource
 | 
			
		||||
    public void setJavaMailSender(JavaMailSender javaMailSender) {
 | 
			
		||||
        MailServiceImpl.javaMailSender = javaMailSender;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    public void setExecutor(@Qualifier("mailExecutorService") ExecutorService executor) {
 | 
			
		||||
        this.executor = executor;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    public void setTemplateEngine(TemplateEngine templateEngine) {
 | 
			
		||||
        MailServiceImpl.templateEngine = templateEngine;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Value("${spring.mail.username}")
 | 
			
		||||
    public void setDefaultFrom(String defaultFrom) {
 | 
			
		||||
        MailServiceImpl.defaultFrom = defaultFrom;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -25,15 +25,26 @@ public class NotifyServiceImpl implements NotifyService {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void notify(Object content, NotifyType notifyType) {
 | 
			
		||||
        if (NotifyType.MAIL_TYPE.equals(notifyType)) {
 | 
			
		||||
        logger.debug("通知内容:{},通知类型:{}", notifyType, notifyType);
 | 
			
		||||
        if (NotifyType.TYPE_MAIL.equals(notifyType)) {
 | 
			
		||||
            sendMail(content);
 | 
			
		||||
        } else if (NotifyType.SMS_TYPE.equals(notifyType)) {
 | 
			
		||||
        } else if (NotifyType.TYPE_MSG.equals(notifyType)) {
 | 
			
		||||
            sendSms(content);
 | 
			
		||||
        } else if (NotifyType.TYPE_LOG.equals(notifyType)) {
 | 
			
		||||
            logger.info("新通知:{}", content);
 | 
			
		||||
        } else if (NotifyType.TYPE_CUSTOM.equals(notifyType)) {
 | 
			
		||||
            logger.warn("您选择了自定义通知,请实现CustomNotify接口并调用 notify(CustomNotify customNotify方法)");
 | 
			
		||||
        } else {
 | 
			
		||||
            logger.info("未匹配到通知类型:[{}]", content);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void notify(CustomNotify customNotify) {
 | 
			
		||||
        logger.debug("执行自定义通知");
 | 
			
		||||
        customNotify.onNotify();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void sendSms(Object content) {
 | 
			
		||||
        logger.info("即将发送短信通知,通知内容:{}", content);
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,48 @@
 | 
			
		||||
{
 | 
			
		||||
  "properties": [
 | 
			
		||||
    {
 | 
			
		||||
      "name": "notify.mail.host",
 | 
			
		||||
      "type": "java.lang.String",
 | 
			
		||||
      "description": "Description for notify.mail.host."
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "name": "notify.mail.protocol",
 | 
			
		||||
      "type": "java.lang.String",
 | 
			
		||||
      "description": "Description for notify.mail.protocol."
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "name": "notify.mail.default-encoding",
 | 
			
		||||
      "type": "java.lang.String",
 | 
			
		||||
      "description": "Description for notify.mail.default-encoding."
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "name": "notify.mail.password",
 | 
			
		||||
      "type": "java.lang.String",
 | 
			
		||||
      "description": "Description for notify.mail.password."
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "name": "notify.mail.username",
 | 
			
		||||
      "type": "java.lang.String",
 | 
			
		||||
      "description": "Description for notify.mail.username."
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "name": "notify.mail.port",
 | 
			
		||||
      "type": "java.lang.String",
 | 
			
		||||
      "description": "Description for notify.mail.port."
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "name": "notify.mail.properties.mail.debug",
 | 
			
		||||
      "type": "java.lang.String",
 | 
			
		||||
      "description": "Description for notify.mail.properties.mail.debug."
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "name": "notify.mail.properties.mail.interval",
 | 
			
		||||
      "type": "java.lang.String",
 | 
			
		||||
      "description": "Description for notify.mail.properties.mail.debug."
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      "name": "notify.mail.properties.stmp.socketFactory.class",
 | 
			
		||||
      "type": "java.lang.String",
 | 
			
		||||
      "description": "Description for notify.mail.properties.stmp.socketFactory.class."
 | 
			
		||||
    }
 | 
			
		||||
  ] }
 | 
			
		||||
@@ -1,129 +0,0 @@
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
 | 
			
		||||
<!--${government}-->
 | 
			
		||||
<!--${title}-->
 | 
			
		||||
<!--${suggestion}-->
 | 
			
		||||
<!--${deadline}-->
 | 
			
		||||
<!--${secret}-->
 | 
			
		||||
<!--${url}-->
 | 
			
		||||
<!--${officeName}-->
 | 
			
		||||
<!--${createTime}-->
 | 
			
		||||
<head>
 | 
			
		||||
    <meta charset="utf-8">
 | 
			
		||||
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
 | 
			
		||||
    <title>${title}</title>
 | 
			
		||||
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
 | 
			
		||||
    <style>
 | 
			
		||||
        .contact-clean {
 | 
			
		||||
            background: #f1f7fc;
 | 
			
		||||
            padding: 80px 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        a {
 | 
			
		||||
            text-decoration: none;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @media (max-width: 767px) {
 | 
			
		||||
            .contact-clean {
 | 
			
		||||
                padding: 20px 0;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        .contact-clean form {
 | 
			
		||||
            max-width: 480px;
 | 
			
		||||
            width: 90%;
 | 
			
		||||
            margin: 0 auto;
 | 
			
		||||
            background-color: #ffffff;
 | 
			
		||||
            padding: 40px;
 | 
			
		||||
            border-radius: 4px;
 | 
			
		||||
            color: #505e6c;
 | 
			
		||||
            box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.1);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @media (max-width: 767px) {
 | 
			
		||||
            .contact-clean form {
 | 
			
		||||
                padding: 30px;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        .contact-clean h2 {
 | 
			
		||||
            margin-top: 5px;
 | 
			
		||||
            font-weight: bold;
 | 
			
		||||
            font-size: 28px;
 | 
			
		||||
            margin-bottom: 36px;
 | 
			
		||||
            color: inherit;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        .contact-clean .form-group:last-child {
 | 
			
		||||
            margin-bottom: 5px;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        .contact-clean form .form-control {
 | 
			
		||||
            background: #fff;
 | 
			
		||||
            border-radius: 2px;
 | 
			
		||||
            box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.05);
 | 
			
		||||
            outline: none;
 | 
			
		||||
            color: inherit;
 | 
			
		||||
            padding-left: 12px;
 | 
			
		||||
            height: 42px;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        .contact-clean form .form-control:focus {
 | 
			
		||||
            border: 1px solid #b2b2b2;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        .contact-clean form textarea.form-control {
 | 
			
		||||
            min-height: 100px;
 | 
			
		||||
            max-height: 260px;
 | 
			
		||||
            padding-top: 10px;
 | 
			
		||||
            resize: vertical;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        .contact-clean form .btn {
 | 
			
		||||
            padding: 16px 32px;
 | 
			
		||||
            border: none;
 | 
			
		||||
            background: none;
 | 
			
		||||
            box-shadow: none;
 | 
			
		||||
            text-shadow: none;
 | 
			
		||||
            opacity: 0.9;
 | 
			
		||||
            text-transform: uppercase;
 | 
			
		||||
            font-weight: bold;
 | 
			
		||||
            font-size: 13px;
 | 
			
		||||
            letter-spacing: 0.4px;
 | 
			
		||||
            line-height: 1;
 | 
			
		||||
            outline: none !important;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        .contact-clean form .btn:hover {
 | 
			
		||||
            opacity: 1;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        .contact-clean form .btn:active {
 | 
			
		||||
            transform: translateY(1px);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        .contact-clean form .btn-primary {
 | 
			
		||||
            background-color: #055ada !important;
 | 
			
		||||
            margin-top: 15px;
 | 
			
		||||
            color: #fff;
 | 
			
		||||
        }
 | 
			
		||||
    </style>
 | 
			
		||||
</head>
 | 
			
		||||
 | 
			
		||||
<body>
 | 
			
		||||
<div class="contact-clean">
 | 
			
		||||
    <form method="post">
 | 
			
		||||
        <h2 class="text-center" th:text="${mail.getSubject()}"></h2>
 | 
			
		||||
        <p th:utext="${mail.getContent()}"></p>
 | 
			
		||||
        <div class="alert alert-danger" role="alert">
 | 
			
		||||
            <span>
 | 
			
		||||
                <strong>注意</strong>本邮件由系统自动发送,请勿回复本邮件,如果邮件内容您并不知情,请忽略本邮件
 | 
			
		||||
            </span>
 | 
			
		||||
        </div>
 | 
			
		||||
    </form>
 | 
			
		||||
</div>
 | 
			
		||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
 | 
			
		||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
 | 
			
		||||
</body>
 | 
			
		||||
 | 
			
		||||
</html>
 | 
			
		||||
							
								
								
									
										7
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								pom.xml
									
									
									
									
									
								
							@@ -4,7 +4,7 @@
 | 
			
		||||
 | 
			
		||||
    <groupId>com.hxuanyu</groupId>
 | 
			
		||||
    <artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
 | 
			
		||||
    <version>1.0.2</version>
 | 
			
		||||
    <version>1.0.4-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.2</tag>
 | 
			
		||||
        <tag>v1.0.0</tag>
 | 
			
		||||
    </scm>
 | 
			
		||||
    <developers>
 | 
			
		||||
        <developer>
 | 
			
		||||
@@ -34,6 +34,7 @@
 | 
			
		||||
        <module>network-spring-boot-starter</module>
 | 
			
		||||
        <module>notify-spring-boot-starter</module>
 | 
			
		||||
        <module>monitor-spring-boot-starter</module>
 | 
			
		||||
        <module>test</module>
 | 
			
		||||
    </modules>
 | 
			
		||||
    <parent>
 | 
			
		||||
        <groupId>org.springframework.boot</groupId>
 | 
			
		||||
@@ -165,7 +166,7 @@
 | 
			
		||||
                            <tagNameFormat>v@{project.version}</tagNameFormat>
 | 
			
		||||
                            <autoVersionSubmodules>true</autoVersionSubmodules>
 | 
			
		||||
                            <password>${git.password}</password>
 | 
			
		||||
                            <username>hxuanyu</username>
 | 
			
		||||
                            <username>${git.username}</username>
 | 
			
		||||
                        </configuration>
 | 
			
		||||
                    </plugin>
 | 
			
		||||
                </plugins>
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										61
									
								
								test/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								test/pom.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,61 @@
 | 
			
		||||
<?xml version="1.0" encoding="UTF-8"?>
 | 
			
		||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
 | 
			
		||||
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 | 
			
		||||
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 | 
			
		||||
    <parent>
 | 
			
		||||
        <artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
 | 
			
		||||
        <groupId>com.hxuanyu</groupId>
 | 
			
		||||
        <version>1.0.4-SNAPSHOT</version>
 | 
			
		||||
    </parent>
 | 
			
		||||
    <modelVersion>4.0.0</modelVersion>
 | 
			
		||||
 | 
			
		||||
    <artifactId>test</artifactId>
 | 
			
		||||
 | 
			
		||||
    <dependencies>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>org.springframework.boot</groupId>
 | 
			
		||||
            <artifactId>spring-boot-starter-web</artifactId>
 | 
			
		||||
        </dependency>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>com.hxuanyu</groupId>
 | 
			
		||||
            <artifactId>notify-spring-boot-starter</artifactId>
 | 
			
		||||
        </dependency>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>com.hxuanyu</groupId>
 | 
			
		||||
            <artifactId>common-spring-boot-starter</artifactId>
 | 
			
		||||
        </dependency>
 | 
			
		||||
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>com.hxuanyu</groupId>
 | 
			
		||||
            <artifactId>monitor-spring-boot-starter</artifactId>
 | 
			
		||||
        </dependency>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>com.hxuanyu</groupId>
 | 
			
		||||
            <artifactId>network-spring-boot-starter</artifactId>
 | 
			
		||||
        </dependency>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>junit</groupId>
 | 
			
		||||
            <artifactId>junit</artifactId>
 | 
			
		||||
            <scope>test</scope>
 | 
			
		||||
        </dependency>
 | 
			
		||||
    </dependencies>
 | 
			
		||||
 | 
			
		||||
    <properties>
 | 
			
		||||
        <maven.compiler.source>8</maven.compiler.source>
 | 
			
		||||
        <maven.compiler.target>8</maven.compiler.target>
 | 
			
		||||
    </properties>
 | 
			
		||||
 | 
			
		||||
    <build>
 | 
			
		||||
        <plugins>
 | 
			
		||||
            <plugin>
 | 
			
		||||
                <groupId>org.apache.maven.plugins</groupId>
 | 
			
		||||
                <artifactId>maven-deploy-plugin</artifactId>
 | 
			
		||||
                <version>2.8.2</version>
 | 
			
		||||
                <configuration>
 | 
			
		||||
                    <skip>true</skip>
 | 
			
		||||
                </configuration>
 | 
			
		||||
            </plugin>
 | 
			
		||||
        </plugins>
 | 
			
		||||
    </build>
 | 
			
		||||
 | 
			
		||||
</project>
 | 
			
		||||
							
								
								
									
										17
									
								
								test/src/main/java/com/hxuanyu/test/MainApplication.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								test/src/main/java/com/hxuanyu/test/MainApplication.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
package com.hxuanyu.test;
 | 
			
		||||
 | 
			
		||||
import org.springframework.boot.SpringApplication;
 | 
			
		||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * TODO
 | 
			
		||||
 *
 | 
			
		||||
 * @author hanxuanyu
 | 
			
		||||
 * @version 1.0
 | 
			
		||||
 */
 | 
			
		||||
@SpringBootApplication
 | 
			
		||||
public class MainApplication {
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
        SpringApplication.run(MainApplication.class, args);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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);
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,52 @@
 | 
			
		||||
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 org.slf4j.Logger;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
import org.springframework.web.bind.annotation.*;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Resource;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 测试控制器
 | 
			
		||||
 *
 | 
			
		||||
 * @author hanxuanyu
 | 
			
		||||
 * @version 1.0
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
@RestController
 | 
			
		||||
public class NetworkTestController {
 | 
			
		||||
 | 
			
		||||
    private final Logger logger = LoggerFactory.getLogger(NetworkTestController.class);
 | 
			
		||||
 | 
			
		||||
    @ResponseBody
 | 
			
		||||
    @GetMapping("/")
 | 
			
		||||
    public Msg<String> testGet() {
 | 
			
		||||
        logger.info("收到GET请求");
 | 
			
		||||
        return Msg.success("GET 测试");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @ResponseBody
 | 
			
		||||
    @PostMapping("/")
 | 
			
		||||
    public Msg<String> testPost() {
 | 
			
		||||
        logger.info("收到POST请求");
 | 
			
		||||
        return Msg.success("POST 测试");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @ResponseBody
 | 
			
		||||
    @PutMapping("/")
 | 
			
		||||
    public Msg<String> testPut() {
 | 
			
		||||
        logger.info("收到PUT请求");
 | 
			
		||||
        return Msg.success("PUT 测试");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @ResponseBody
 | 
			
		||||
    @DeleteMapping("/")
 | 
			
		||||
    public Msg<String> testDelete() {
 | 
			
		||||
        logger.info("收到DELETE请求");
 | 
			
		||||
        return Msg.success("DELETE 测试");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,46 @@
 | 
			
		||||
package com.hxuanyu.test.monitor;
 | 
			
		||||
 | 
			
		||||
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/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(() -> 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();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,14 +1,15 @@
 | 
			
		||||
spring:
 | 
			
		||||
notify:
 | 
			
		||||
  mail:
 | 
			
		||||
    host: smtp.domain
 | 
			
		||||
    host: smtp.qq.com
 | 
			
		||||
    protocol: smtp
 | 
			
		||||
    default-encoding: UTF-8
 | 
			
		||||
    password: your_password
 | 
			
		||||
    username: your_username
 | 
			
		||||
    password: your pwd
 | 
			
		||||
    username: your account
 | 
			
		||||
    port: 587
 | 
			
		||||
    properties:
 | 
			
		||||
      mail:
 | 
			
		||||
        debug: false
 | 
			
		||||
      stmp:
 | 
			
		||||
        socketFactory:
 | 
			
		||||
          class: javax.net.ssl.SSLSocketFactory
 | 
			
		||||
          class: javax.net.ssl.SSLSocketFactory
 | 
			
		||||
    interval: 5000
 | 
			
		||||
@@ -0,0 +1,22 @@
 | 
			
		||||
package com.hxuanyu.starter.test.common;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
import com.hxuanyu.common.message.Msg;
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
import org.slf4j.Logger;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
 | 
			
		||||
public class MsgTest {
 | 
			
		||||
 | 
			
		||||
    private final Logger logger = LoggerFactory.getLogger(MsgTest.class);
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void test() {
 | 
			
		||||
        Msg<String> msg = Msg.success("测试内容", "测试体");
 | 
			
		||||
        logger.info(msg.toString());
 | 
			
		||||
        assert msg.isSuccess();
 | 
			
		||||
        msg = Msg.failed("失败消息");
 | 
			
		||||
        logger.info(msg.toString());
 | 
			
		||||
        assert msg.isFailed();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,25 @@
 | 
			
		||||
package com.hxuanyu.starter.test.monitor;
 | 
			
		||||
 | 
			
		||||
import com.hxuanyu.monitor.base.BaseMonitorItem;
 | 
			
		||||
import com.hxuanyu.monitor.manager.MonitorItemBeanManager;
 | 
			
		||||
import com.hxuanyu.test.MainApplication;
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
import org.slf4j.Logger;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.boot.test.context.SpringBootTest;
 | 
			
		||||
 | 
			
		||||
@SpringBootTest(classes = MainApplication.class)
 | 
			
		||||
public class MonitorTest {
 | 
			
		||||
 | 
			
		||||
    private final Logger logger = LoggerFactory.getLogger(MonitorTest.class);
 | 
			
		||||
    @Autowired
 | 
			
		||||
    MonitorItemBeanManager monitorItemBeanManager;
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testMonitorItemScan() {
 | 
			
		||||
        BaseMonitorItem testMonitorItem = monitorItemBeanManager.getMonitorItemMap().get("ScheduledTask-TestMonitorItem");
 | 
			
		||||
        assert testMonitorItem.getMonitorItemName() != null;
 | 
			
		||||
        logger.info("获取到的Bean:{}", testMonitorItem.toString());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,56 @@
 | 
			
		||||
package com.hxuanyu.starter.test.network;
 | 
			
		||||
 | 
			
		||||
import com.hxuanyu.common.message.Msg;
 | 
			
		||||
import com.hxuanyu.network.service.HttpService;
 | 
			
		||||
import com.hxuanyu.test.MainApplication;
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
import org.junit.jupiter.api.Timeout;
 | 
			
		||||
import org.junit.runner.RunWith;
 | 
			
		||||
import org.slf4j.Logger;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.boot.test.context.SpringBootTest;
 | 
			
		||||
import org.springframework.test.context.web.WebAppConfiguration;
 | 
			
		||||
 | 
			
		||||
import static java.lang.Thread.sleep;
 | 
			
		||||
 | 
			
		||||
@SpringBootTest(classes = MainApplication.class)
 | 
			
		||||
public class NetworkTest {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    HttpService httpService;
 | 
			
		||||
 | 
			
		||||
    private final Logger logger = LoggerFactory.getLogger(NetworkTest.class);
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    @Timeout(3000)
 | 
			
		||||
    public void testGet() throws InterruptedException {
 | 
			
		||||
        Msg<String> msg = httpService.doGet("https://baidu.com");
 | 
			
		||||
        logger.info("GET测试:{}", msg);
 | 
			
		||||
        assert msg.isSuccess();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    @Timeout(3000)
 | 
			
		||||
    public void testPost() {
 | 
			
		||||
        Msg<String> msg = httpService.doPost("https://baidu.com");
 | 
			
		||||
        logger.info("POST测试:{}", msg);
 | 
			
		||||
        assert msg.isSuccess();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    @Timeout(3000)
 | 
			
		||||
    public void testDelete() {
 | 
			
		||||
        Msg<String> msg = httpService.doDelete("https://baidu.com");
 | 
			
		||||
        logger.info("DELETE测试:{}", msg);
 | 
			
		||||
        assert msg.isSuccess();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    @Timeout(3000)
 | 
			
		||||
    public void testPut() {
 | 
			
		||||
        Msg<String> msg = httpService.doPut("https://baidu.com");
 | 
			
		||||
        logger.info("PUT测试:{}", msg);
 | 
			
		||||
        assert msg.isSuccess();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,31 @@
 | 
			
		||||
package com.hxuanyu.starter.test.notify;
 | 
			
		||||
 | 
			
		||||
import com.hxuanyu.notify.enums.NotifyType;
 | 
			
		||||
import com.hxuanyu.notify.model.Mail;
 | 
			
		||||
import com.hxuanyu.notify.service.NotifyService;
 | 
			
		||||
import com.hxuanyu.test.MainApplication;
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.boot.test.context.SpringBootTest;
 | 
			
		||||
 | 
			
		||||
import static java.lang.Thread.sleep;
 | 
			
		||||
 | 
			
		||||
@SpringBootTest(classes = MainApplication.class)
 | 
			
		||||
public class NotifyTest {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    NotifyService notifyService;
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testNotify() throws InterruptedException {
 | 
			
		||||
        notifyService.notify(new Mail("2252193204@qq.com", "test subject", "test success"), NotifyType.TYPE_MAIL);
 | 
			
		||||
        sleep(2000);
 | 
			
		||||
        notifyService.notify("短信通知方式", NotifyType.TYPE_MSG);
 | 
			
		||||
        notifyService.notify("日志输出方式", NotifyType.TYPE_LOG);
 | 
			
		||||
        notifyService.notify(() -> {
 | 
			
		||||
            // do some things
 | 
			
		||||
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user