9 Commits

20 changed files with 349 additions and 192 deletions

View File

@@ -3,7 +3,7 @@
<parent> <parent>
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId> <artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
<groupId>com.hxuanyu</groupId> <groupId>com.hxuanyu</groupId>
<version>1.0.4</version> <version>1.0.6</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <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.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@@ -12,6 +12,8 @@ import java.util.*;
* @version 1.0 * @version 1.0
*/ */
public class BeanUtils { public class BeanUtils {
public static Field findField(Class<?> clazz, String name) { public static Field findField(Class<?> clazz, String name) {
try { try {
return clazz.getField(name); return clazz.getField(name);

View File

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

View File

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

View File

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

View File

@@ -1,6 +1,6 @@
package com.hxuanyu.monitor.config; 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.context.annotation.Configuration;
import org.springframework.scheduling.SchedulingException; import org.springframework.scheduling.SchedulingException;
import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.TaskScheduler;

View File

@@ -1,6 +1,7 @@
package com.hxuanyu.monitor.manager; package com.hxuanyu.monitor.manager;
import com.hxuanyu.common.message.Msg; import com.hxuanyu.common.message.Msg;
import com.hxuanyu.common.spring.SpringContextUtil;
import com.hxuanyu.monitor.annotation.MonitorItem; import com.hxuanyu.monitor.annotation.MonitorItem;
import com.hxuanyu.monitor.base.BaseMonitorItem; import com.hxuanyu.monitor.base.BaseMonitorItem;
import com.hxuanyu.monitor.common.CheckResult; import com.hxuanyu.monitor.common.CheckResult;
@@ -68,7 +69,7 @@ public class MonitorItemBeanManager implements ApplicationListener<ContextRefres
} }
public Msg<String> addMonitorTask(BaseMonitorItem item) { public Msg<String> addMonitorTask(BaseMonitorItem item) {
String taskId = "ScheduledTask-" + item.getMonitorItemName(); String taskId = item.getMonitorItemName();
if (MONITOR_ITEM_MAP.containsKey(taskId)) { if (MONITOR_ITEM_MAP.containsKey(taskId)) {
return Msg.failed("任务已经存在,请执行修改操作"); return Msg.failed("任务已经存在,请执行修改操作");
} }
@@ -78,13 +79,22 @@ public class MonitorItemBeanManager implements ApplicationListener<ContextRefres
return Msg.success("添加成功"); return Msg.success("添加成功");
} }
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) { public Msg<String> setMonitorTaskCron(String taskId, String cron) {
if (MONITOR_ITEM_MAP.containsKey(taskId)) { if (MONITOR_ITEM_MAP.containsKey(taskId)) {
schedulingConfigurer.cancelTriggerTask(taskId); schedulingConfigurer.cancelTriggerTask(taskId);
BaseMonitorItem item = MONITOR_ITEM_MAP.get(taskId); BaseMonitorItem item = MONITOR_ITEM_MAP.get(taskId);
item.setCron(cron); item.setCron(cron);
addTask(taskId, item); addTask(taskId, item);
logger.info("修改定时任务:{}, 执行周期:{}", taskId, item.getCron()); logger.info("修改定时任务:{}, 执行周期:{}", taskId,item.getCron());
return Msg.success("修改成功"); return Msg.success("修改成功");
} else { } else {
return Msg.failed("修改失败,该任务不存在"); return Msg.failed("修改失败,该任务不存在");
@@ -107,7 +117,7 @@ public class MonitorItemBeanManager implements ApplicationListener<ContextRefres
schedulingConfigurer.resetTriggerTask(taskId, new TriggerTask(() -> { schedulingConfigurer.resetTriggerTask(taskId, new TriggerTask(() -> {
CheckResult checkResult = item.check(); CheckResult checkResult = item.check();
if (checkResult.isTriggered()) { if (checkResult.isTriggered()) {
if (NotifyType.TYPE_CUSTOM.equals(checkResult.getNotifyType())){ if (NotifyType.TYPE_CUSTOM.equals(checkResult.getNotifyType())) {
logger.info("定时任务[{}]触发成功,执行自定义通知", taskId); logger.info("定时任务[{}]触发成功,执行自定义通知", taskId);
notifyService.notify(checkResult.getCustomNotify()); notifyService.notify(checkResult.getCustomNotify());
} else { } else {

View File

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

View File

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

View File

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

View File

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

View File

@@ -4,7 +4,7 @@
<groupId>com.hxuanyu</groupId> <groupId>com.hxuanyu</groupId>
<artifactId>hxuanyu-spring-boot-starter-parent</artifactId> <artifactId>hxuanyu-spring-boot-starter-parent</artifactId>
<version>1.0.4</version> <version>1.0.6</version>
<name>hxuanyu-spring-boot-starter-parent</name> <name>hxuanyu-spring-boot-starter-parent</name>
<description>MonitorPushingParent</description> <description>MonitorPushingParent</description>
<packaging>pom</packaging> <packaging>pom</packaging>
@@ -19,7 +19,7 @@
<connection>scm:git:https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter.git</connection> <connection>scm:git:https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter.git</connection>
<developerConnection>scm:git:https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter.git</developerConnection> <developerConnection>scm:git:https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter.git</developerConnection>
<url>https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter</url> <url>https://git.hxuanyu.com/hxuanyu/hxuanyu-spring-boot-starter</url>
<tag>v1.0.4</tag> <tag>v1.0.6</tag>
</scm> </scm>
<developers> <developers>
<developer> <developer>

View File

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

View File

@@ -2,9 +2,10 @@ package com.hxuanyu.test.controller;
import com.hxuanyu.common.message.Msg; import com.hxuanyu.common.message.Msg;
import com.hxuanyu.monitor.base.BaseMonitorItem; import com.hxuanyu.monitor.base.BaseMonitorItem;
import com.hxuanyu.monitor.common.CheckResult;
import com.hxuanyu.monitor.manager.MonitorItemBeanManager; import com.hxuanyu.monitor.manager.MonitorItemBeanManager;
import com.hxuanyu.notify.enums.NotifyType; import com.hxuanyu.test.monitor.TestMonitorItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
@@ -21,6 +22,7 @@ import java.util.Map;
@RequestMapping("/monitor") @RequestMapping("/monitor")
@RestController @RestController
public class MonitorTestController { public class MonitorTestController {
private final Logger logger = LoggerFactory.getLogger(MonitorTestController.class);
@Resource @Resource
MonitorItemBeanManager monitorItemBeanManager; MonitorItemBeanManager monitorItemBeanManager;
@@ -49,12 +51,7 @@ public class MonitorTestController {
@PostMapping("/") @PostMapping("/")
public Msg<String> addTaskList() { public Msg<String> addTaskList() {
return monitorItemBeanManager.addMonitorTask(new BaseMonitorItem("CustomBean", "0/10 * * * * *") { return monitorItemBeanManager.addMonitorTask(TestMonitorItem.class, "0/5 * * * * *");
@Override
public CheckResult check() {
return CheckResult.triggered("动态新增通知", NotifyType.TYPE_LOG);
}
});
} }

View File

@@ -14,14 +14,21 @@ import org.slf4j.LoggerFactory;
* *
* @author hxuanyu * @author hxuanyu
*/ */
@MonitorItem(cron = "0/20 * * * * *")
public class TestMonitorItem extends BaseMonitorItem { public class TestMonitorItem extends BaseMonitorItem {
private final Logger logger = LoggerFactory.getLogger(TestMonitorItem.class); private final Logger logger = LoggerFactory.getLogger(TestMonitorItem.class);
public TestMonitorItem(String monitorItemName, String cron) {
super(monitorItemName, cron);
}
public TestMonitorItem(String cron) {
super(cron);
}
@Override @Override
public CheckResult check() { public CheckResult check() {
double random = Math.random(); double random = Math.random();
int result = (int) (random * NotifyType.values().length); int result = (int) (random * NotifyType.values().length);
logger.info("随机索引值:{}", result); logger.info("随机索引值:{}", result);

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

@@ -19,7 +19,9 @@ public class MonitorTest {
@Test @Test
public void testMonitorItemScan() { public void testMonitorItemScan() {
BaseMonitorItem testMonitorItem = monitorItemBeanManager.getMonitorItemMap().get("ScheduledTask-TestMonitorItem"); BaseMonitorItem testMonitorItem = monitorItemBeanManager.getMonitorItemMap().get("ScheduledTask-TestMonitorItem");
assert testMonitorItem.getMonitorItemName() != null; if (testMonitorItem != null){
logger.info("获取到的Bean{}", testMonitorItem.toString()); assert testMonitorItem.getMonitorItemName() != null;
logger.info("获取到的Bean{}", testMonitorItem.toString());
}
} }
} }

View File

@@ -5,14 +5,11 @@ import com.hxuanyu.network.service.HttpService;
import com.hxuanyu.test.MainApplication; import com.hxuanyu.test.MainApplication;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout; import org.junit.jupiter.api.Timeout;
import org.junit.runner.RunWith;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.web.WebAppConfiguration;
import static java.lang.Thread.sleep;
@SpringBootTest(classes = MainApplication.class) @SpringBootTest(classes = MainApplication.class)
public class NetworkTest { public class NetworkTest {
@@ -24,7 +21,7 @@ public class NetworkTest {
@Test @Test
@Timeout(3000) @Timeout(3000)
public void testGet() throws InterruptedException { public void testGet(){
Msg<String> msg = httpService.doGet("https://baidu.com"); Msg<String> msg = httpService.doGet("https://baidu.com");
logger.info("GET测试{}", msg); logger.info("GET测试{}", msg);
assert msg.isSuccess(); assert msg.isSuccess();