Compare commits
No commits in common. "master" and "v1.0.2" have entirely different histories.
24
README.md
24
README.md
|
@ -2,33 +2,9 @@
|
||||||
|
|
||||||
本项目基于springboot进行开发,实现了一系列的spring-boot-starter,可以作为开发中的工具包进行使用。
|
本项目基于springboot进行开发,实现了一系列的spring-boot-starter,可以作为开发中的工具包进行使用。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### 模块划分
|
### 模块划分
|
||||||
|
|
||||||
- common-spring-boot-starter:常用的基础类,比如用作消息流转的`Msg`以及一些工具类
|
- common-spring-boot-starter:常用的基础类,比如用作消息流转的`Msg`以及一些工具类
|
||||||
- monitor-spring-boot-starter:监控工具包,可以实现定时监控某些数据并在触发条件后实时通知
|
- monitor-spring-boot-starter:监控工具包,可以实现定时监控某些数据并在触发条件后实时通知
|
||||||
- network-spring-boot-starter:网络工具,实现了一系列http请求发送方法,引入后即可快速发送http请求
|
- network-spring-boot-starter:网络工具,实现了一系列http请求发送方法,引入后即可快速发送http请求
|
||||||
- notify-spring-boot-starter:通知工具,集成了邮件通知、短信通知等功能
|
- notify-spring-boot-starter:通知工具,集成了邮件通知、短信通知等功能
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## 快速开始
|
|
||||||
|
|
||||||
### 引入maven依赖
|
|
||||||
|
|
||||||
```xml
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.hxuanyu</groupId>
|
|
||||||
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
|
||||||
<version>1.0.4</version>
|
|
||||||
</dependency>
|
|
||||||
```
|
|
||||||
|
|
||||||
**子模块的依赖详见子模块的Readme**:
|
|
||||||
|
|
||||||
- [common-spring-boot-starter](https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter/src/branch/master/common-spring-boot-starter)
|
|
||||||
- [monitor-spring-boot-starter](https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter/src/branch/master/monitor-spring-boot-starter/README.md)
|
|
||||||
- [network-spring-boot-starter](https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter/src/branch/master/network-spring-boot-starter)
|
|
||||||
- [notify-spring-boot-starter](https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter/src/branch/master/notify-spring-boot-starter)
|
|
||||||
|
|
||||||
|
|
|
@ -1,58 +0,0 @@
|
||||||
## 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>
|
<parent>
|
||||||
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
||||||
<groupId>com.hxuanyu</groupId>
|
<groupId>com.hxuanyu</groupId>
|
||||||
<version>1.0.7-SNAPSHOT</version>
|
<version>1.0.2</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
package com.hxuanyu.common.config;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* common 自动配置类
|
|
||||||
*
|
|
||||||
* @author hanxuanyu
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
@ComponentScan("com.hxuanyu.common")
|
|
||||||
public class CommonConfiguration {
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,92 +0,0 @@
|
||||||
package com.hxuanyu.common.spring;
|
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.beans.factory.config.BeanDefinition;
|
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.ApplicationContextAware;
|
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
|
||||||
import org.springframework.context.annotation.Lazy;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* SpringContext工具类,用于获取全局ApplicationContext以及从中获取bean
|
|
||||||
*
|
|
||||||
* @author hanxuanyu
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
@Lazy(false)
|
|
||||||
public class SpringContextUtil implements ApplicationContextAware {
|
|
||||||
|
|
||||||
private static ApplicationContext applicationContext;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
||||||
SpringContextUtil.applicationContext = applicationContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ApplicationContext getApplicationContext() {
|
|
||||||
return applicationContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Object getBean(String name) {
|
|
||||||
return getApplicationContext().getBean(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> T getBean(Class<T> clazz) {
|
|
||||||
return getApplicationContext().getBean(clazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> T getBean(String name, Class<T> clazz) {
|
|
||||||
return getApplicationContext().getBean(name, clazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 主动向Spring容器中注册bean
|
|
||||||
*
|
|
||||||
* @param name BeanName
|
|
||||||
* @param clazz 注册的bean的类性
|
|
||||||
* @param args 构造方法的必要参数,顺序和类型要求和clazz中定义的一致
|
|
||||||
* @param <T> 泛型
|
|
||||||
* @return 返回注册到容器中的bean对象
|
|
||||||
*/
|
|
||||||
public static <T> T registerBean(String name, Class<T> clazz, Object... args) {
|
|
||||||
return registerBean((ConfigurableApplicationContext) applicationContext, name, clazz, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 主动向Spring容器中注册bean
|
|
||||||
*
|
|
||||||
* @param applicationContext Spring容器
|
|
||||||
* @param name BeanName
|
|
||||||
* @param clazz 注册的bean的类性
|
|
||||||
* @param args 构造方法的必要参数,顺序和类型要求和clazz中定义的一致
|
|
||||||
* @param <T> 泛型
|
|
||||||
* @return 返回注册到容器中的bean对象
|
|
||||||
*/
|
|
||||||
public static <T> T registerBean(ConfigurableApplicationContext applicationContext, String name, Class<T> clazz,
|
|
||||||
Object... args) {
|
|
||||||
if (applicationContext.containsBean(name)) {
|
|
||||||
Object bean = applicationContext.getBean(name);
|
|
||||||
if (bean.getClass().isAssignableFrom(clazz)) {
|
|
||||||
return (T) bean;
|
|
||||||
} else {
|
|
||||||
throw new RuntimeException("BeanName 重复 " + name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
|
|
||||||
for (Object arg : args) {
|
|
||||||
beanDefinitionBuilder.addConstructorArgValue(arg);
|
|
||||||
}
|
|
||||||
BeanDefinition beanDefinition = beanDefinitionBuilder.getRawBeanDefinition();
|
|
||||||
|
|
||||||
BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry) applicationContext.getBeanFactory();
|
|
||||||
beanFactory.registerBeanDefinition(name, beanDefinition);
|
|
||||||
return applicationContext.getBean(name, clazz);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1 +0,0 @@
|
||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.hxuanyu.common.config.CommonConfiguration
|
|
|
@ -1,111 +0,0 @@
|
||||||
## 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>
|
<parent>
|
||||||
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
||||||
<groupId>com.hxuanyu</groupId>
|
<groupId>com.hxuanyu</groupId>
|
||||||
<version>1.0.7-SNAPSHOT</version>
|
<version>1.0.2</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -19,10 +19,6 @@
|
||||||
<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>
|
|
@ -10,11 +10,10 @@ import com.hxuanyu.monitor.common.CheckResult;
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public abstract class BaseMonitorItem {
|
public abstract class BaseMonitorItem {
|
||||||
protected String monitorItemName;
|
private String monitorItemName;
|
||||||
protected String cron;
|
private String cron;
|
||||||
|
|
||||||
public BaseMonitorItem() {
|
public BaseMonitorItem() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public BaseMonitorItem(String monitorItemName, String cron) {
|
public BaseMonitorItem(String monitorItemName, String cron) {
|
||||||
|
@ -22,11 +21,6 @@ public abstract class BaseMonitorItem {
|
||||||
this.cron = cron;
|
this.cron = cron;
|
||||||
}
|
}
|
||||||
|
|
||||||
public BaseMonitorItem(String cron) {
|
|
||||||
this.monitorItemName = getClass().getSimpleName();
|
|
||||||
this.cron = cron;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getMonitorItemName() {
|
public String getMonitorItemName() {
|
||||||
return monitorItemName;
|
return monitorItemName;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 触发器通知
|
* 触发器通知
|
||||||
|
@ -14,33 +13,21 @@ 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() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private CheckResult(boolean triggered, Object notifyContent, NotifyType notifyType, NotifyService.CustomNotify customNotify) {
|
public CheckResult(boolean triggered, Object notifyContent, NotifyType notifyType) {
|
||||||
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 "CheckResult{" +
|
return "Notify{" +
|
||||||
"triggered=" + triggered +
|
"triggered=" + triggered +
|
||||||
", notifyContent=" + notifyContent +
|
", notifyContent='" + notifyContent + '\'' +
|
||||||
", notifyType=" + notifyType +
|
|
||||||
", customNotify=" + customNotify +
|
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +37,7 @@ public class CheckResult {
|
||||||
* @return 通知对象
|
* @return 通知对象
|
||||||
*/
|
*/
|
||||||
public static CheckResult nonTriggered() {
|
public static CheckResult nonTriggered() {
|
||||||
return new CheckResult(false, null, null, null);
|
return new CheckResult(false, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -61,19 +48,7 @@ public class CheckResult {
|
||||||
* @return 返回结果
|
* @return 返回结果
|
||||||
*/
|
*/
|
||||||
public static CheckResult triggered(Object notifyContent, NotifyType notifyType) {
|
public static CheckResult triggered(Object notifyContent, NotifyType notifyType) {
|
||||||
return new CheckResult(true, notifyContent, notifyType, null);
|
return new CheckResult(true, notifyContent, notifyType);
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 自定义通知类型触发
|
|
||||||
*
|
|
||||||
* @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,6 +1,6 @@
|
||||||
package com.hxuanyu.monitor.config;
|
package com.hxuanyu.monitor.config;
|
||||||
|
|
||||||
import com.hxuanyu.common.util.BeanUtils;
|
import com.hxuanyu.monitor.utils.BeanUtils;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.scheduling.SchedulingException;
|
import org.springframework.scheduling.SchedulingException;
|
||||||
import org.springframework.scheduling.TaskScheduler;
|
import org.springframework.scheduling.TaskScheduler;
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package com.hxuanyu.monitor.manager;
|
package com.hxuanyu.monitor.manager;
|
||||||
|
|
||||||
import com.hxuanyu.common.message.Msg;
|
|
||||||
import com.hxuanyu.common.spring.SpringContextUtil;
|
|
||||||
import com.hxuanyu.monitor.annotation.MonitorItem;
|
import com.hxuanyu.monitor.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;
|
||||||
|
@ -60,56 +58,31 @@ 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);
|
||||||
Msg<String> msg = addMonitorTask(item);
|
addMonitorTask(item);
|
||||||
logger.info("添加成功:{}", msg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Msg<String> addMonitorTask(BaseMonitorItem item) {
|
public void addMonitorTask(BaseMonitorItem item) {
|
||||||
String taskId = 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 Msg<String> addMonitorTask(String itemName, Class<? extends BaseMonitorItem> clazz, Object... args) {
|
public void setMonitorTaskCron(String taskId, String cron) {
|
||||||
BaseMonitorItem item = SpringContextUtil.registerBean(itemName, clazz, args);
|
|
||||||
return addMonitorTask(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Msg<String> addMonitorTask(Class<? extends BaseMonitorItem> clazz, Object... args) {
|
|
||||||
return addMonitorTask(clazz.getSimpleName(), clazz, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Msg<String> setMonitorTaskCron(String taskId, String cron) {
|
|
||||||
if (MONITOR_ITEM_MAP.containsKey(taskId)) {
|
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 Msg<String> deleteMonitorTask(String taskId) {
|
public void deleteMonitorTask(String taskId) {
|
||||||
if (MONITOR_ITEM_MAP.containsKey(taskId)) {
|
|
||||||
MONITOR_ITEM_MAP.remove(taskId);
|
MONITOR_ITEM_MAP.remove(taskId);
|
||||||
schedulingConfigurer.cancelTriggerTask(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) {
|
||||||
|
@ -117,13 +90,8 @@ public class MonitorItemBeanManager implements ApplicationListener<ContextRefres
|
||||||
schedulingConfigurer.resetTriggerTask(taskId, new TriggerTask(() -> {
|
schedulingConfigurer.resetTriggerTask(taskId, new TriggerTask(() -> {
|
||||||
CheckResult checkResult = item.check();
|
CheckResult checkResult = item.check();
|
||||||
if (checkResult.isTriggered()) {
|
if (checkResult.isTriggered()) {
|
||||||
if (NotifyType.TYPE_CUSTOM.equals(checkResult.getNotifyType())) {
|
|
||||||
logger.info("定时任务[{}]触发成功,执行自定义通知", taskId);
|
|
||||||
notifyService.notify(checkResult.getCustomNotify());
|
|
||||||
} else {
|
|
||||||
logger.info("定时任务[{}]触发成功,发送通知:[{}]", taskId, checkResult.getNotifyContent());
|
logger.info("定时任务[{}]触发成功,发送通知:[{}]", taskId, checkResult.getNotifyContent());
|
||||||
notifyService.notify(checkResult.getNotifyContent(), checkResult.getNotifyType());
|
notifyService.notify(checkResult.getNotifyContent(), NotifyType.MAIL_TYPE);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, new CronTrigger(cron)));
|
}, new CronTrigger(cron)));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.hxuanyu.common.util;
|
package com.hxuanyu.monitor.utils;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
@ -12,8 +12,6 @@ import java.util.*;
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public class BeanUtils {
|
public class BeanUtils {
|
||||||
|
|
||||||
|
|
||||||
public static Field findField(Class<?> clazz, String name) {
|
public static Field findField(Class<?> clazz, String name) {
|
||||||
try {
|
try {
|
||||||
return clazz.getField(name);
|
return clazz.getField(name);
|
|
@ -1,44 +0,0 @@
|
||||||
## 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>
|
<parent>
|
||||||
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
||||||
<groupId>com.hxuanyu</groupId>
|
<groupId>com.hxuanyu</groupId>
|
||||||
<version>1.0.7-SNAPSHOT</version>
|
<version>1.0.2</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package com.hxuanyu.network.service;
|
package com.hxuanyu.network.service;
|
||||||
|
|
||||||
import com.hxuanyu.common.message.Msg;
|
import com.hxuanyu.common.message.Msg;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpEntity;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ public interface HttpService {
|
||||||
* @param url 请求地址
|
* @param url 请求地址
|
||||||
* @return 统一结果报文
|
* @return 统一结果报文
|
||||||
*/
|
*/
|
||||||
Msg<HttpResponse> doGetWithResponse(String url);
|
Msg<HttpEntity> doGetWithEntity(String url);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ public interface HttpService {
|
||||||
* @param url 请求地址
|
* @param url 请求地址
|
||||||
* @param listener 异步请求结果监听
|
* @param listener 异步请求结果监听
|
||||||
*/
|
*/
|
||||||
void doGetSyncWithResponse(String url, NetWorkListener<HttpResponse> listener);
|
void doGetSyncWithEntity(String url, NetWorkListener<HttpEntity> listener);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送get请求;带请求参数
|
* 发送get请求;带请求参数
|
||||||
|
@ -66,7 +66,7 @@ public interface HttpService {
|
||||||
* @param params 请求参数集合
|
* @param params 请求参数集合
|
||||||
* @return 全局报文
|
* @return 全局报文
|
||||||
*/
|
*/
|
||||||
Msg<HttpResponse> doGetWithResponse(String url, Map<String, String> params);
|
Msg<HttpEntity> doGetWithEntity(String url, Map<String, String> params);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异步Get请求
|
* 异步Get请求
|
||||||
|
@ -84,7 +84,7 @@ public interface HttpService {
|
||||||
* @param params 请求参数
|
* @param params 请求参数
|
||||||
* @param listener 异步请求结果监听
|
* @param listener 异步请求结果监听
|
||||||
*/
|
*/
|
||||||
void doGetSyncWithResponse(String url, Map<String, String> params, NetWorkListener<HttpResponse> listener);
|
void doGetSyncWithEntity(String url, Map<String, String> params, NetWorkListener<HttpEntity> listener);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送get请求;带请求头和请求参数
|
* 发送get请求;带请求头和请求参数
|
||||||
|
@ -104,7 +104,7 @@ public interface HttpService {
|
||||||
* @param params 请求参数集合
|
* @param params 请求参数集合
|
||||||
* @return 全局报文
|
* @return 全局报文
|
||||||
*/
|
*/
|
||||||
Msg<HttpResponse> doGetWithResponse(String url, Map<String, String> headers, Map<String, String> params);
|
Msg<HttpEntity> doGetWithEntity(String url, Map<String, String> headers, Map<String, String> params);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异步Get请求
|
* 异步Get请求
|
||||||
|
@ -124,7 +124,7 @@ public interface HttpService {
|
||||||
* @param params 请求体
|
* @param params 请求体
|
||||||
* @param listener 异步结果监听器
|
* @param listener 异步结果监听器
|
||||||
*/
|
*/
|
||||||
void doGetSyncWithResponse(String url, Map<String, String> headers, Map<String, String> params, NetWorkListener<HttpResponse> listener);
|
void doGetSyncWithEntity(String url, Map<String, String> headers, Map<String, String> params, NetWorkListener<HttpEntity> listener);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送post请求;不带请求头和请求参数
|
* 发送post请求;不带请求头和请求参数
|
||||||
|
@ -140,7 +140,7 @@ public interface HttpService {
|
||||||
* @param url 请求地址
|
* @param url 请求地址
|
||||||
* @return 统一报文
|
* @return 统一报文
|
||||||
*/
|
*/
|
||||||
Msg<HttpResponse> doPostWithResponse(String url);
|
Msg<HttpEntity> doPostWithEntity(String url);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -157,7 +157,7 @@ public interface HttpService {
|
||||||
* @param url 请求地址
|
* @param url 请求地址
|
||||||
* @param listener 请求参数
|
* @param listener 请求参数
|
||||||
*/
|
*/
|
||||||
void doPostSyncWithResponse(String url, NetWorkListener<HttpResponse> listener);
|
void doPostSyncWithEntity(String url, NetWorkListener<HttpEntity> listener);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 同步Post请求
|
* 同步Post请求
|
||||||
|
@ -176,7 +176,7 @@ public interface HttpService {
|
||||||
* @param params 请求参数
|
* @param params 请求参数
|
||||||
* @return 统一消息体
|
* @return 统一消息体
|
||||||
*/
|
*/
|
||||||
Msg<HttpResponse> doPostWithResponse(String url, Map<String, String> params);
|
Msg<HttpEntity> doPostWithEntity(String url, Map<String, String> params);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异步Post请求
|
* 异步Post请求
|
||||||
|
@ -194,7 +194,7 @@ public interface HttpService {
|
||||||
* @param params 请求参数
|
* @param params 请求参数
|
||||||
* @param listener 异步请求结果监听
|
* @param listener 异步请求结果监听
|
||||||
*/
|
*/
|
||||||
void doPostSyncWithResponse(String url, Map<String, String> params, NetWorkListener<HttpResponse> listener);
|
void doPostSyncWithEntity(String url, Map<String, String> params, NetWorkListener<HttpEntity> listener);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送post请求;带请求头和请求参数
|
* 发送post请求;带请求头和请求参数
|
||||||
|
@ -214,7 +214,7 @@ public interface HttpService {
|
||||||
* @param params 请求参数集合
|
* @param params 请求参数集合
|
||||||
* @return 统一返回报文
|
* @return 统一返回报文
|
||||||
*/
|
*/
|
||||||
Msg<HttpResponse> doPostWithResponse(String url, Map<String, String> headers, Map<String, String> params);
|
Msg<HttpEntity> doPostWithEntity(String url, Map<String, String> headers, Map<String, String> params);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -235,7 +235,7 @@ public interface HttpService {
|
||||||
* @param params 请求参数
|
* @param params 请求参数
|
||||||
* @param listener 异步请求结果监听
|
* @param listener 异步请求结果监听
|
||||||
*/
|
*/
|
||||||
void doPostSyncWithResponse(String url, Map<String, String> headers, Map<String, String> params, NetWorkListener<HttpResponse> listener);
|
void doPostSyncWithEntity(String url, Map<String, String> headers, Map<String, String> params, NetWorkListener<HttpEntity> listener);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送put请求;不带请求参数
|
* 发送put请求;不带请求参数
|
||||||
|
@ -252,7 +252,7 @@ public interface HttpService {
|
||||||
* @param url 请求地址
|
* @param url 请求地址
|
||||||
* @return 统一消息返回报文
|
* @return 统一消息返回报文
|
||||||
*/
|
*/
|
||||||
Msg<HttpResponse> doPutWithResponse(String url);
|
Msg<HttpEntity> doPutWithEntity(String url);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送put请求;带请求参数
|
* 发送put请求;带请求参数
|
||||||
|
@ -270,23 +270,25 @@ public interface HttpService {
|
||||||
* @param params 请求参数
|
* @param params 请求参数
|
||||||
* @return 统一消息报文
|
* @return 统一消息报文
|
||||||
*/
|
*/
|
||||||
Msg<HttpResponse> doPutWithResponse(String url, Map<String, String> params);
|
Msg<HttpEntity> doPutWithEntity(String url, Map<String, String> params);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送delete请求;不带请求参数
|
* 发送delete请求;不带请求参数
|
||||||
*
|
*
|
||||||
* @param url 请求地址
|
* @param url 请求地址
|
||||||
* @return 统一返回报文
|
* @return 统一返回报文
|
||||||
|
* @throws Exception 异常
|
||||||
*/
|
*/
|
||||||
Msg<String> doDelete(String url);
|
Msg<String> doDelete(String url) throws Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送delete请求;不带请求参数
|
* 发送delete请求;不带请求参数
|
||||||
*
|
*
|
||||||
* @param url 请求地址
|
* @param url 请求地址
|
||||||
* @return 统一返回报文
|
* @return 统一返回报文
|
||||||
|
* @throws Exception 异常
|
||||||
*/
|
*/
|
||||||
Msg<HttpResponse> doDeleteWithResponse(String url);
|
Msg<HttpEntity> doDeleteWithEntity(String url) throws Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送delete请求;带请求参数
|
* 发送delete请求;带请求参数
|
||||||
|
@ -294,8 +296,9 @@ public interface HttpService {
|
||||||
* @param url 请求地址
|
* @param url 请求地址
|
||||||
* @param params 请求参数
|
* @param params 请求参数
|
||||||
* @return 统一返回报文
|
* @return 统一返回报文
|
||||||
|
* @throws Exception 异常
|
||||||
*/
|
*/
|
||||||
Msg<String> doDelete(String url, Map<String, String> params);
|
Msg<String> doDelete(String url, Map<String, String> params) throws Exception;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -304,8 +307,9 @@ public interface HttpService {
|
||||||
* @param url 请求地址
|
* @param url 请求地址
|
||||||
* @param params 请求参数
|
* @param params 请求参数
|
||||||
* @return 统一返回报文
|
* @return 统一返回报文
|
||||||
|
* @throws Exception 异常
|
||||||
*/
|
*/
|
||||||
Msg<HttpResponse> doDeleteWithResponse(String url, Map<String, String> params);
|
Msg<HttpEntity> doDeleteWithEntity(String url, Map<String, String> params) throws Exception;
|
||||||
|
|
||||||
interface NetWorkListener<T> {
|
interface NetWorkListener<T> {
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -2,12 +2,13 @@ package com.hxuanyu.network.service.impl;
|
||||||
|
|
||||||
import com.hxuanyu.common.message.Msg;
|
import com.hxuanyu.common.message.Msg;
|
||||||
import com.hxuanyu.network.service.HttpService;
|
import com.hxuanyu.network.service.HttpService;
|
||||||
import org.apache.http.*;
|
import org.apache.http.HttpEntity;
|
||||||
|
import org.apache.http.HttpStatus;
|
||||||
|
import org.apache.http.NameValuePair;
|
||||||
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||||
import org.apache.http.client.methods.*;
|
import org.apache.http.client.methods.*;
|
||||||
import org.apache.http.client.utils.URIBuilder;
|
import org.apache.http.client.utils.URIBuilder;
|
||||||
import org.apache.http.config.ConnectionConfig;
|
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.impl.client.HttpClients;
|
||||||
import org.apache.http.message.BasicNameValuePair;
|
import org.apache.http.message.BasicNameValuePair;
|
||||||
|
@ -20,7 +21,6 @@ import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
|
|
||||||
|
@ -56,9 +56,9 @@ public class HttpServiceImpl implements HttpService {
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Msg<HttpResponse> doGetWithResponse(String url) {
|
public Msg<HttpEntity> doGetWithEntity(String url) {
|
||||||
|
|
||||||
return doGetWithResponse(url, null, null);
|
return doGetWithEntity(url, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -72,8 +72,8 @@ public class HttpServiceImpl implements HttpService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doGetSyncWithResponse(String url, NetWorkListener<HttpResponse> listener) {
|
public void doGetSyncWithEntity(String url, NetWorkListener<HttpEntity> listener) {
|
||||||
doGetSyncWithResponse(url, null, listener);
|
doGetSyncWithEntity(url, null, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -83,8 +83,8 @@ public class HttpServiceImpl implements HttpService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Msg<HttpResponse> doGetWithResponse(String url, Map<String, String> params) {
|
public Msg<HttpEntity> doGetWithEntity(String url, Map<String, String> params) {
|
||||||
return doGetWithResponse(url, null, params);
|
return doGetWithEntity(url, null, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -93,48 +93,22 @@ public class HttpServiceImpl implements HttpService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doGetSyncWithResponse(String url, Map<String, String> params, NetWorkListener<HttpResponse> listener) {
|
public void doGetSyncWithEntity(String url, Map<String, String> params, NetWorkListener<HttpEntity> listener) {
|
||||||
doGetSyncWithResponse(url, null, params, listener);
|
doGetSyncWithEntity(url, null, params, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Msg<String> doGet(String url, Map<String, String> headers, Map<String, String> params) {
|
public Msg<String> doGet(String url, Map<String, String> headers, Map<String, String> params) {
|
||||||
// 创建httpClient对象
|
Msg<HttpEntity> httpEntityMsg = doGetWithEntity(url, headers, params);
|
||||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
return getStringMsg(httpEntityMsg);
|
||||||
// 创建访问的地址
|
|
||||||
HttpGet httpGet = buildHttpGet(url, params, headers);
|
|
||||||
// 创建httpResponse对象
|
|
||||||
CloseableHttpResponse httpResponse = null;
|
|
||||||
// 执行请求并获得响应结果
|
|
||||||
Msg<HttpResponse> httpClientResult = getHttpClientResult(httpClient, httpGet);
|
|
||||||
if (httpClientResult.isSuccess()) {
|
|
||||||
String result = EntityUtils.toString(httpClientResult.getData().getEntity());
|
|
||||||
return Msg.success("请求成功[" + httpClientResult.getCode() + "]", result);
|
|
||||||
} else {
|
|
||||||
return Msg.failed("请求失败[" + httpClientResult.getCode() + "]");
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error("请求过程出现异常: {}", e.getMessage());
|
|
||||||
return Msg.failed("请求过程中出现异常" + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Msg<HttpResponse> doGetWithResponse(String url, Map<String, String> headers, Map<String, String> params) {
|
public Msg<HttpEntity> doGetWithEntity(String url, Map<String, String> headers, Map<String, String> params) {
|
||||||
// 创建httpClient对象
|
// 创建httpClient对象
|
||||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||||
HttpGet httpGet = buildHttpGet(url, params, headers);
|
|
||||||
// 执行请求并获得响应结果
|
|
||||||
return getHttpClientResult(httpClient, httpGet);
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error("请求过程出现异常: {}", e.getMessage());
|
|
||||||
return Msg.failed("请求过程中出现异常" + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private HttpGet buildHttpGet(String url, Map<String, String> params, Map<String, String> headers) throws URISyntaxException {
|
|
||||||
// 创建访问的地址
|
// 创建访问的地址
|
||||||
URIBuilder uriBuilder = new URIBuilder(url);
|
URIBuilder uriBuilder = new URIBuilder(url);
|
||||||
if (params != null) {
|
if (params != null) {
|
||||||
|
@ -143,18 +117,35 @@ public class HttpServiceImpl implements HttpService {
|
||||||
uriBuilder.setParameter(entry.getKey(), entry.getValue());
|
uriBuilder.setParameter(entry.getKey(), entry.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建http对象
|
// 创建http对象
|
||||||
HttpGet httpGet = new HttpGet(uriBuilder.build());
|
HttpGet httpGet = new HttpGet(uriBuilder.build());
|
||||||
RequestConfig requestConfig = RequestConfig
|
/*
|
||||||
.custom()
|
* setConnectTimeout:设置连接超时时间,单位毫秒。
|
||||||
.setConnectTimeout(CONNECT_TIMEOUT)
|
* setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection
|
||||||
.setConnectionRequestTimeout(CONNECT_TIMEOUT)
|
* 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
|
||||||
.setSocketTimeout(SOCKET_TIMEOUT)
|
* setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
|
||||||
.build();
|
*/
|
||||||
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
|
||||||
httpGet.setConfig(requestConfig);
|
httpGet.setConfig(requestConfig);
|
||||||
|
|
||||||
// 设置请求头
|
// 设置请求头
|
||||||
packageHeader(headers, httpGet);
|
packageHeader(headers, httpGet);
|
||||||
return httpGet;
|
|
||||||
|
// 创建httpResponse对象
|
||||||
|
CloseableHttpResponse httpResponse = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 执行请求并获得响应结果
|
||||||
|
return getHttpClientResult(httpClient, httpGet);
|
||||||
|
} finally {
|
||||||
|
// 释放资源
|
||||||
|
release(httpClient);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("请求过程出现异常: {}", e.getMessage());
|
||||||
|
return Msg.failed("网络请求发生异常:" + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -177,10 +168,10 @@ public class HttpServiceImpl implements HttpService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doGetSyncWithResponse(String url, Map<String, String> headers, Map<String, String> params, NetWorkListener<HttpResponse> listener) {
|
public void doGetSyncWithEntity(String url, Map<String, String> headers, Map<String, String> params, NetWorkListener<HttpEntity> listener) {
|
||||||
executorService.execute(() -> {
|
executorService.execute(() -> {
|
||||||
try {
|
try {
|
||||||
Msg<HttpResponse> msg = doGetWithResponse(url, headers, params);
|
Msg<HttpEntity> msg = doGetWithEntity(url, headers, params);
|
||||||
logger.debug("异步请求结果:{}", msg);
|
logger.debug("异步请求结果:{}", msg);
|
||||||
if (msg.getCode().equals(Msg.MSG_CODE_SUCCESS)) {
|
if (msg.getCode().equals(Msg.MSG_CODE_SUCCESS)) {
|
||||||
listener.onSuccess(msg);
|
listener.onSuccess(msg);
|
||||||
|
@ -200,8 +191,8 @@ public class HttpServiceImpl implements HttpService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Msg<HttpResponse> doPostWithResponse(String url) {
|
public Msg<HttpEntity> doPostWithEntity(String url) {
|
||||||
return doPostWithResponse(url, null, null);
|
return doPostWithEntity(url, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -210,8 +201,8 @@ public class HttpServiceImpl implements HttpService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doPostSyncWithResponse(String url, NetWorkListener<HttpResponse> listener) {
|
public void doPostSyncWithEntity(String url, NetWorkListener<HttpEntity> listener) {
|
||||||
doPostSyncWithResponse(url, null, listener);
|
doPostSyncWithEntity(url, null, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -221,8 +212,8 @@ public class HttpServiceImpl implements HttpService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Msg<HttpResponse> doPostWithResponse(String url, Map<String, String> params) {
|
public Msg<HttpEntity> doPostWithEntity(String url, Map<String, String> params) {
|
||||||
return doPostWithResponse(url, null, params);
|
return doPostWithEntity(url, null, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -231,32 +222,40 @@ public class HttpServiceImpl implements HttpService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doPostSyncWithResponse(String url, Map<String, String> params, NetWorkListener<HttpResponse> listener) {
|
public void doPostSyncWithEntity(String url, Map<String, String> params, NetWorkListener<HttpEntity> listener) {
|
||||||
doPostSyncWithResponse(url, null, params, listener);
|
doPostSyncWithEntity(url, null, params, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Msg<String> doPost(String url, Map<String, String> headers, Map<String, String> params) {
|
public Msg<String> doPost(String url, Map<String, String> headers, Map<String, String> params) {
|
||||||
Msg<HttpResponse> httpResponseMsg = doPostWithResponse(url, headers, params);
|
Msg<HttpEntity> httpEntityMsg = doPostWithEntity(url, headers, params);
|
||||||
String stringResult;
|
return getStringMsg(httpEntityMsg);
|
||||||
try {
|
|
||||||
stringResult = EntityUtils.toString(httpResponseMsg.getData().getEntity());
|
|
||||||
return Msg.success("请求成功", stringResult);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return Msg.failed("转换字符串失败:" + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Msg<HttpResponse> doPostWithResponse(String url, Map<String, String> headers, Map<String, String> params) {
|
public Msg<HttpEntity> doPostWithEntity(String url, Map<String, String> headers, Map<String, String> params) {
|
||||||
// 创建httpClient对象
|
// 创建httpClient对象
|
||||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||||
|
|
||||||
// 创建http对象
|
// 创建http对象
|
||||||
HttpPost httpPost = new HttpPost(url);
|
HttpPost httpPost = new HttpPost(url);
|
||||||
|
/*
|
||||||
|
* setConnectTimeout:设置连接超时时间,单位毫秒。
|
||||||
|
* setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection
|
||||||
|
* 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
|
||||||
|
* setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
|
||||||
|
*/
|
||||||
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
|
||||||
httpPost.setConfig(requestConfig);
|
httpPost.setConfig(requestConfig);
|
||||||
|
// 设置请求头
|
||||||
|
httpPost.setHeader("Cookie", "");
|
||||||
|
httpPost.setHeader("Connection", "keep-alive");
|
||||||
|
httpPost.setHeader("Accept", "application/json");
|
||||||
|
httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
|
||||||
|
httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
|
||||||
|
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
|
||||||
packageHeader(headers, httpPost);
|
packageHeader(headers, httpPost);
|
||||||
|
|
||||||
// 封装请求参数
|
// 封装请求参数
|
||||||
try {
|
try {
|
||||||
packageParam(params, httpPost);
|
packageParam(params, httpPost);
|
||||||
|
@ -266,6 +265,13 @@ public class HttpServiceImpl implements HttpService {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
logger.error("请求过程出现异常: {}", e.getMessage());
|
logger.error("请求过程出现异常: {}", e.getMessage());
|
||||||
return Msg.failed("请求过程出现异常: " + e.getMessage());
|
return Msg.failed("请求过程出现异常: " + e.getMessage());
|
||||||
|
} finally {
|
||||||
|
// 释放资源
|
||||||
|
try {
|
||||||
|
release(httpClient);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -287,10 +293,10 @@ public class HttpServiceImpl implements HttpService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doPostSyncWithResponse(String url, Map<String, String> headers, Map<String, String> params, NetWorkListener<HttpResponse> listener) {
|
public void doPostSyncWithEntity(String url, Map<String, String> headers, Map<String, String> params, NetWorkListener<HttpEntity> listener) {
|
||||||
executorService.execute(() -> {
|
executorService.execute(() -> {
|
||||||
try {
|
try {
|
||||||
Msg<HttpResponse> msg = doPostWithResponse(url, headers, params);
|
Msg<HttpEntity> msg = doPostWithEntity(url, headers, params);
|
||||||
if (msg.getCode().equals(Msg.MSG_CODE_SUCCESS)) {
|
if (msg.getCode().equals(Msg.MSG_CODE_SUCCESS)) {
|
||||||
listener.onSuccess(msg);
|
listener.onSuccess(msg);
|
||||||
} else if (msg.getCode().equals(Msg.MSG_CODE_FAILED)) {
|
} else if (msg.getCode().equals(Msg.MSG_CODE_FAILED)) {
|
||||||
|
@ -310,26 +316,18 @@ public class HttpServiceImpl implements HttpService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Msg<HttpResponse> doPutWithResponse(String url) {
|
public Msg<HttpEntity> doPutWithEntity(String url) {
|
||||||
return doPutWithResponse(url, null);
|
return doPutWithEntity(url, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Msg<String> doPut(String url, Map<String, String> params) {
|
public Msg<String> doPut(String url, Map<String, String> params) {
|
||||||
Msg<HttpResponse> httpResponseMsg = doPutWithResponse(url, params);
|
Msg<HttpEntity> httpEntityMsg = doPutWithEntity(url, params);
|
||||||
String stringResult;
|
return getStringMsg(httpEntityMsg);
|
||||||
try {
|
|
||||||
stringResult = EntityUtils.toString(httpResponseMsg.getData().getEntity());
|
|
||||||
return Msg.success("请求成功", stringResult);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return Msg.failed("转换字符串失败:" + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Msg<HttpResponse> doPutWithResponse(String url, Map<String, String> params) {
|
public Msg<HttpEntity> doPutWithEntity(String url, Map<String, String> params) {
|
||||||
// 创建httpClient对象
|
|
||||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||||
HttpPut httpPut = new HttpPut(url);
|
HttpPut httpPut = new HttpPut(url);
|
||||||
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
|
||||||
|
@ -344,26 +342,41 @@ public class HttpServiceImpl implements HttpService {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
logger.error("请求过程出现异常: {}", e.getMessage());
|
logger.error("请求过程出现异常: {}", e.getMessage());
|
||||||
return Msg.failed("网络请求时发生异常: " + e.getMessage());
|
return Msg.failed("网络请求时发生异常: " + e.getMessage());
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
release(httpClient);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String parseEntityToString(HttpEntity httpEntity) throws IOException {
|
||||||
|
return EntityUtils.toString(httpEntity, ENCODING);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Msg<String> getStringMsg(Msg<HttpEntity> httpEntityMsg) {
|
||||||
|
if (Msg.MSG_CODE_SUCCESS.equals(httpEntityMsg.getCode())) {
|
||||||
|
try {
|
||||||
|
String result = parseEntityToString(httpEntityMsg.getData());
|
||||||
|
return Msg.success(httpEntityMsg.getMsg(), result);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return Msg.failed("转换字符串过程中发生异常:" + e.getMessage());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Msg.failed(httpEntityMsg.getMsg());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Msg<String> doDelete(String url) {
|
public Msg<String> doDelete(String url) {
|
||||||
Msg<HttpResponse> httpResponseMsg = doDeleteWithResponse(url);
|
Msg<HttpEntity> httpEntityMsg = doDeleteWithEntity(url);
|
||||||
String stringResult;
|
return getStringMsg(httpEntityMsg);
|
||||||
try {
|
|
||||||
stringResult = EntityUtils.toString(httpResponseMsg.getData().getEntity());
|
|
||||||
return Msg.success("请求成功", stringResult);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return Msg.failed("转换字符串失败:" + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Msg<HttpResponse> doDeleteWithResponse(String url) {
|
public Msg<HttpEntity> doDeleteWithEntity(String url) {
|
||||||
// 创建httpClient对象
|
|
||||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||||
HttpDelete httpDelete = new HttpDelete(url);
|
HttpDelete httpDelete = new HttpDelete(url);
|
||||||
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
|
||||||
|
@ -373,30 +386,29 @@ public class HttpServiceImpl implements HttpService {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return Msg.failed("转换String时发生IO异常");
|
return Msg.failed("转换String时发生IO异常");
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
release(httpClient);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Msg<String> doDelete(String url, Map<String, String> params) {
|
public Msg<String> doDelete(String url, Map<String, String> params) {
|
||||||
Msg<HttpResponse> httpResponseMsg = doDeleteWithResponse(url, params);
|
Msg<HttpEntity> httpEntityMsg = doDeleteWithEntity(url, params);
|
||||||
String stringResult;
|
return getStringMsg(httpEntityMsg);
|
||||||
try {
|
|
||||||
stringResult = EntityUtils.toString(httpResponseMsg.getData().getEntity());
|
|
||||||
return Msg.success("请求成功", stringResult);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return Msg.failed("转换字符串失败:" + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Msg<HttpResponse> doDeleteWithResponse(String url, Map<String, String> params) {
|
public Msg<HttpEntity> doDeleteWithEntity(String url, Map<String, String> params){
|
||||||
if (params == null) {
|
if (params == null) {
|
||||||
params = new HashMap<>(0);
|
params = new HashMap<>(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
params.put("_method", "delete");
|
params.put("_method", "delete");
|
||||||
return doPostWithResponse(url, params);
|
return doPostWithEntity(url, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -426,15 +438,27 @@ public class HttpServiceImpl implements HttpService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Msg<HttpResponse> getHttpClientResult(CloseableHttpClient httpClient, HttpRequestBase httpMethod) throws IOException {
|
private Msg<HttpEntity> getHttpClientResult(CloseableHttpClient httpClient, HttpRequestBase httpMethod) {
|
||||||
// 执行请求
|
// 执行请求
|
||||||
CloseableHttpResponse httpResponse;
|
CloseableHttpResponse httpResponse;
|
||||||
|
try {
|
||||||
logger.info("执行请求:{},请求方式:{}", httpMethod.getURI().toString(), httpMethod.getMethod());
|
logger.info("执行请求:{},请求方式:{}", httpMethod.getURI().toString(), httpMethod.getMethod());
|
||||||
httpResponse = httpClient.execute(httpMethod);
|
httpResponse = httpClient.execute(httpMethod);
|
||||||
// 获取返回结果
|
// 获取返回结果
|
||||||
if (httpResponse != null && httpResponse.getStatusLine() != null) {
|
if (httpResponse != null && httpResponse.getStatusLine() != null) {
|
||||||
return Msg.success("请求成功", httpResponse);
|
HttpEntity content;
|
||||||
|
if (httpResponse.getEntity() != null) {
|
||||||
|
content = httpResponse.getEntity();
|
||||||
|
return Msg.success(httpResponse.getStatusLine().getStatusCode() + "请求成功", content);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
logger.error("请求过程中出现异常: {}", e.getMessage());
|
||||||
|
return Msg.failed("请求过程出现异常" + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return Msg.failed(HttpStatus.SC_INTERNAL_SERVER_ERROR + "请求失败");
|
return Msg.failed(HttpStatus.SC_INTERNAL_SERVER_ERROR + "请求失败");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,103 +0,0 @@
|
||||||
## 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>
|
<parent>
|
||||||
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
||||||
<groupId>com.hxuanyu</groupId>
|
<groupId>com.hxuanyu</groupId>
|
||||||
<version>1.0.7-SNAPSHOT</version>
|
<version>1.0.2</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|
|
@ -1,108 +0,0 @@
|
||||||
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,57 +1,18 @@
|
||||||
package com.hxuanyu.notify.config;
|
package com.hxuanyu.notify.config;
|
||||||
|
|
||||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.*;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Properties;
|
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author hxuanyu
|
* @author hxuanyu
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan("com.hxuanyu.notify")
|
@ComponentScan("com.hxuanyu.notify.service")
|
||||||
@EnableConfigurationProperties(MailProperties.class)
|
|
||||||
public class NotifyConfiguration {
|
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")
|
@Bean(name = "mailExecutorService")
|
||||||
public ExecutorService mailExecutorService() {
|
public ExecutorService mailExecutorService() {
|
||||||
// 使用 ThreadFactoryBuilder 创建自定义线程名称的 ThreadFactory
|
// 使用 ThreadFactoryBuilder 创建自定义线程名称的 ThreadFactory
|
||||||
|
|
|
@ -10,12 +10,10 @@ public enum NotifyType {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通知类型
|
* 邮件类型
|
||||||
*/
|
*/
|
||||||
TYPE_LOG("日志输出"),
|
MAIL_TYPE("邮件"),
|
||||||
TYPE_MAIL("邮件"),
|
SMS_TYPE("短信");
|
||||||
TYPE_CUSTOM("自定义"),
|
|
||||||
TYPE_MSG("短信");
|
|
||||||
|
|
||||||
private final String typeName;
|
private final String typeName;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通知服务
|
* 通知服务
|
||||||
|
@ -16,18 +17,4 @@ 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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,19 +2,18 @@ package com.hxuanyu.notify.service.impl;
|
||||||
|
|
||||||
|
|
||||||
import com.hxuanyu.notify.common.MailQueue;
|
import com.hxuanyu.notify.common.MailQueue;
|
||||||
import com.hxuanyu.notify.config.MailProperties;
|
|
||||||
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 org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.mail.javamail.JavaMailSender;
|
import org.springframework.mail.javamail.JavaMailSender;
|
||||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
|
||||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.thymeleaf.TemplateEngine;
|
||||||
|
import org.thymeleaf.context.Context;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
import javax.annotation.PreDestroy;
|
import javax.annotation.PreDestroy;
|
||||||
|
@ -28,14 +27,15 @@ import java.util.concurrent.ExecutorService;
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class MailServiceImpl implements MailService {
|
public class MailServiceImpl implements MailService {
|
||||||
@Resource
|
|
||||||
MailProperties mailProperties;
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
|
private static final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
|
||||||
private static boolean isRunning = true;
|
private static boolean isRunning = true;
|
||||||
ExecutorService executor;
|
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());
|
executor.submit(new PollMail());
|
||||||
}
|
}
|
||||||
|
|
||||||
class PollMail implements Runnable {
|
static class PollMail implements Runnable {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (isRunning) {
|
while (isRunning) {
|
||||||
|
@ -55,7 +55,7 @@ public class MailServiceImpl implements MailService {
|
||||||
if (mail != null) {
|
if (mail != null) {
|
||||||
//可以设置延时 以及重复校验等等操作
|
//可以设置延时 以及重复校验等等操作
|
||||||
sendMailSync(mail);
|
sendMailSync(mail);
|
||||||
Thread.sleep(mailProperties.getInterval());
|
Thread.sleep(10000);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -71,14 +71,10 @@ public class MailServiceImpl implements MailService {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void sendMailSync(Mail mail) {
|
private static void sendMailSync(Mail mail) {
|
||||||
if (mailProperties == null){
|
|
||||||
logger.error("未配置邮件信息,请在配置文件中添加配置信息后再发送邮件");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
String from = mail.getFrom();
|
String from = mail.getFrom();
|
||||||
if (from == null) {
|
if (from == null) {
|
||||||
from = mailProperties.getUsername();
|
from = defaultFrom;
|
||||||
logger.info("未传入发件人,从配置中读取:{}", from);
|
logger.info("未传入发件人,从配置中读取:{}", from);
|
||||||
}
|
}
|
||||||
MimeMessage mimeMessage;
|
MimeMessage mimeMessage;
|
||||||
|
@ -91,7 +87,12 @@ public class MailServiceImpl implements MailService {
|
||||||
mimeMessageHelper.setFrom(from);
|
mimeMessageHelper.setFrom(from);
|
||||||
mimeMessageHelper.setTo(mail.getTo());
|
mimeMessageHelper.setTo(mail.getTo());
|
||||||
mimeMessageHelper.setSubject(mail.getSubject());
|
mimeMessageHelper.setSubject(mail.getSubject());
|
||||||
String content = mail.getContent();
|
// 利用 Thymeleaf 引擎渲染 HTML
|
||||||
|
Context context = new Context();
|
||||||
|
// 设置注入的变量
|
||||||
|
context.setVariable("templates/mail", mail);
|
||||||
|
// 模板设置为 "mail"
|
||||||
|
String content = templateEngine.process("templates/mail/mail", context);
|
||||||
// 设置邮件内容
|
// 设置邮件内容
|
||||||
// true 表示开启 html
|
// true 表示开启 html
|
||||||
mimeMessageHelper.setText(content, true);
|
mimeMessageHelper.setText(content, true);
|
||||||
|
@ -111,9 +112,24 @@ public class MailServiceImpl implements MailService {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
public void setJavaMailSender(JavaMailSender javaMailSender) {
|
||||||
|
MailServiceImpl.javaMailSender = javaMailSender;
|
||||||
|
}
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void setExecutor(@Qualifier("mailExecutorService") ExecutorService executor) {
|
public void setExecutor(@Qualifier("mailExecutorService") ExecutorService executor) {
|
||||||
this.executor = 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,26 +25,15 @@ 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.MAIL_TYPE.equals(notifyType)) {
|
||||||
if (NotifyType.TYPE_MAIL.equals(notifyType)) {
|
|
||||||
sendMail(content);
|
sendMail(content);
|
||||||
} else if (NotifyType.TYPE_MSG.equals(notifyType)) {
|
} else if (NotifyType.SMS_TYPE.equals(notifyType)) {
|
||||||
sendSms(content);
|
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 {
|
} 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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,48 +0,0 @@
|
||||||
{
|
|
||||||
"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,10 +1,10 @@
|
||||||
notify:
|
spring:
|
||||||
mail:
|
mail:
|
||||||
host: smtp.qq.com
|
host: smtp.domain
|
||||||
protocol: smtp
|
protocol: smtp
|
||||||
default-encoding: UTF-8
|
default-encoding: UTF-8
|
||||||
password: your pwd
|
password: your_password
|
||||||
username: your account
|
username: your_username
|
||||||
port: 587
|
port: 587
|
||||||
properties:
|
properties:
|
||||||
mail:
|
mail:
|
||||||
|
@ -12,4 +12,3 @@ notify:
|
||||||
stmp:
|
stmp:
|
||||||
socketFactory:
|
socketFactory:
|
||||||
class: javax.net.ssl.SSLSocketFactory
|
class: javax.net.ssl.SSLSocketFactory
|
||||||
interval: 5000
|
|
|
@ -0,0 +1,129 @@
|
||||||
|
<!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>
|
<groupId>com.hxuanyu</groupId>
|
||||||
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
||||||
<version>1.0.7-SNAPSHOT</version>
|
<version>1.0.2</version>
|
||||||
<name>hxuanyu-spring-boot-starter-parent</name>
|
<name>hxuanyu-spring-boot-starter-parent</name>
|
||||||
<description>MonitorPushingParent</description>
|
<description>MonitorPushingParent</description>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
|
@ -19,7 +19,7 @@
|
||||||
<connection>scm:git:https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter.git</connection>
|
<connection>scm:git:https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter.git</connection>
|
||||||
<developerConnection>scm:git:https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter.git</developerConnection>
|
<developerConnection>scm:git:https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter.git</developerConnection>
|
||||||
<url>https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter</url>
|
<url>https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter</url>
|
||||||
<tag>v1.0.0</tag>
|
<tag>v1.0.2</tag>
|
||||||
</scm>
|
</scm>
|
||||||
<developers>
|
<developers>
|
||||||
<developer>
|
<developer>
|
||||||
|
@ -34,7 +34,6 @@
|
||||||
<module>network-spring-boot-starter</module>
|
<module>network-spring-boot-starter</module>
|
||||||
<module>notify-spring-boot-starter</module>
|
<module>notify-spring-boot-starter</module>
|
||||||
<module>monitor-spring-boot-starter</module>
|
<module>monitor-spring-boot-starter</module>
|
||||||
<module>test</module>
|
|
||||||
</modules>
|
</modules>
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
@ -166,7 +165,7 @@
|
||||||
<tagNameFormat>v@{project.version}</tagNameFormat>
|
<tagNameFormat>v@{project.version}</tagNameFormat>
|
||||||
<autoVersionSubmodules>true</autoVersionSubmodules>
|
<autoVersionSubmodules>true</autoVersionSubmodules>
|
||||||
<password>${git.password}</password>
|
<password>${git.password}</password>
|
||||||
<username>${git.username}</username>
|
<username>hxuanyu</username>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
|
|
59
test/pom.xml
59
test/pom.xml
|
@ -1,59 +0,0 @@
|
||||||
<?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.7-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>
|
|
|
@ -1,17 +0,0 @@
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,58 +0,0 @@
|
||||||
package com.hxuanyu.test.controller;
|
|
||||||
|
|
||||||
import com.hxuanyu.common.message.Msg;
|
|
||||||
import com.hxuanyu.monitor.base.BaseMonitorItem;
|
|
||||||
import com.hxuanyu.monitor.manager.MonitorItemBeanManager;
|
|
||||||
import com.hxuanyu.test.monitor.TestMonitorItem;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
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 {
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(MonitorTestController.class);
|
|
||||||
|
|
||||||
@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(TestMonitorItem.class, "0/5 * * * * *");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,52 +0,0 @@
|
||||||
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 测试");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,53 +0,0 @@
|
||||||
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
|
|
||||||
*/
|
|
||||||
public class TestMonitorItem extends BaseMonitorItem {
|
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(TestMonitorItem.class);
|
|
||||||
|
|
||||||
public TestMonitorItem(String monitorItemName, String cron) {
|
|
||||||
super(monitorItemName, cron);
|
|
||||||
}
|
|
||||||
|
|
||||||
public TestMonitorItem(String cron) {
|
|
||||||
super(cron);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public CheckResult check() {
|
|
||||||
double random = Math.random();
|
|
||||||
int result = (int) (random * NotifyType.values().length);
|
|
||||||
logger.info("随机索引值:{}", result);
|
|
||||||
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,51 +0,0 @@
|
||||||
package com.hxuanyu.starter.test;
|
|
||||||
|
|
||||||
import org.apache.http.HttpEntity;
|
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
|
||||||
import org.apache.http.impl.client.HttpClients;
|
|
||||||
import org.apache.http.util.EntityUtils;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class OtherTest {
|
|
||||||
|
|
||||||
private static final String STATION_NAME_URL = "https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9226";
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testHttpClient() {
|
|
||||||
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
||||||
try {
|
|
||||||
// 创建httpget.
|
|
||||||
HttpGet httpget = new HttpGet(STATION_NAME_URL);
|
|
||||||
System.out.println("executing request " + httpget.getURI());
|
|
||||||
// 执行get请求.
|
|
||||||
try (CloseableHttpResponse response = httpclient.execute(httpget)) {
|
|
||||||
// 获取响应实体
|
|
||||||
HttpEntity entity = response.getEntity();
|
|
||||||
System.out.println("--------------------------------------");
|
|
||||||
// 打印响应状态
|
|
||||||
System.out.println(response.getStatusLine());
|
|
||||||
if (entity != null) {
|
|
||||||
// 打印响应内容长度
|
|
||||||
System.out.println("Response content length: " + entity.getContentLength());
|
|
||||||
// 打印响应内容
|
|
||||||
System.out.println("Response content: " + EntityUtils.toString(entity));
|
|
||||||
}
|
|
||||||
System.out.println("------------------------------------");
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
// 关闭连接,释放资源
|
|
||||||
try {
|
|
||||||
httpclient.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,22 +0,0 @@
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,27 +0,0 @@
|
||||||
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");
|
|
||||||
if (testMonitorItem != null){
|
|
||||||
assert testMonitorItem.getMonitorItemName() != null;
|
|
||||||
logger.info("获取到的Bean:{}", testMonitorItem.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,53 +0,0 @@
|
||||||
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.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 NetworkTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
HttpService httpService;
|
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(NetworkTest.class);
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Timeout(3000)
|
|
||||||
public void testGet(){
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,31 +0,0 @@
|
||||||
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
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue