Compare commits

..

24 Commits

Author SHA1 Message Date
hanxuanyu a40d593a53 [maven-release-plugin] prepare for next development iteration 2022-02-22 21:16:49 +08:00
hanxuanyu 30f67b6dee [maven-release-plugin] prepare release v1.0.6 2022-02-22 21:16:41 +08:00
hanxuanyu a53455bf87 任务ID直接使用监控项名称 2022-02-09 17:34:26 +08:00
hanxuanyu 1016ad6751 定时任务调用逻辑修改 2022-02-08 10:20:38 +08:00
hanxuanyu cba56f6e56 [maven-release-plugin] prepare for next development iteration 2022-02-07 20:59:50 +08:00
hanxuanyu 1e01449a7b [maven-release-plugin] prepare release v1.0.5 2022-02-07 20:59:46 +08:00
hanxuanyu e91812f942 修复GET较长字符串数据时数据流中断的bug 2022-02-07 20:54:42 +08:00
hanxuanyu ddf4b4bc07 网络请求工具bug修改 2022-02-07 17:31:12 +08:00
hanxuanyu 38416ff420 新增部分工具 2022-01-27 17:17:42 +08:00
hanxuanyu b5bc5b883e [maven-release-plugin] prepare for next development iteration 2022-01-25 16:43:08 +08:00
hanxuanyu f727de32d0 [maven-release-plugin] prepare release v1.0.4 2022-01-25 16:43:04 +08:00
hanxuanyu ca5d97c7a0 完善自述文件 2022-01-25 16:41:28 +08:00
hanxuanyu 95b41916dc 完善自述文件 2022-01-25 16:34:22 +08:00
hanxuanyu cb418ba35a 新增自定义通知类型以及通知bug修复 2022-01-25 16:33:18 +08:00
hanxuanyu 0c074c046f 测试类编写 2022-01-24 23:52:29 +08:00
hanxuanyu ca65b5e8fc 修复notify中mail模块的bug 2022-01-24 20:57:21 +08:00
hanxuanyu baccb20c3c 修复notify中mail模块的bug 2022-01-24 20:43:33 +08:00
hanxuanyu 8c47f75673 添加Readme 2022-01-24 18:50:26 +08:00
hanxuanyu 16df08bbe8 notify模块测试内容编写 2022-01-24 18:02:25 +08:00
hanxuanyu c100c89f5a 新增测试模块 2022-01-24 17:50:55 +08:00
hanxuanyu 45ab626751 [maven-release-plugin] prepare for next development iteration 2022-01-24 14:21:31 +08:00
hanxuanyu 58d2ce559d [maven-release-plugin] prepare release v1.0.3 2022-01-24 14:21:28 +08:00
hanxuanyu 6febbb049d release配置修改 2022-01-24 14:16:35 +08:00
hanxuanyu dab1b9fa55 [maven-release-plugin] prepare for next development iteration 2022-01-24 12:45:43 +08:00
39 changed files with 1363 additions and 372 deletions

View File

@ -2,9 +2,33 @@
本项目基于springboot进行开发实现了一系列的spring-boot-starter可以作为开发中的工具包进行使用。
### 模块划分
- common-spring-boot-starter常用的基础类比如用作消息流转的`Msg`以及一些工具类
- monitor-spring-boot-starter监控工具包可以实现定时监控某些数据并在触发条件后实时通知
- network-spring-boot-starter网络工具实现了一系列http请求发送方法引入后即可快速发送http请求
- notify-spring-boot-starter通知工具集成了邮件通知、短信通知等功能
- notify-spring-boot-starter通知工具集成了邮件通知、短信通知等功能
## 快速开始
### 引入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)

View File

@ -0,0 +1,58 @@
## common-spring-boot-starter
### 简介
本模块封装了常用的工具类,对外提供基础的功能,当前包含:
- Msg统一消息对象
### 引入
```java
<dependency>
<groupId>com.hxuanyu</groupId>
<artifactId>common-spring-boot-starter</artifactId>
<version>1.0.4</version>
</dependency>
```
### 功能
#### Msg
用于规范方法间调用返回的结果即方法无论调用成功还是失败都以Msg对象作为返回值实际的返回值封装在msg中调用者可根据Msg的状态码进行状态判断成功则从Msg对象中取值失败则做相应的失败处理避免了直接调用时可能出现的空指针问题。
使用方式:
```java
public void test() {
Msg<String> msg = doSomeThing("args");
if (msg.isSuccess()) {
// do some things
} else if (msg.isFailed()) {
// do some things
}
}
private Msg<String> doSomeThing(String args) {
if (args != null) {
return Msg.success("your success msg", "your data");
} else {
return Msg.failed("your failed msg");
}
}
```
- 如果方法调用成功,则在返回值中传入成功消息以及可选的返回对象,该对象为泛型,可以在声明方法时指定
- 如果方法调用失败,则在返回值中传入失败原因,**注意失败时不可设置data字段只能传入失败消息**
- 调用者可以根据错误码或者直接调用`msg.isSuccess()`方法判断是否调用成功,并对结果进行相应处理
- 在链式调用时位于中间的方法不建议直接将上游请求到的msg结果传递给下游应重新创建新的msg对象

View File

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

View File

@ -0,0 +1,16 @@
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 {
}

View File

@ -0,0 +1,92 @@
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);
}
}

View File

