Compare commits
9 Commits
0c074c046f
...
v1.0.5
Author | SHA1 | Date | |
---|---|---|---|
1e01449a7b | |||
e91812f942 | |||
ddf4b4bc07 | |||
38416ff420 | |||
b5bc5b883e | |||
f727de32d0 | |||
ca5d97c7a0 | |||
95b41916dc | |||
cb418ba35a |
46
README.md
46
README.md
@@ -15,50 +15,20 @@
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 1. 引入maven依赖
|
||||
|
||||
本项目已经上传到中央仓库,使用时在pom文件中添加如下依赖即可:
|
||||
|
||||
- network-spring-boot-starter
|
||||
### 引入maven依赖
|
||||
|
||||
```xml
|
||||
<!--network-spring-boot-starter-->
|
||||
<dependency>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<artifactId>network-spring-boot-starter</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
||||
<version>1.0.4</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
- notify-spring-boot-starter
|
||||
**子模块的依赖详见子模块的Readme**:
|
||||
|
||||
```xml
|
||||
<!--notify-spring-boot-starter-->
|
||||
<dependency>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<artifactId>notify-spring-boot-starter</artifactId>
|
||||
<version>1.0.1</version>
|
||||
</dependency>
|
||||
```
|
||||
- [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)
|
||||
|
||||
- monitor-spring-boot-starter
|
||||
|
||||
```xml
|
||||
<!--monitor-spring-boot-starter-->
|
||||
<dependency>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<artifactId>monitor-spring-boot-starter</artifactId>
|
||||
<version>1.0.1</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
- monitor-spring-boot-starter
|
||||
|
||||
```xml
|
||||
<!--monitor-spring-boot-starter-->
|
||||
<dependency>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<artifactId>monitor-spring-boot-starter</artifactId>
|
||||
<version>1.0.1</version>
|
||||
</dependency>
|
||||
```
|
58
common-spring-boot-starter/README.md
Normal file
58
common-spring-boot-starter/README.md
Normal file
@@ -0,0 +1,58 @@
|
||||
## common-spring-boot-starter
|
||||
|
||||
### 简介
|
||||
|
||||
本模块封装了常用的工具类,对外提供基础的功能,当前包含:
|
||||
|
||||
- Msg:统一消息对象
|
||||
|
||||
|
||||
|
||||
### 引入
|
||||
|
||||
```java
|
||||
|
||||
<dependency>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<artifactId>common-spring-boot-starter</artifactId>
|
||||
<version>1.0.4</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
|
||||
|
||||
### 功能
|
||||
|
||||
#### Msg
|
||||
|
||||
用于规范方法间调用返回的结果,即方法无论调用成功还是失败,都以Msg对象作为返回值,实际的返回值封装在msg中,调用者可根据Msg的状态码进行状态判断,成功则从Msg对象中取值,失败则做相应的失败处理,避免了直接调用时可能出现的空指针问题。
|
||||
|
||||
|
||||
|
||||
使用方式:
|
||||
|
||||
```java
|
||||
public void test() {
|
||||
Msg<String> msg = doSomeThing("args");
|
||||
if (msg.isSuccess()) {
|
||||
// do some things
|
||||
} else if (msg.isFailed()) {
|
||||
// do some things
|
||||
}
|
||||
}
|
||||
|
||||
private Msg<String> doSomeThing(String args) {
|
||||
if (args != null) {
|
||||
return Msg.success("your success msg", "your data");
|
||||
} else {
|
||||
return Msg.failed("your failed msg");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
- 如果方法调用成功,则在返回值中传入成功消息以及可选的返回对象,该对象为泛型,可以在声明方法时指定
|
||||
- 如果方法调用失败,则在返回值中传入失败原因,**注意,失败时不可设置data字段,只能传入失败消息**
|
||||
- 调用者可以根据错误码或者直接调用`msg.isSuccess()`方法判断是否调用成功,并对结果进行相应处理
|
||||
- 在链式调用时,位于中间的方法不建议直接将上游请求到的msg结果传递给下游,应重新创建新的msg对象
|
@@ -3,7 +3,7 @@
|
||||
<parent>
|
||||
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<version>1.0.4-SNAPSHOT</version>
|
||||
<version>1.0.5</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@@ -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 {
|
||||
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
@@ -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);
|
@@ -0,0 +1 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.hxuanyu.common.config.CommonConfiguration
|
111
monitor-spring-boot-starter/README.md
Normal file
111
monitor-spring-boot-starter/README.md
Normal file
@@ -0,0 +1,111 @@
|
||||
## monitor-spring-boot-starter
|
||||
|
||||
### 简介
|
||||
|
||||
- 本模块实现了一个定时监控并推送通知的服务,适用于处理一些定时监控并实时通知的场景,举个栗子,可以用来监控火车票,当检测到有票之后触发通知,通过配置好的通知方式告知我们,目前内置了邮件通知、日志输出方式,以及自定义通知。
|
||||
|
||||
|
||||
|
||||
### 引入
|
||||
|
||||
```java
|
||||
<dependency>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<artifactId>monitor-spring-boot-starter</artifactId>
|
||||
<version>1.0.4</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
|
||||
|
||||
### 使用
|
||||
|
||||
- 创建监控项
|
||||
|
||||
```java
|
||||
@MonitorItem(cron = "0/20 * * * * *")
|
||||
public class TestMonitorItem extends BaseMonitorItem {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(TestMonitorItem.class);
|
||||
|
||||
@Override
|
||||
public CheckResult check() {
|
||||
|
||||
double random = Math.random();
|
||||
int result = (int) (random * NotifyType.values().length);
|
||||
logger.info("随机索引值:{}", result);
|
||||
if (NotifyType.values().length > 0) {
|
||||
NotifyType notifyType = NotifyType.values()[result];
|
||||
switch (notifyType) {
|
||||
case TYPE_LOG:
|
||||
return CheckResult.triggered("日志输出", NotifyType.TYPE_LOG);
|
||||
case TYPE_CUSTOM:
|
||||
return CheckResult.triggered(new NotifyService.CustomNotify() {
|
||||
@Override
|
||||
public void onNotify() {
|
||||
logger.info("自定义通知");
|
||||
}
|
||||
});
|
||||
case TYPE_MAIL:
|
||||
return CheckResult.triggered(new Mail("2252193204@qq.com", "测试邮件主题", "测试邮件内容"), NotifyType.TYPE_MAIL);
|
||||
case TYPE_MSG:
|
||||
return CheckResult.triggered("短信通知", NotifyType.TYPE_MSG);
|
||||
default:
|
||||
return CheckResult.triggered("默认通知", null);
|
||||
}
|
||||
}
|
||||
|
||||
return CheckResult.nonTriggered();
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
> MonitorManager会在启动时扫描标注了`@MonitorItem`的类,并创建对应的实例,定时执行类中实现的`check()`方法,当方法返回`CheckResult`的`triggered`字段为`true`时,会自动执行`CheckResult`中传入的通知。
|
||||
|
||||
|
||||
|
||||
- 新增任务
|
||||
|
||||
```java
|
||||
Msg<String> msg = monitorItemBeanManager.addMonitorTask(new BaseMonitorItem("CustomBean", "0/10 * * * * *") {
|
||||
@Override
|
||||
public CheckResult check() {
|
||||
return CheckResult.triggered("动态新增通知", NotifyType.TYPE_LOG);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
|
||||
- 定时任务管理器:用于修改监控间隔、删除任务或者查看当前所有任务
|
||||
|
||||
```java
|
||||
@Resource
|
||||
MonitorItemBeanManager monitorItemBeanManager;
|
||||
```
|
||||
|
||||
|
||||
|
||||
- 查看任务列表
|
||||
|
||||
```java
|
||||
Map<String, BaseMonitorItem> monitorItemMap = monitorItemBeanManager.getMonitorItemMap();
|
||||
```
|
||||
|
||||
|
||||
|
||||
- 设置任务监控频率
|
||||
|
||||
```java
|
||||
Msg<String> msg = monitorItemBeanManager.setMonitorTaskCron(taskId, taskCron);
|
||||
```
|
||||
|
||||
|
||||
|
||||
- 移除定时任务
|
||||
|
||||
```java
|
||||
Msg<String> msg = monitorItemBeanManager.deleteMonitorTask(taskId);
|
||||
```
|
||||
|
@@ -3,7 +3,7 @@
|
||||
<parent>
|
||||
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<version>1.0.4-SNAPSHOT</version>
|
||||
<version>1.0.5</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@@ -19,6 +19,10 @@
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<artifactId>notify-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<artifactId>common-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@@ -1,6 +1,7 @@
|
||||
package com.hxuanyu.monitor.common;
|
||||
|
||||
import com.hxuanyu.notify.enums.NotifyType;
|
||||
import com.hxuanyu.notify.service.NotifyService;
|
||||
|
||||
/**
|
||||
* 触发器通知
|
||||
@@ -13,21 +14,33 @@ public class CheckResult {
|
||||
private boolean triggered;
|
||||
private Object notifyContent;
|
||||
private NotifyType notifyType;
|
||||
private NotifyService.CustomNotify customNotify;
|
||||
|
||||
public CheckResult() {
|
||||
}
|
||||
|
||||
public CheckResult(boolean triggered, Object notifyContent, NotifyType notifyType) {
|
||||
private CheckResult(boolean triggered, Object notifyContent, NotifyType notifyType, NotifyService.CustomNotify customNotify) {
|
||||
this.triggered = triggered;
|
||||
this.notifyContent = notifyContent;
|
||||
this.notifyType = notifyType;
|
||||
this.customNotify = customNotify;
|
||||
}
|
||||
|
||||
public NotifyService.CustomNotify getCustomNotify() {
|
||||
return customNotify;
|
||||
}
|
||||
|
||||
public void setCustomNotify(NotifyService.CustomNotify customNotify) {
|
||||
this.customNotify = customNotify;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Notify{" +
|
||||
return "CheckResult{" +
|
||||
"triggered=" + triggered +
|
||||
", notifyContent='" + notifyContent + '\'' +
|
||||
", notifyContent=" + notifyContent +
|
||||
", notifyType=" + notifyType +
|
||||
", customNotify=" + customNotify +
|
||||
'}';
|
||||
}
|
||||
|
||||
@@ -37,18 +50,30 @@ public class CheckResult {
|
||||
* @return 通知对象
|
||||
*/
|
||||
public static CheckResult nonTriggered() {
|
||||
return new CheckResult(false, null, null);
|
||||
return new CheckResult(false, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通知触发,需要传入通知信息
|
||||
*
|
||||
* @param notifyContent 通知内容
|
||||
* @param notifyType 通知类型
|
||||
* @param notifyType 通知类型
|
||||
* @return 返回结果
|
||||
*/
|
||||
public static CheckResult triggered(Object notifyContent, NotifyType notifyType) {
|
||||
return new CheckResult(true, notifyContent, notifyType);
|
||||
return new CheckResult(true, notifyContent, notifyType, null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 自定义通知类型触发
|
||||
*
|
||||
* @param customNotify 自定义通知
|
||||
* @return 返回结果
|
||||
*/
|
||||
public static CheckResult triggered(NotifyService.CustomNotify customNotify) {
|
||||
return new CheckResult(true, null, NotifyType.TYPE_CUSTOM, customNotify);
|
||||
}
|
||||
|
||||
public boolean isTriggered() {
|
||||
|
@@ -1,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;
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package com.hxuanyu.monitor.manager;
|
||||
|
||||
import com.hxuanyu.common.message.Msg;
|
||||
import com.hxuanyu.monitor.annotation.MonitorItem;
|
||||
import com.hxuanyu.monitor.base.BaseMonitorItem;
|
||||
import com.hxuanyu.monitor.common.CheckResult;
|
||||
@@ -58,31 +59,47 @@ public class MonitorItemBeanManager implements ApplicationListener<ContextRefres
|
||||
item.setMonitorItemName(name);
|
||||
item.setCron(cron);
|
||||
logger.info("获取到的Bean:{}", item);
|
||||
addMonitorTask(item);
|
||||
Msg<String> msg = addMonitorTask(item);
|
||||
logger.info("添加成功:{}", msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addMonitorTask(BaseMonitorItem item) {
|
||||
public Msg<String> addMonitorTask(BaseMonitorItem item) {
|
||||
String taskId = "ScheduledTask-" + item.getMonitorItemName();
|
||||
if (MONITOR_ITEM_MAP.containsKey(taskId)) {
|
||||
return Msg.failed("任务已经存在,请执行修改操作");
|
||||
}
|
||||
MONITOR_ITEM_MAP.put(taskId, item);
|
||||
logger.info("添加定时任务:{}, 执行周期:{}", taskId, item.getCron());
|
||||
addTask(taskId, item);
|
||||
return Msg.success("添加成功");
|
||||
}
|
||||
|
||||
public void setMonitorTaskCron(String taskId, String cron) {
|
||||
public Msg<String> setMonitorTaskCron(String taskId, String cron) {
|
||||
if (MONITOR_ITEM_MAP.containsKey(taskId)) {
|
||||
schedulingConfigurer.cancelTriggerTask(taskId);
|
||||
BaseMonitorItem item = MONITOR_ITEM_MAP.get(taskId);
|
||||
item.setCron(cron);
|
||||
addTask(taskId, item);
|
||||
logger.info("修改定时任务:{}, 执行周期:{}", taskId, item.getCron());
|
||||
return Msg.success("修改成功");
|
||||
} else {
|
||||
return Msg.failed("修改失败,该任务不存在");
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteMonitorTask(String taskId) {
|
||||
MONITOR_ITEM_MAP.remove(taskId);
|
||||
schedulingConfigurer.cancelTriggerTask(taskId);
|
||||
public Msg<String> deleteMonitorTask(String taskId) {
|
||||
if (MONITOR_ITEM_MAP.containsKey(taskId)) {
|
||||
MONITOR_ITEM_MAP.remove(taskId);
|
||||
schedulingConfigurer.cancelTriggerTask(taskId);
|
||||
logger.info("删除定时任务:{}", taskId);
|
||||
return Msg.success("删除任务成功");
|
||||
} else {
|
||||
return Msg.failed("任务不存在");
|
||||
}
|
||||
}
|
||||
|
||||
private void addTask(String taskId, BaseMonitorItem item) {
|
||||
@@ -90,8 +107,13 @@ public class MonitorItemBeanManager implements ApplicationListener<ContextRefres
|
||||
schedulingConfigurer.resetTriggerTask(taskId, new TriggerTask(() -> {
|
||||
CheckResult checkResult = item.check();
|
||||
if (checkResult.isTriggered()) {
|
||||
logger.info("定时任务[{}]触发成功,发送通知:[{}]", taskId, checkResult.getNotifyContent());
|
||||
notifyService.notify(checkResult.getNotifyContent(), NotifyType.TYPE_MAIL);
|
||||
if (NotifyType.TYPE_CUSTOM.equals(checkResult.getNotifyType())){
|
||||
logger.info("定时任务[{}]触发成功,执行自定义通知", taskId);
|
||||
notifyService.notify(checkResult.getCustomNotify());
|
||||
} else {
|
||||
logger.info("定时任务[{}]触发成功,发送通知:[{}]", taskId, checkResult.getNotifyContent());
|
||||
notifyService.notify(checkResult.getNotifyContent(), checkResult.getNotifyType());
|
||||
}
|
||||
}
|
||||
}, new CronTrigger(cron)));
|
||||
}
|
||||
|
44
network-spring-boot-starter/README.md
Normal file
44
network-spring-boot-starter/README.md
Normal file
@@ -0,0 +1,44 @@
|
||||
## network-spring-boot-starter
|
||||
|
||||
### 简介
|
||||
|
||||
本模块对`HttpClient`进行了封装,实现了GET、POST、PUT、DELETE等Http请求的同步和异步方法
|
||||
|
||||
|
||||
|
||||
### 引入
|
||||
|
||||
```java
|
||||
|
||||
<dependency>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<artifactId>network-spring-boot-starter</artifactId>
|
||||
<version>1</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
|
||||
|
||||
### 使用
|
||||
|
||||
- 引入`HttpService`对象
|
||||
|
||||
```java
|
||||
@Resource
|
||||
HttpService httpService;
|
||||
```
|
||||
|
||||
|
||||
|
||||
- 调用相关方法
|
||||
|
||||
```java
|
||||
Msg<String> msg = httpService.doGet("https://baidu.com");
|
||||
if (msg.isSuccess()) {
|
||||
logger.info(msg.toString());
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
- 本模块依赖了common-spring-boot-starter模块,引入本模块后会自动引入common模块下的相关类,同时`HttpService`的方法返回值使用了common模块下的`Msg`统一封装
|
@@ -3,7 +3,7 @@
|
||||
<parent>
|
||||
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<version>1.0.4-SNAPSHOT</version>
|
||||
<version>1.0.5</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@@ -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,7 +270,7 @@ 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请求;不带请求参数
|
||||
@@ -286,7 +286,7 @@ public interface HttpService {
|
||||
* @param url 请求地址
|
||||
* @return 统一返回报文
|
||||
*/
|
||||
Msg<HttpEntity> doDeleteWithEntity(String url);
|
||||
Msg<HttpResponse> doDeleteWithResponse(String url);
|
||||
|
||||
/**
|
||||
* 发送delete请求;带请求参数
|
||||
@@ -305,7 +305,7 @@ public interface HttpService {
|
||||
* @param params 请求参数
|
||||
* @return 统一返回报文
|
||||
*/
|
||||
Msg<HttpEntity> doDeleteWithEntity(String url, Map<String, String> params);
|
||||
Msg<HttpResponse> doDeleteWithResponse(String url, Map<String, String> params);
|
||||
|
||||
interface NetWorkListener<T> {
|
||||
/**
|
||||
|
@@ -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 + "请求失败");
|
||||
}
|
||||
|
||||
|
103
notify-spring-boot-starter/README.md
Normal file
103
notify-spring-boot-starter/README.md
Normal file
@@ -0,0 +1,103 @@
|
||||
## notify-spring-boot-starter
|
||||
|
||||
### 简介
|
||||
|
||||
本模块用于对用户进行通知,目前支持邮件通知,后续会加入短信等更多类型
|
||||
|
||||
- MAIL:对springboot的mail模块进行了封装,实现了一个邮件发送队列,并支持html作为邮件内容
|
||||
|
||||
|
||||
|
||||
### 引入
|
||||
|
||||
```java
|
||||
<dependency>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<artifactId>notify-spring-boot-starter</artifactId>
|
||||
<version>1.0.4</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
|
||||
|
||||
### 使用
|
||||
|
||||
#### Mail
|
||||
|
||||
- 引入`NotifyService`对象
|
||||
|
||||
```java
|
||||
@Resource
|
||||
NotifyService notifyService;
|
||||
```
|
||||
|
||||
|
||||
|
||||
- 添加Mail配置
|
||||
|
||||
```java
|
||||
notify:
|
||||
mail:
|
||||
host: your mail host
|
||||
protocol: smtp
|
||||
default-encoding: UTF-8
|
||||
password: your passwd
|
||||
username: your account
|
||||
port: 587
|
||||
properties:
|
||||
mail:
|
||||
debug: false
|
||||
stmp:
|
||||
socketFactory:
|
||||
class: javax.net.ssl.SSLSocketFactory
|
||||
```
|
||||
|
||||
|
||||
|
||||
- 调用相关方法
|
||||
|
||||
```java
|
||||
notifyService.notify(new Mail("2252193204@qq.com", "test subject", "test success"), NotifyType.TYPE_MAIL);
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### 短信和日志
|
||||
|
||||
- 引入`NotifyService`对象
|
||||
|
||||
```java
|
||||
@Resource
|
||||
NotifyService notifyService;
|
||||
```
|
||||
|
||||
- 调用相关方法
|
||||
|
||||
```java
|
||||
notifyService.notify("短信通知方式", NotifyType.TYPE_MSG);
|
||||
notifyService.notify("日志输出方式", NotifyType.TYPE_LOG);
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### 自定义
|
||||
|
||||
- 引入`NotifyService`对象
|
||||
|
||||
```java
|
||||
@Resource
|
||||
NotifyService notifyService;
|
||||
```
|
||||
|
||||
- 调用方法
|
||||
|
||||
```java
|
||||
notifyService.notify(new NotifyService.CustomNotify() {
|
||||
@Override
|
||||
public void onNotify() {
|
||||
// do some things
|
||||
|
||||
}
|
||||
});
|
||||
```
|
||||
|
@@ -3,7 +3,7 @@
|
||||
<parent>
|
||||
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<version>1.0.4-SNAPSHOT</version>
|
||||
<version>1.0.5</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@@ -10,10 +10,11 @@ public enum NotifyType {
|
||||
|
||||
|
||||
/**
|
||||
* 邮件类型
|
||||
* 通知类型
|
||||
*/
|
||||
TYPE_LOG("日志输出"),
|
||||
TYPE_MAIL("邮件"),
|
||||
TYPE_CUSTOM("自定义"),
|
||||
TYPE_MSG("短信");
|
||||
|
||||
private final String typeName;
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package com.hxuanyu.notify.service;
|
||||
|
||||
import com.hxuanyu.notify.enums.NotifyType;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 通知服务
|
||||
@@ -17,4 +16,18 @@ public interface NotifyService {
|
||||
* @param notifyType 通知类型
|
||||
*/
|
||||
void notify(Object content, NotifyType notifyType);
|
||||
|
||||
/**
|
||||
* 自定义通知
|
||||
*
|
||||
* @param customNotify 自定义通知接口,需手动实现
|
||||
*/
|
||||
void notify(CustomNotify customNotify);
|
||||
|
||||
interface CustomNotify {
|
||||
/**
|
||||
* 通知
|
||||
*/
|
||||
void onNotify();
|
||||
}
|
||||
}
|
||||
|
@@ -25,17 +25,26 @@ public class NotifyServiceImpl implements NotifyService {
|
||||
|
||||
@Override
|
||||
public void notify(Object content, NotifyType notifyType) {
|
||||
logger.debug("通知内容:{},通知类型:{}", notifyType, notifyType);
|
||||
if (NotifyType.TYPE_MAIL.equals(notifyType)) {
|
||||
sendMail(content);
|
||||
} 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);
|
||||
}
|
||||
|
4
pom.xml
4
pom.xml
@@ -4,7 +4,7 @@
|
||||
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
||||
<version>1.0.4-SNAPSHOT</version>
|
||||
<version>1.0.5</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.0</tag>
|
||||
<tag>v1.0.5</tag>
|
||||
</scm>
|
||||
<developers>
|
||||
<developer>
|
||||
|
11
test/pom.xml
11
test/pom.xml
@@ -1,11 +1,9 @@
|
||||
<?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">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<version>1.0.4-SNAPSHOT</version>
|
||||
<version>1.0.5</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@@ -16,11 +14,6 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<artifactId>notify-spring-boot-starter</artifactId>
|
||||
|
@@ -0,0 +1,61 @@
|
||||
package com.hxuanyu.test.controller;
|
||||
|
||||
import com.hxuanyu.common.message.Msg;
|
||||
import com.hxuanyu.monitor.base.BaseMonitorItem;
|
||||
import com.hxuanyu.monitor.common.CheckResult;
|
||||
import com.hxuanyu.monitor.manager.MonitorItemBeanManager;
|
||||
import com.hxuanyu.notify.enums.NotifyType;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 监控项测试控制器
|
||||
*
|
||||
* @author hanxuanyu
|
||||
* @version 1.0
|
||||
*/
|
||||
|
||||
@RequestMapping("/monitor")
|
||||
@RestController
|
||||
public class MonitorTestController {
|
||||
|
||||
@Resource
|
||||
MonitorItemBeanManager monitorItemBeanManager;
|
||||
|
||||
@PutMapping("/")
|
||||
public Msg<String> setCron(String taskId, String taskCron) {
|
||||
if (taskId == null || taskCron == null) {
|
||||
return Msg.failed("参数不匹配");
|
||||
}
|
||||
return monitorItemBeanManager.setMonitorTaskCron(taskId, taskCron);
|
||||
}
|
||||
|
||||
@DeleteMapping("/")
|
||||
public Msg<String> deleteTask(String taskId) {
|
||||
if (taskId == null) {
|
||||
return Msg.failed("taskId 未填写");
|
||||
}
|
||||
return monitorItemBeanManager.deleteMonitorTask(taskId);
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public Msg<Collection<BaseMonitorItem>> getTaskList() {
|
||||
Map<String, BaseMonitorItem> monitorItemMap = monitorItemBeanManager.getMonitorItemMap();
|
||||
return Msg.success("获取成功", monitorItemMap.values());
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public Msg<String> addTaskList() {
|
||||
return monitorItemBeanManager.addMonitorTask(new BaseMonitorItem("CustomBean", "0/10 * * * * *") {
|
||||
@Override
|
||||
public CheckResult check() {
|
||||
return CheckResult.triggered("动态新增通知", NotifyType.TYPE_LOG);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -1,12 +1,11 @@
|
||||
package com.hxuanyu.test.controller;
|
||||
|
||||
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 com.hxuanyu.notify.service.NotifyService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@@ -19,28 +18,9 @@ import javax.annotation.Resource;
|
||||
*/
|
||||
|
||||
@RestController
|
||||
public class TestController {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(TestController.class);
|
||||
@Resource
|
||||
MailService mailService;
|
||||
|
||||
@RequestMapping("/testMail")
|
||||
public Msg<Mail> testMail() {
|
||||
Mail mail = new Mail();
|
||||
mail.setContent("邮件内容");
|
||||
mail.setSubject("邮件主题");
|
||||
mail.setTo("2252193204@qq.com");
|
||||
|
||||
try {
|
||||
mailService.sendMail(mail);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return Msg.success("发送邮件成功", mail);
|
||||
}
|
||||
public class NetworkTestController {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(NetworkTestController.class);
|
||||
|
||||
@ResponseBody
|
||||
@GetMapping("/")
|
@@ -4,17 +4,43 @@ import com.hxuanyu.monitor.annotation.MonitorItem;
|
||||
import com.hxuanyu.monitor.base.BaseMonitorItem;
|
||||
import com.hxuanyu.monitor.common.CheckResult;
|
||||
import com.hxuanyu.notify.enums.NotifyType;
|
||||
import com.hxuanyu.notify.model.Mail;
|
||||
import com.hxuanyu.notify.service.NotifyService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 定时任务监控测试
|
||||
*
|
||||
* @author hxuanyu
|
||||
*/
|
||||
@MonitorItem(cron = "0/5 * * * * *")
|
||||
@MonitorItem(cron = "0/20 * * * * *")
|
||||
public class TestMonitorItem extends BaseMonitorItem {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(TestMonitorItem.class);
|
||||
|
||||
@Override
|
||||
public CheckResult check() {
|
||||
return CheckResult.triggered("Hello", NotifyType.TYPE_LOG);
|
||||
|
||||
double random = Math.random();
|
||||
int result = (int) (random * NotifyType.values().length);
|
||||
logger.info("随机索引值:{}", result);
|
||||
if (NotifyType.values().length > 0) {
|
||||
NotifyType notifyType = NotifyType.values()[result];
|
||||
switch (notifyType) {
|
||||
case TYPE_LOG:
|
||||
return CheckResult.triggered("日志输出", NotifyType.TYPE_LOG);
|
||||
case TYPE_CUSTOM:
|
||||
return CheckResult.triggered(() -> logger.info("自定义通知"));
|
||||
case TYPE_MAIL:
|
||||
return CheckResult.triggered(new Mail("2252193204@qq.com", "测试邮件主题", "测试邮件内容"), NotifyType.TYPE_MAIL);
|
||||
case TYPE_MSG:
|
||||
return CheckResult.triggered("短信通知", NotifyType.TYPE_MSG);
|
||||
default:
|
||||
return CheckResult.triggered("默认通知", null);
|
||||
}
|
||||
}
|
||||
|
||||
return CheckResult.nonTriggered();
|
||||
}
|
||||
}
|
||||
|
51
test/src/test/java/com/hxuanyu/starter/test/OtherTest.java
Normal file
51
test/src/test/java/com/hxuanyu/starter/test/OtherTest.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -5,14 +5,11 @@ import com.hxuanyu.network.service.HttpService;
|
||||
import com.hxuanyu.test.MainApplication;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
import static java.lang.Thread.sleep;
|
||||
|
||||
@SpringBootTest(classes = MainApplication.class)
|
||||
public class NetworkTest {
|
||||
@@ -24,7 +21,7 @@ public class NetworkTest {
|
||||
|
||||
@Test
|
||||
@Timeout(3000)
|
||||
public void testGet() throws InterruptedException {
|
||||
public void testGet(){
|
||||
Msg<String> msg = httpService.doGet("https://baidu.com");
|
||||
logger.info("GET测试:{}", msg);
|
||||
assert msg.isSuccess();
|
||||
|
@@ -22,6 +22,10 @@ public class NotifyTest {
|
||||
sleep(2000);
|
||||
notifyService.notify("短信通知方式", NotifyType.TYPE_MSG);
|
||||
notifyService.notify("日志输出方式", NotifyType.TYPE_LOG);
|
||||
notifyService.notify(() -> {
|
||||
// do some things
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user