@ -1,4 +1,4 @@
package com.hxuanyu.monitor.utils;
package com.hxuanyu.common.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@ -12,6 +12,8 @@ import java.util.*;
* @version 1.0
*/
public class BeanUtils {
public static Field findField(Class<?> clazz, String name) {
try {
return clazz.getField(name);

View File

@ -0,0 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.hxuanyu.common.config.CommonConfiguration

View File

@ -0,0 +1,111 @@
## monitor-spring-boot-starter
### 简介
- 本模块实现了一个定时监控并推送通知的服务,适用于处理一些定时监控并实时通知的场景,举个栗子,可以用来监控火车票,当检测到有票之后触发通知,通过配置好的通知方式告知我们,目前内置了邮件通知、日志输出方式,以及自定义通知。
### 引入
```java
<dependency>
<groupId>com.hxuanyu</groupId>
<artifactId>monitor-spring-boot-starter</artifactId>
<version>1.0.4</version>
</dependency>
```
### 使用
- 创建监控项
```java
@MonitorItem(cron = "0/20 * * * * *")
public class TestMonitorItem extends BaseMonitorItem {
private final Logger logger = LoggerFactory.getLogger(TestMonitorItem.class);
@Override
public CheckResult check() {
double random = Math.random();
int result = (int) (random * NotifyType.values().length);
logger.info("随机索引值:{}", result);
if (NotifyType.values().length > 0) {
NotifyType notifyType = NotifyType.values()[result];
switch (notifyType) {
case TYPE_LOG:
return CheckResult.triggered("日志输出", NotifyType.TYPE_LOG);
case TYPE_CUSTOM:
return CheckResult.triggered(new NotifyService.CustomNotify() {
@Override
public void onNotify() {
logger.info("自定义通知");
}
});
case TYPE_MAIL:
return CheckResult.triggered(new Mail("2252193204@qq.com", "测试邮件主题", "测试邮件内容"), NotifyType.TYPE_MAIL);
case TYPE_MSG:
return CheckResult.triggered("短信通知", NotifyType.TYPE_MSG);
default:
return CheckResult.triggered("默认通知", null);
}
}
return CheckResult.nonTriggered();
}
}
```
> MonitorManager会在启动时扫描标注了`@MonitorItem`的类,并创建对应的实例,定时执行类中实现的`check()`方法,当方法返回`CheckResult`的`triggered`字段为`true`时,会自动执行`CheckResult`中传入的通知。
- 新增任务
```java
Msg<String> msg = monitorItemBeanManager.addMonitorTask(new BaseMonitorItem("CustomBean", "0/10 * * * * *") {
@Override
public CheckResult check() {
return CheckResult.triggered("动态新增通知", NotifyType.TYPE_LOG);
}
});
```
- 定时任务管理器:用于修改监控间隔、删除任务或者查看当前所有任务
```java
@Resource
MonitorItemBeanManager monitorItemBeanManager;
```
- 查看任务列表
```java
Map<String, BaseMonitorItem> monitorItemMap = monitorItemBeanManager.getMonitorItemMap();
```
- 设置任务监控频率
```java
Msg<String> msg = monitorItemBeanManager.setMonitorTaskCron(taskId, taskCron);
```
- 移除定时任务
```java
Msg<String> msg = monitorItemBeanManager.deleteMonitorTask(taskId);
```

View File

@ -3,7 +3,7 @@
<parent>
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
<groupId>com.hxuanyu</groupId>
<version>1.0.2</version>
<version>1.0.7-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -19,6 +19,10 @@
<groupId>com.hxuanyu</groupId>
<artifactId>notify-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.hxuanyu</groupId>
<artifactId>common-spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>

View File

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

View File

@ -1,6 +1,7 @@
package com.hxuanyu.monitor.common;
import com.hxuanyu.notify.enums.NotifyType;
import com.hxuanyu.notify.service.NotifyService;
/**
* 触发器通知
@ -13,21 +14,33 @@ public class CheckResult {
private boolean triggered;
private Object notifyContent;
private NotifyType notifyType;
private NotifyService.CustomNotify customNotify;
public CheckResult() {
}
public CheckResult(boolean triggered, Object notifyContent, NotifyType notifyType) {
private CheckResult(boolean triggered, Object notifyContent, NotifyType notifyType, NotifyService.CustomNotify customNotify) {
this.triggered = triggered;
this.notifyContent = notifyContent;
this.notifyType = notifyType;
this.customNotify = customNotify;
}
public NotifyService.CustomNotify getCustomNotify() {
return customNotify;
}
public void setCustomNotify(NotifyService.CustomNotify customNotify) {
this.customNotify = customNotify;
}
@Override
public String toString() {
return "Notify{" +
return "CheckResult{" +
"triggered=" + triggered +
", notifyContent='" + notifyContent + '\'' +
", notifyContent=" + notifyContent +
", notifyType=" + notifyType +
", customNotify=" + customNotify +
'}';
}
@ -37,18 +50,30 @@ public class CheckResult {
* @return 通知对象
*/
public static CheckResult nonTriggered() {
return new CheckResult(false, null, null);
return new CheckResult(false, null, null, null);
}
/**
* 通知触发需要传入通知信息
*
* @param notifyContent 通知内容
* @param notifyType 通知类型
* @param notifyType 通知类型
* @return 返回结果
*/
public static CheckResult triggered(Object notifyContent, NotifyType notifyType) {
return new CheckResult(true, notifyContent, notifyType);
return new CheckResult(true, notifyContent, notifyType, null);
}
/**
* 自定义通知类型触发
*
* @param customNotify 自定义通知
* @return 返回结果
*/
public static CheckResult triggered(NotifyService.CustomNotify customNotify) {
return new CheckResult(true, null, NotifyType.TYPE_CUSTOM, customNotify);
}
public boolean isTriggered() {

View File

@ -1,6 +1,6 @@
package com.hxuanyu.monitor.config;
import com.hxuanyu.monitor.utils.BeanUtils;
import com.hxuanyu.common.util.BeanUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.SchedulingException;
import org.springframework.scheduling.TaskScheduler;

View File

@ -1,5 +1,7 @@
package com.hxuanyu.monitor.manager;
import com.hxuanyu.common.message.Msg;
import com.hxuanyu.common.spring.SpringContextUtil;
import com.hxuanyu.monitor.annotation.MonitorItem;
import com.hxuanyu.monitor.base.BaseMonitorItem;
import com.hxuanyu.monitor.common.CheckResult;
@ -58,31 +60,56 @@ public class MonitorItemBeanManager implements ApplicationListener<ContextRefres
item.setMonitorItemName(name);
item.setCron(cron);
logger.info("获取到的Bean{}", item);
addMonitorTask(item);
Msg<String> msg = addMonitorTask(item);
logger.info("添加成功:{}", msg);
}
}
}
}
}
public void addMonitorTask(BaseMonitorItem item) {
String taskId = "ScheduledTask-" + item.getMonitorItemName();
public Msg<String> addMonitorTask(BaseMonitorItem item) {
String taskId = item.getMonitorItemName();
if (MONITOR_ITEM_MAP.containsKey(taskId)) {
return Msg.failed("任务已经存在,请执行修改操作");
}
MONITOR_ITEM_MAP.put(taskId, item);
logger.info("添加定时任务:{}, 执行周期:{}", taskId, item.getCron());
addTask(taskId, item);
return Msg.success("添加成功");
}
public void setMonitorTaskCron(String taskId, String cron) {
public Msg<String> addMonitorTask(String itemName, Class<? extends BaseMonitorItem> clazz, Object... args) {
BaseMonitorItem item = SpringContextUtil.registerBean(itemName, clazz, args);
return addMonitorTask(item);
}
public Msg<String> addMonitorTask(Class<? extends BaseMonitorItem> clazz, Object... args) {
return addMonitorTask(clazz.getSimpleName(), clazz, args);
}
public Msg<String> setMonitorTaskCron(String taskId, String cron) {
if (MONITOR_ITEM_MAP.containsKey(taskId)) {
schedulingConfigurer.cancelTriggerTask(taskId);
BaseMonitorItem item = MONITOR_ITEM_MAP.get(taskId);
item.setCron(cron);
addTask(taskId, item);
logger.info("修改定时任务:{}, 执行周期:{}", taskId,item.getCron());
return Msg.success("修改成功");
} else {
return Msg.failed("修改失败,该任务不存在");
}
}
public void deleteMonitorTask(String taskId) {
MONITOR_ITEM_MAP.remove(taskId);
schedulingConfigurer.cancelTriggerTask(taskId);
public Msg<String> deleteMonitorTask(String taskId) {
if (MONITOR_ITEM_MAP.containsKey(taskId)) {
MONITOR_ITEM_MAP.remove(taskId);
schedulingConfigurer.cancelTriggerTask(taskId);
logger.info("删除定时任务:{}", taskId);
return Msg.success("删除任务成功");
} else {
return Msg.failed("任务不存在");
}
}
private void addTask(String taskId, BaseMonitorItem item) {
@ -90,8 +117,13 @@ public class MonitorItemBeanManager implements ApplicationListener<ContextRefres
schedulingConfigurer.resetTriggerTask(taskId, new TriggerTask(() -> {
CheckResult checkResult = item.check();
if (checkResult.isTriggered()) {
logger.info("定时任务[{}]触发成功,发送通知:[{}]", taskId, checkResult.getNotifyContent());
notifyService.notify(checkResult.getNotifyContent(), NotifyType.MAIL_TYPE);
if (NotifyType.TYPE_CUSTOM.equals(checkResult.getNotifyType())) {
logger.info("定时任务[{}]触发成功,执行自定义通知", taskId);
notifyService.notify(checkResult.getCustomNotify());
} else {
logger.info("定时任务[{}]触发成功,发送通知:[{}]", taskId, checkResult.getNotifyContent());
notifyService.notify(checkResult.getNotifyContent(), checkResult.getNotifyType());
}
}
}, new CronTrigger(cron)));
}

View File

@ -0,0 +1,44 @@
## network-spring-boot-starter
### 简介
本模块对`HttpClient`进行了封装实现了GET、POST、PUT、DELETE等Http请求的同步和异步方法
### 引入
```java
<dependency>
<groupId>com.hxuanyu</groupId>
<artifactId>network-spring-boot-starter</artifactId>
<version>1</version>
</dependency>
```
### 使用
- 引入`HttpService`对象
```java
@Resource
HttpService httpService;
```
- 调用相关方法
```java
Msg<String> msg = httpService.doGet("https://baidu.com");
if (msg.isSuccess()) {
logger.info(msg.toString());
}
```
- 本模块依赖了common-spring-boot-starter模块引入本模块后会自动引入common模块下的相关类同时`HttpService`的方法返回值使用了common模块下的`Msg`统一封装

View File

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

View File

@ -1,7 +1,7 @@
package com.hxuanyu.network.service;
import com.hxuanyu.common.message.Msg;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import java.util.Map;
@ -20,7 +20,7 @@ public interface HttpService {
* @param url 请求地址
* @return 统一结果报文
*/
Msg<HttpEntity> doGetWithEntity(String url);
Msg<HttpResponse> doGetWithResponse(String url);
@ -47,7 +47,7 @@ public interface HttpService {
* @param url 请求地址
* @param listener 异步请求结果监听
*/
void doGetSyncWithEntity(String url, NetWorkListener<HttpEntity> listener);
void doGetSyncWithResponse(String url, NetWorkListener<HttpResponse> listener);
/**
* 发送get请求带请求参数
@ -66,7 +66,7 @@ public interface HttpService {
* @param params 请求参数集合
* @return 全局报文
*/
Msg<HttpEntity> doGetWithEntity(String url, Map<String, String> params);
Msg<HttpResponse> doGetWithResponse(String url, Map<String, String> params);
/**
* 异步Get请求
@ -84,7 +84,7 @@ public interface HttpService {
* @param params 请求参数
* @param listener 异步请求结果监听
*/
void doGetSyncWithEntity(String url, Map<String, String> params, NetWorkListener<HttpEntity> listener);
void doGetSyncWithResponse(String url, Map<String, String> params, NetWorkListener<HttpResponse> listener);
/**
* 发送get请求带请求头和请求参数
@ -104,7 +104,7 @@ public interface HttpService {
* @param params 请求参数集合
* @return 全局报文
*/
Msg<HttpEntity> doGetWithEntity(String url, Map<String, String> headers, Map<String, String> params);
Msg<HttpResponse> doGetWithResponse(String url, Map<String, String> headers, Map<String, String> params);
/**
* 异步Get请求
@ -124,7 +124,7 @@ public interface HttpService {
* @param params 请求体
* @param listener 异步结果监听器
*/
void doGetSyncWithEntity(String url, Map<String, String> headers, Map<String, String> params, NetWorkListener<HttpEntity> listener);
void doGetSyncWithResponse(String url, Map<String, String> headers, Map<String, String> params, NetWorkListener<HttpResponse> listener);
/**
* 发送post请求不带请求头和请求参数
@ -140,7 +140,7 @@ public interface HttpService {
* @param url 请求地址
* @return 统一报文
*/
Msg<HttpEntity> doPostWithEntity(String url);
Msg<HttpResponse> doPostWithResponse(String url);
/**
@ -157,7 +157,7 @@ public interface HttpService {
* @param url 请求地址
* @param listener 请求参数
*/
void doPostSyncWithEntity(String url, NetWorkListener<HttpEntity> listener);
void doPostSyncWithResponse(String url, NetWorkListener<HttpResponse> listener);
/**
* 同步Post请求
@ -176,7 +176,7 @@ public interface HttpService {
* @param params 请求参数
* @return 统一消息体
*/
Msg<HttpEntity> doPostWithEntity(String url, Map<String, String> params);
Msg<HttpResponse> doPostWithResponse(String url, Map<String, String> params);
/**
* 异步Post请求
@ -194,7 +194,7 @@ public interface HttpService {
* @param params 请求参数
* @param listener 异步请求结果监听
*/
void doPostSyncWithEntity(String url, Map<String, String> params, NetWorkListener<HttpEntity> listener);
void doPostSyncWithResponse(String url, Map<String, String> params, NetWorkListener<HttpResponse> listener);
/**
* 发送post请求带请求头和请求参数
@ -214,7 +214,7 @@ public interface HttpService {
* @param params 请求参数集合
* @return 统一返回报文
*/
Msg<HttpEntity> doPostWithEntity(String url, Map<String, String> headers, Map<String, String> params);
Msg<HttpResponse> doPostWithResponse(String url, Map<String, String> headers, Map<String, String> params);
/**
@ -235,7 +235,7 @@ public interface HttpService {
* @param params 请求参数
* @param listener 异步请求结果监听
*/
void doPostSyncWithEntity(String url, Map<String, String> headers, Map<String, String> params, NetWorkListener<HttpEntity> listener);
void doPostSyncWithResponse(String url, Map<String, String> headers, Map<String, String> params, NetWorkListener<HttpResponse> listener);
/**
* 发送put请求不带请求参数
@ -252,7 +252,7 @@ public interface HttpService {
* @param url 请求地址
* @return 统一消息返回报文
*/
Msg<HttpEntity> doPutWithEntity(String url);
Msg<HttpResponse> doPutWithResponse(String url);
/**
* 发送put请求带请求参数
@ -270,25 +270,23 @@ public interface HttpService {
* @param params 请求参数
* @return 统一消息报文
*/
Msg<HttpEntity> doPutWithEntity(String url, Map<String, String> params);
Msg<HttpResponse> doPutWithResponse(String url, Map<String, String> params);
/**
* 发送delete请求不带请求参数
*
* @param url 请求地址
* @return 统一返回报文
* @throws Exception 异常
*/
Msg<String> doDelete(String url) throws Exception;
Msg<String> doDelete(String url);
/**
* 发送delete请求不带请求参数
*
* @param url 请求地址
* @return 统一返回报文
* @throws Exception 异常
*/
Msg<HttpEntity> doDeleteWithEntity(String url) throws Exception;
Msg<HttpResponse> doDeleteWithResponse(String url);
/**
* 发送delete请求带请求参数
@ -296,9 +294,8 @@ public interface HttpService {
* @param url 请求地址
* @param params 请求参数
* @return 统一返回报文
* @throws Exception 异常
*/
Msg<String> doDelete(String url, Map<String, String> params) throws Exception;
Msg<String> doDelete(String url, Map<String, String> params);
/**
@ -307,9 +304,8 @@ public interface HttpService {
* @param url 请求地址
* @param params 请求参数
* @return 统一返回报文
* @throws Exception 异常
*/
Msg<HttpEntity> doDeleteWithEntity(String url, Map<String, String> params) throws Exception;
Msg<HttpResponse> doDeleteWithResponse(String url, Map<String, String> params);
interface NetWorkListener<T> {
/**

View File

@ -2,13 +2,12 @@ package com.hxuanyu.network.service.impl;
import com.hxuanyu.common.message.Msg;
import com.hxuanyu.network.service.HttpService;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.*;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
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.HttpClients;
import org.apache.http.message.BasicNameValuePair;
@ -21,6 +20,7 @@ import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.*;
import java.util.concurrent.ExecutorService;
@ -56,9 +56,9 @@ public class HttpServiceImpl implements HttpService {
@Override
public Msg<HttpEntity> doGetWithEntity(String url) {
public Msg<HttpResponse> doGetWithResponse(String url) {
return doGetWithEntity(url, null, null);
return doGetWithResponse(url, null, null);
}
@Override
@ -72,8 +72,8 @@ public class HttpServiceImpl implements HttpService {
}
@Override
public void doGetSyncWithEntity(String url, NetWorkListener<HttpEntity> listener) {
doGetSyncWithEntity(url, null, listener);
public void doGetSyncWithResponse(String url, NetWorkListener<HttpResponse> listener) {
doGetSyncWithResponse(url, null, listener);
}
@ -83,8 +83,8 @@ public class HttpServiceImpl implements HttpService {
}
@Override
public Msg<HttpEntity> doGetWithEntity(String url, Map<String, String> params) {
return doGetWithEntity(url, null, params);
public Msg<HttpResponse> doGetWithResponse(String url, Map<String, String> params) {
return doGetWithResponse(url, null, params);
}
@Override
@ -93,61 +93,70 @@ public class HttpServiceImpl implements HttpService {
}
@Override
public void doGetSyncWithEntity(String url, Map<String, String> params, NetWorkListener<HttpEntity> listener) {
doGetSyncWithEntity(url, null, params, listener);
public void doGetSyncWithResponse(String url, Map<String, String> params, NetWorkListener<HttpResponse> listener) {
doGetSyncWithResponse(url, null, params, listener);
}
@Override
public Msg<String> doGet(String url, Map<String, String> headers, Map<String, String> params) {
Msg<HttpEntity> httpEntityMsg = doGetWithEntity(url, headers, params);
return getStringMsg(httpEntityMsg);
// 创建httpClient对象
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
// 创建访问的地址
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
public Msg<HttpEntity> doGetWithEntity(String url, Map<String, String> headers, Map<String, String> params) {
public Msg<HttpResponse> doGetWithResponse(String url, Map<String, String> headers, Map<String, String> params) {
// 创建httpClient对象
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
// 创建访问的地址
URIBuilder uriBuilder = new URIBuilder(url);
if (params != null) {
Set<Map.Entry<String, String>> entrySet = params.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
uriBuilder.setParameter(entry.getKey(), entry.getValue());
}
}
// 创建http对象
HttpGet httpGet = new HttpGet(uriBuilder.build());
/*
* setConnectTimeout设置连接超时时间单位毫秒
* setConnectionRequestTimeout设置从connect Manager(连接池)获取Connection
* 超时时间单位毫秒这个属性是新加的属性因为目前版本是可以共享连接池的
* setSocketTimeout请求获取数据的超时时间(即响应时间)单位毫秒 如果访问一个接口多少时间内无法返回数据就直接放弃此次调用
*/
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
httpGet.setConfig(requestConfig);
// 设置请求头
packageHeader(headers, httpGet);
// 创建httpResponse对象
CloseableHttpResponse httpResponse = null;
try {
// 执行请求并获得响应结果
return getHttpClientResult(httpClient, httpGet);
} finally {
// 释放资源
release(httpClient);
}
HttpGet httpGet = buildHttpGet(url, params, headers);
// 执行请求并获得响应结果
return getHttpClientResult(httpClient, httpGet);
} catch (Exception e) {
logger.error("请求过程出现异常: {}", e.getMessage());
return Msg.failed("网络请求发生异常:" + 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);
if (params != null) {
Set<Map.Entry<String, String>> entrySet = params.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
uriBuilder.setParameter(entry.getKey(), entry.getValue());
}
}
// 创建http对象
HttpGet httpGet = new HttpGet(uriBuilder.build());
RequestConfig requestConfig = RequestConfig
.custom()
.setConnectTimeout(CONNECT_TIMEOUT)
.setConnectionRequestTimeout(CONNECT_TIMEOUT)
.setSocketTimeout(SOCKET_TIMEOUT)
.build();
httpGet.setConfig(requestConfig);
// 设置请求头
packageHeader(headers, httpGet);
return httpGet;
}
@Override
public void doGetSync(String url, Map<String, String> headers, Map<String, String> params, NetWorkListener<String> listener) {
@ -168,10 +177,10 @@ public class HttpServiceImpl implements HttpService {
}
@Override
public void doGetSyncWithEntity(String url, Map<String, String> headers, Map<String, String> params, NetWorkListener<HttpEntity> listener) {
public void doGetSyncWithResponse(String url, Map<String, String> headers, Map<String, String> params, NetWorkListener<HttpResponse> listener) {
executorService.execute(() -> {
try {
Msg<HttpEntity> msg = doGetWithEntity(url, headers, params);
Msg<HttpResponse> msg = doGetWithResponse(url, headers, params);
logger.debug("异步请求结果:{}", msg);
if (msg.getCode().equals(Msg.MSG_CODE_SUCCESS)) {
listener.onSuccess(msg);
@ -191,8 +200,8 @@ public class HttpServiceImpl implements HttpService {
}
@Override
public Msg<HttpEntity> doPostWithEntity(String url) {
return doPostWithEntity(url, null, null);
public Msg<HttpResponse> doPostWithResponse(String url) {
return doPostWithResponse(url, null, null);
}
@Override
@ -201,8 +210,8 @@ public class HttpServiceImpl implements HttpService {
}
@Override
public void doPostSyncWithEntity(String url, NetWorkListener<HttpEntity> listener) {
doPostSyncWithEntity(url, null, listener);
public void doPostSyncWithResponse(String url, NetWorkListener<HttpResponse> listener) {
doPostSyncWithResponse(url, null, listener);
}
@ -212,8 +221,8 @@ public class HttpServiceImpl implements HttpService {
}
@Override
public Msg<HttpEntity> doPostWithEntity(String url, Map<String, String> params) {
return doPostWithEntity(url, null, params);
public Msg<HttpResponse> doPostWithResponse(String url, Map<String, String> params) {
return doPostWithResponse(url, null, params);
}
@Override
@ -222,40 +231,32 @@ public class HttpServiceImpl implements HttpService {
}
@Override
public void doPostSyncWithEntity(String url, Map<String, String> params, NetWorkListener<HttpEntity> listener) {
doPostSyncWithEntity(url, null, params, listener);
public void doPostSyncWithResponse(String url, Map<String, String> params, NetWorkListener<HttpResponse> listener) {
doPostSyncWithResponse(url, null, params, listener);
}
@Override
public Msg<String> doPost(String url, Map<String, String> headers, Map<String, String> params) {
Msg<HttpEntity> httpEntityMsg = doPostWithEntity(url, headers, params);
return getStringMsg(httpEntityMsg);
Msg<HttpResponse> httpResponseMsg = doPostWithResponse(url, headers, params);
String stringResult;
try {
stringResult = EntityUtils.toString(httpResponseMsg.getData().getEntity());
return Msg.success("请求成功", stringResult);
} catch (IOException e) {
e.printStackTrace();
return Msg.failed("转换字符串失败:" + e.getMessage());
}
}
@Override
public Msg<HttpEntity> doPostWithEntity(String url, Map<String, String> headers, Map<String, String> params) {
public Msg<HttpResponse> doPostWithResponse(String url, Map<String, String> headers, Map<String, String> params) {
// 创建httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建http对象
HttpPost httpPost = new HttpPost(url);
/*
* setConnectTimeout设置连接超时时间单位毫秒
* setConnectionRequestTimeout设置从connect Manager(连接池)获取Connection
* 超时时间单位毫秒这个属性是新加的属性因为目前版本是可以共享连接池的
* setSocketTimeout请求获取数据的超时时间(即响应时间)单位毫秒 如果访问一个接口多少时间内无法返回数据就直接放弃此次调用
*/
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
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);
// 封装请求参数
try {
packageParam(params, httpPost);
@ -265,13 +266,6 @@ public class HttpServiceImpl implements HttpService {
e.printStackTrace();
logger.error("请求过程出现异常: {}", e.getMessage());
return Msg.failed("请求过程出现异常: " + e.getMessage());
} finally {
// 释放资源
try {
release(httpClient);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@ -293,10 +287,10 @@ public class HttpServiceImpl implements HttpService {
}
@Override
public void doPostSyncWithEntity(String url, Map<String, String> headers, Map<String, String> params, NetWorkListener<HttpEntity> listener) {
public void doPostSyncWithResponse(String url, Map<String, String> headers, Map<String, String> params, NetWorkListener<HttpResponse> listener) {
executorService.execute(() -> {
try {
Msg<HttpEntity> msg = doPostWithEntity(url, headers, params);
Msg<HttpResponse> msg = doPostWithResponse(url, headers, params);
if (msg.getCode().equals(Msg.MSG_CODE_SUCCESS)) {
listener.onSuccess(msg);
} else if (msg.getCode().equals(Msg.MSG_CODE_FAILED)) {
@ -316,18 +310,26 @@ public class HttpServiceImpl implements HttpService {
}
@Override
public Msg<HttpEntity> doPutWithEntity(String url) {
return doPutWithEntity(url, null);
public Msg<HttpResponse> doPutWithResponse(String url) {
return doPutWithResponse(url, null);
}
@Override
public Msg<String> doPut(String url, Map<String, String> params) {
Msg<HttpEntity> httpEntityMsg = doPutWithEntity(url, params);
return getStringMsg(httpEntityMsg);
Msg<HttpResponse> httpResponseMsg = doPutWithResponse(url, params);
String stringResult;
try {
stringResult = EntityUtils.toString(httpResponseMsg.getData().getEntity());
return Msg.success("请求成功", stringResult);
} catch (IOException e) {
e.printStackTrace();
return Msg.failed("转换字符串失败:" + e.getMessage());
}
}
@Override
public Msg<HttpEntity> doPutWithEntity(String url, Map<String, String> params) {
public Msg<HttpResponse> doPutWithResponse(String url, Map<String, String> params) {
// 创建httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
@ -342,41 +344,26 @@ public class HttpServiceImpl implements HttpService {
e.printStackTrace();
logger.error("请求过程出现异常: {}", 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
public Msg<String> doDelete(String url) {
Msg<HttpEntity> httpEntityMsg = doDeleteWithEntity(url);
return getStringMsg(httpEntityMsg);
Msg<HttpResponse> httpResponseMsg = doDeleteWithResponse(url);
String stringResult;
try {
stringResult = EntityUtils.toString(httpResponseMsg.getData().getEntity());
return Msg.success("请求成功", stringResult);
} catch (IOException e) {
e.printStackTrace();
return Msg.failed("转换字符串失败:" + e.getMessage());
}
}
@Override
public Msg<HttpEntity> doDeleteWithEntity(String url) {
public Msg<HttpResponse> doDeleteWithResponse(String url) {
// 创建httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpDelete httpDelete = new HttpDelete(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
@ -386,29 +373,30 @@ public class HttpServiceImpl implements HttpService {
} catch (Exception e) {
e.printStackTrace();
return Msg.failed("转换String时发生IO异常");
} finally {
try {
release(httpClient);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public Msg<String> doDelete(String url, Map<String, String> params) {
Msg<HttpEntity> httpEntityMsg = doDeleteWithEntity(url, params);
return getStringMsg(httpEntityMsg);
Msg<HttpResponse> httpResponseMsg = doDeleteWithResponse(url, params);
String stringResult;
try {
stringResult = EntityUtils.toString(httpResponseMsg.getData().getEntity());
return Msg.success("请求成功", stringResult);
} catch (IOException e) {
e.printStackTrace();
return Msg.failed("转换字符串失败:" + e.getMessage());
}
}
@Override
public Msg<HttpEntity> doDeleteWithEntity(String url, Map<String, String> params){
public Msg<HttpResponse> doDeleteWithResponse(String url, Map<String, String> params) {
if (params == null) {
params = new HashMap<>(0);
}
params.put("_method", "delete");
return doPostWithEntity(url, params);
return doPostWithResponse(url, params);
}
@ -438,27 +426,15 @@ public class HttpServiceImpl implements HttpService {
}
}
private Msg<HttpEntity> getHttpClientResult(CloseableHttpClient httpClient, HttpRequestBase httpMethod) {
private Msg<HttpResponse> getHttpClientResult(CloseableHttpClient httpClient, HttpRequestBase httpMethod) throws IOException {
// 执行请求
CloseableHttpResponse httpResponse;
try {
logger.info("执行请求:{},请求方式:{}", httpMethod.getURI().toString(), httpMethod.getMethod());
httpResponse = httpClient.execute(httpMethod);
// 获取返回结果
if (httpResponse != null && httpResponse.getStatusLine() != null) {
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());
logger.info("执行请求:{},请求方式:{}", httpMethod.getURI().toString(), httpMethod.getMethod());
httpResponse = httpClient.execute(httpMethod);
// 获取返回结果
if (httpResponse != null && httpResponse.getStatusLine() != null) {
return Msg.success("请求成功", httpResponse);
}
return Msg.failed(HttpStatus.SC_INTERNAL_SERVER_ERROR + "请求失败");
}

View File

@ -0,0 +1,103 @@
## notify-spring-boot-starter
### 简介
本模块用于对用户进行通知,目前支持邮件通知,后续会加入短信等更多类型
- MAIL对springboot的mail模块进行了封装实现了一个邮件发送队列并支持html作为邮件内容
### 引入
```java
<dependency>
<groupId>com.hxuanyu</groupId>
<artifactId>notify-spring-boot-starter</artifactId>
<version>1.0.4</version>
</dependency>
```
### 使用
#### Mail
- 引入`NotifyService`对象
```java
@Resource
NotifyService notifyService;
```
- 添加Mail配置
```java
notify:
mail:
host: your mail host
protocol: smtp
default-encoding: UTF-8
password: your passwd
username: your account
port: 587
properties:
mail:
debug: false
stmp:
socketFactory:
class: javax.net.ssl.SSLSocketFactory
```
- 调用相关方法
```java
notifyService.notify(new Mail("2252193204@qq.com", "test subject", "test success"), NotifyType.TYPE_MAIL);
```
#### 短信和日志
- 引入`NotifyService`对象
```java
@Resource
NotifyService notifyService;
```
- 调用相关方法
```java
notifyService.notify("短信通知方式", NotifyType.TYPE_MSG);
notifyService.notify("日志输出方式", NotifyType.TYPE_LOG);
```
#### 自定义
- 引入`NotifyService`对象
```java
@Resource
NotifyService notifyService;
```
- 调用方法
```java
notifyService.notify(new NotifyService.CustomNotify() {
@Override
public void onNotify() {
// do some things
}
});
```

View File

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

View File

@ -0,0 +1,108 @@
package com.hxuanyu.notify.config;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author hxuanyu
*/
@ConfigurationProperties(prefix = "notify.mail")
@ConditionalOnProperty(prefix = "notify.mail", havingValue = "true")
public class MailProperties {
private static final Charset DEFAULT_CHARSET;
private String host;
private Integer port;
private String username;
private String password;
private String protocol = "smtp";
private Charset defaultEncoding;
private Map<String, String> properties;
private String jndiName;
private Integer interval = 10000;
public MailProperties() {
this.defaultEncoding = DEFAULT_CHARSET;
this.properties = new HashMap();
}
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
public Integer getInterval() {
return interval;
}
public void setInterval(Integer interval) {
this.interval = interval;
}
public String getHost() {
return this.host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return this.port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getProtocol() {
return this.protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public Charset getDefaultEncoding() {
return this.defaultEncoding;
}
public void setDefaultEncoding(Charset defaultEncoding) {
this.defaultEncoding = defaultEncoding;
}
public Map<String, String> getProperties() {
return this.properties;
}
public void setJndiName(String jndiName) {
this.jndiName = jndiName;
}
public String getJndiName() {
return this.jndiName;
}
static {
DEFAULT_CHARSET = StandardCharsets.UTF_8;
}
}

View File

@ -1,18 +1,57 @@
package com.hxuanyu.notify.config;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.*;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.*;
/**
* @author hxuanyu
*/
@Configuration
@ComponentScan("com.hxuanyu.notify.service")
@ComponentScan("com.hxuanyu.notify")
@EnableConfigurationProperties(MailProperties.class)
public class NotifyConfiguration {
@Autowired
MailProperties mailProperties;
@Bean
JavaMailSenderImpl mailSender(MailProperties properties) {
JavaMailSenderImpl sender = new JavaMailSenderImpl();
this.applyProperties(properties, sender);
return sender;
}
private void applyProperties(MailProperties properties, JavaMailSenderImpl sender) {
sender.setHost(properties.getHost());
if (properties.getPort() != null) {
sender.setPort(properties.getPort());
}
sender.setUsername(properties.getUsername());
sender.setPassword(properties.getPassword());
sender.setProtocol(properties.getProtocol());
if (properties.getDefaultEncoding() != null) {
sender.setDefaultEncoding(properties.getDefaultEncoding().name());
}
if (!properties.getProperties().isEmpty()) {
sender.setJavaMailProperties(this.asProperties(properties.getProperties()));
}
}
private Properties asProperties(Map<String, String> source) {
Properties properties = new Properties();
properties.putAll(source);
return properties;
}
@Bean(name = "mailExecutorService")
public ExecutorService mailExecutorService() {
// 使用 ThreadFactoryBuilder 创建自定义线程名称的 ThreadFactory

View File

@ -10,10 +10,12 @@ public enum NotifyType {
/**
* 邮件类型
* 通知类型
*/
MAIL_TYPE("邮件"),
SMS_TYPE("短信");
TYPE_LOG("日志输出"),
TYPE_MAIL("邮件"),
TYPE_CUSTOM("自定义"),
TYPE_MSG("短信");
private final String typeName;

View File

@ -1,7 +1,6 @@
package com.hxuanyu.notify.service;
import com.hxuanyu.notify.enums.NotifyType;
import org.springframework.stereotype.Service;
/**
* 通知服务
@ -17,4 +16,18 @@ public interface NotifyService {
* @param notifyType 通知类型
*/
void notify(Object content, NotifyType notifyType);
/**
* 自定义通知
*
* @param customNotify 自定义通知接口需手动实现
*/
void notify(CustomNotify customNotify);
interface CustomNotify {
/**
* 通知
*/
void onNotify();
}
}

View File

@ -2,18 +2,19 @@ package com.hxuanyu.notify.service.impl;
import com.hxuanyu.notify.common.MailQueue;
import com.hxuanyu.notify.config.MailProperties;
import com.hxuanyu.notify.model.Mail;
import com.hxuanyu.notify.service.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@ -27,15 +28,14 @@ import java.util.concurrent.ExecutorService;
*/
@Service
public class MailServiceImpl implements MailService {
@Resource
MailProperties mailProperties;
private static final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
private static boolean isRunning = true;
ExecutorService executor;
@Resource
private JavaMailSender javaMailSender;
private static JavaMailSender javaMailSender;
private static TemplateEngine templateEngine;
private static String defaultFrom;
@ -45,7 +45,7 @@ public class MailServiceImpl implements MailService {
executor.submit(new PollMail());
}
static class PollMail implements Runnable {
class PollMail implements Runnable {
@Override
public void run() {
while (isRunning) {
@ -55,7 +55,7 @@ public class MailServiceImpl implements MailService {
if (mail != null) {
//可以设置延时 以及重复校验等等操作
sendMailSync(mail);
Thread.sleep(10000);
Thread.sleep(mailProperties.getInterval());
}
} catch (Exception e) {
e.printStackTrace();
@ -71,10 +71,14 @@ public class MailServiceImpl implements MailService {
}
private static void sendMailSync(Mail mail) {
private void sendMailSync(Mail mail) {
if (mailProperties == null){
logger.error("未配置邮件信息,请在配置文件中添加配置信息后再发送邮件");
return;
}
String from = mail.getFrom();
if (from == null) {
from = defaultFrom;
from = mailProperties.getUsername();
logger.info("未传入发件人,从配置中读取:{}", from);
}
MimeMessage mimeMessage;
@ -87,12 +91,7 @@ public class MailServiceImpl implements MailService {
mimeMessageHelper.setFrom(from);
mimeMessageHelper.setTo(mail.getTo());
mimeMessageHelper.setSubject(mail.getSubject());
// 利用 Thymeleaf 引擎渲染 HTML
Context context = new Context();
// 设置注入的变量
context.setVariable("templates/mail", mail);
// 模板设置为 "mail"
String content = templateEngine.process("templates/mail/mail", context);
String content = mail.getContent();
// 设置邮件内容
// true 表示开启 html
mimeMessageHelper.setText(content, true);
@ -112,24 +111,9 @@ public class MailServiceImpl implements MailService {
}
@Resource
public void setJavaMailSender(JavaMailSender javaMailSender) {
MailServiceImpl.javaMailSender = javaMailSender;
}
@Autowired
public void setExecutor(@Qualifier("mailExecutorService") ExecutorService executor) {
this.executor = executor;
}
@Autowired
public void setTemplateEngine(TemplateEngine templateEngine) {
MailServiceImpl.templateEngine = templateEngine;
}
@Value("${spring.mail.username}")
public void setDefaultFrom(String defaultFrom) {
MailServiceImpl.defaultFrom = defaultFrom;
}
}

View File

@ -25,15 +25,26 @@ public class NotifyServiceImpl implements NotifyService {
@Override
public void notify(Object content, NotifyType notifyType) {
if (NotifyType.MAIL_TYPE.equals(notifyType)) {
logger.debug("通知内容:{},通知类型:{}", notifyType, notifyType);
if (NotifyType.TYPE_MAIL.equals(notifyType)) {
sendMail(content);
} else if (NotifyType.SMS_TYPE.equals(notifyType)) {
} else if (NotifyType.TYPE_MSG.equals(notifyType)) {
sendSms(content);
} else if (NotifyType.TYPE_LOG.equals(notifyType)) {
logger.info("新通知:{}", content);
} else if (NotifyType.TYPE_CUSTOM.equals(notifyType)) {
logger.warn("您选择了自定义通知请实现CustomNotify接口并调用 notify(CustomNotify customNotify方法)");
} else {
logger.info("未匹配到通知类型:[{}]", content);
}
}
@Override
public void notify(CustomNotify customNotify) {
logger.debug("执行自定义通知");
customNotify.onNotify();
}
private void sendSms(Object content) {
logger.info("即将发送短信通知,通知内容:{}", content);
}

View File

@ -0,0 +1,48 @@
{
"properties": [
{
"name": "notify.mail.host",
"type": "java.lang.String",
"description": "Description for notify.mail.host."
},
{
"name": "notify.mail.protocol",
"type": "java.lang.String",
"description": "Description for notify.mail.protocol."
},
{
"name": "notify.mail.default-encoding",
"type": "java.lang.String",
"description": "Description for notify.mail.default-encoding."
},
{
"name": "notify.mail.password",
"type": "java.lang.String",
"description": "Description for notify.mail.password."
},
{
"name": "notify.mail.username",
"type": "java.lang.String",
"description": "Description for notify.mail.username."
},
{
"name": "notify.mail.port",
"type": "java.lang.String",
"description": "Description for notify.mail.port."
},
{
"name": "notify.mail.properties.mail.debug",
"type": "java.lang.String",
"description": "Description for notify.mail.properties.mail.debug."
},
{
"name": "notify.mail.properties.mail.interval",
"type": "java.lang.String",
"description": "Description for notify.mail.properties.mail.debug."
},
{
"name": "notify.mail.properties.stmp.socketFactory.class",
"type": "java.lang.String",
"description": "Description for notify.mail.properties.stmp.socketFactory.class."
}
] }

View File

@ -1,129 +0,0 @@
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<!--${government}-->
<!--${title}-->
<!--${suggestion}-->
<!--${deadline}-->
<!--${secret}-->
<!--${url}-->
<!--${officeName}-->
<!--${createTime}-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<title>${title}</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
<style>
.contact-clean {
background: #f1f7fc;
padding: 80px 0;
}
a {
text-decoration: none;
}
@media (max-width: 767px) {
.contact-clean {
padding: 20px 0;
}
}
.contact-clean form {
max-width: 480px;
width: 90%;
margin: 0 auto;
background-color: #ffffff;
padding: 40px;
border-radius: 4px;
color: #505e6c;
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.1);
}
@media (max-width: 767px) {
.contact-clean form {
padding: 30px;
}
}
.contact-clean h2 {
margin-top: 5px;
font-weight: bold;
font-size: 28px;
margin-bottom: 36px;
color: inherit;
}
.contact-clean .form-group:last-child {
margin-bottom: 5px;
}
.contact-clean form .form-control {
background: #fff;
border-radius: 2px;
box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.05);
outline: none;
color: inherit;
padding-left: 12px;
height: 42px;
}
.contact-clean form .form-control:focus {
border: 1px solid #b2b2b2;
}
.contact-clean form textarea.form-control {
min-height: 100px;
max-height: 260px;
padding-top: 10px;
resize: vertical;
}
.contact-clean form .btn {
padding: 16px 32px;
border: none;
background: none;
box-shadow: none;
text-shadow: none;
opacity: 0.9;
text-transform: uppercase;
font-weight: bold;
font-size: 13px;
letter-spacing: 0.4px;
line-height: 1;
outline: none !important;
}
.contact-clean form .btn:hover {
opacity: 1;
}
.contact-clean form .btn:active {
transform: translateY(1px);
}
.contact-clean form .btn-primary {
background-color: #055ada !important;
margin-top: 15px;
color: #fff;
}
</style>
</head>
<body>
<div class="contact-clean">
<form method="post">
<h2 class="text-center" th:text="${mail.getSubject()}"></h2>
<p th:utext="${mail.getContent()}"></p>
<div class="alert alert-danger" role="alert">
<span>
<strong>注意</strong>本邮件由系统自动发送,请勿回复本邮件,如果邮件内容您并不知情,请忽略本邮件
</span>
</div>
</form>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
</body>
</html>

View File

@ -4,7 +4,7 @@
<groupId>com.hxuanyu</groupId>
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
<version>1.0.2</version>
<version>1.0.7-SNAPSHOT</version>
<name>hxuanyu-spring-boot-starter-parent</name>
<description>MonitorPushingParent</description>
<packaging>pom</packaging>
@ -19,7 +19,7 @@
<connection>scm:git:https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter.git</connection>
<developerConnection>scm:git:https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter.git</developerConnection>
<url>https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter</url>
<tag>v1.0.2</tag>
<tag>v1.0.0</tag>
</scm>
<developers>
<developer>
@ -34,6 +34,7 @@
<module>network-spring-boot-starter</module>
<module>notify-spring-boot-starter</module>
<module>monitor-spring-boot-starter</module>
<module>test</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
@ -165,7 +166,7 @@
<tagNameFormat>v@{project.version}</tagNameFormat>
<autoVersionSubmodules>true</autoVersionSubmodules>
<password>${git.password}</password>
<username>hxuanyu</username>
<username>${git.username}</username>
</configuration>
</plugin>
</plugins>

59
test/pom.xml Normal file
View File

@ -0,0 +1,59 @@
<?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>

View File

@ -0,0 +1,17 @@
package com.hxuanyu.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* TODO
*
* @author hanxuanyu
* @version 1.0
*/
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}

View File

@ -0,0 +1,58 @@
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 * * * * *");
}
}

View File

@ -0,0 +1,52 @@
package com.hxuanyu.test.controller;
import com.hxuanyu.common.message.Msg;
import com.hxuanyu.monitor.manager.MonitorItemBeanManager;
import com.hxuanyu.notify.model.Mail;
import com.hxuanyu.notify.service.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* 测试控制器
*
* @author hanxuanyu
* @version 1.0
*/
@RestController
public class NetworkTestController {
private final Logger logger = LoggerFactory.getLogger(NetworkTestController.class);
@ResponseBody
@GetMapping("/")
public Msg<String> testGet() {
logger.info("收到GET请求");
return Msg.success("GET 测试");
}
@ResponseBody
@PostMapping("/")
public Msg<String> testPost() {
logger.info("收到POST请求");
return Msg.success("POST 测试");
}
@ResponseBody
@PutMapping("/")
public Msg<String> testPut() {
logger.info("收到PUT请求");
return Msg.success("PUT 测试");
}
@ResponseBody
@DeleteMapping("/")
public Msg<String> testDelete() {
logger.info("收到DELETE请求");
return Msg.success("DELETE 测试");
}
}

View File

@ -0,0 +1,53 @@
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();
}
}

View File

@ -1,14 +1,15 @@
spring:
notify:
mail:
host: smtp.domain
host: smtp.qq.com
protocol: smtp
default-encoding: UTF-8
password: your_password
username: your_username
password: your pwd
username: your account
port: 587
properties:
mail:
debug: false
stmp:
socketFactory:
class: javax.net.ssl.SSLSocketFactory
class: javax.net.ssl.SSLSocketFactory
interval: 5000

View File

@ -0,0 +1,51 @@
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();
}
}
}
}

View File

@ -0,0 +1,22 @@
package com.hxuanyu.starter.test.common;
import com.hxuanyu.common.message.Msg;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MsgTest {
private final Logger logger = LoggerFactory.getLogger(MsgTest.class);
@Test
public void test() {
Msg<String> msg = Msg.success("测试内容", "测试体");
logger.info(msg.toString());
assert msg.isSuccess();
msg = Msg.failed("失败消息");
logger.info(msg.toString());
assert msg.isFailed();
}
}

View File

@ -0,0 +1,27 @@
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());
}
}
}

View File

@ -0,0 +1,53 @@
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();
}
}

View File

@ -0,0 +1,31 @@
package com.hxuanyu.starter.test.notify;
import com.hxuanyu.notify.enums.NotifyType;
import com.hxuanyu.notify.model.Mail;
import com.hxuanyu.notify.service.NotifyService;
import com.hxuanyu.test.MainApplication;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static java.lang.Thread.sleep;
@SpringBootTest(classes = MainApplication.class)
public class NotifyTest {
@Autowired
NotifyService notifyService;
@Test
public void testNotify() throws InterruptedException {
notifyService.notify(new Mail("2252193204@qq.com", "test subject", "test success"), NotifyType.TYPE_MAIL);
sleep(2000);
notifyService.notify("短信通知方式", NotifyType.TYPE_MSG);
notifyService.notify("日志输出方式", NotifyType.TYPE_LOG);
notifyService.notify(() -> {
// do some things
});
}
}