Merge branch 'master' of http://git.hchbox.com/yuyou/client
commit
eb04337a6d
@ -0,0 +1,32 @@
|
||||
package com.yuyou.openapi.openapi.exception;
|
||||
|
||||
import com.yuyou.openapi.openapi.common.CommonResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
/**
|
||||
* 全局异常处理类
|
||||
*/
|
||||
@ControllerAdvice
|
||||
@Slf4j
|
||||
public class GlobalExceptionHandle {
|
||||
|
||||
|
||||
/**
|
||||
* 捕获异常级别是Exception级别的异常
|
||||
*
|
||||
* @param exception 异常入参
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(Exception.class)
|
||||
@ResponseBody
|
||||
public CommonResponse customException(Exception exception) {
|
||||
if (exception != null){
|
||||
log.error(" =============== [ request error, exception is {} , {}] ===============", exception.getMessage() ,exception);
|
||||
return CommonResponse.createByErrorMessage(exception.getMessage());
|
||||
}
|
||||
return CommonResponse.createByErrorMessage("未知异常错误发生");
|
||||
}
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
package com.yuyou.openapi.openapi.task;
|
||||
|
||||
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.yuyou.openapi.openapi.model.dto.ABMessageDTO;
|
||||
import com.yuyou.openapi.openapi.model.dto.convert.ABMessageCovDTO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 下游处理任务类
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class ABDownTask {
|
||||
|
||||
/**
|
||||
* 配置文件中加载配置信息
|
||||
*/
|
||||
@Value("${ab.customer.app_id}")
|
||||
private String appId;
|
||||
|
||||
@Value("${ab.customer.url}")
|
||||
private String customerUrl;
|
||||
|
||||
|
||||
/**
|
||||
* 任务处理入口,主要用于时间记录
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Async(value = "abTaskExecutor")
|
||||
public boolean doRunTask(List<ABMessageDTO> messageDTOList){
|
||||
Long satrtMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
|
||||
log.info("====== [ task start running, task name is {} ] ======", "ABDownTask");
|
||||
boolean resultTag = runTask(messageDTOList);
|
||||
Long endMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
|
||||
log.info("====== [ task start end, task name is {},cost milliSecond is {} ] ======", "ABDownTask", (endMilliSecond-satrtMilliSecond));
|
||||
return resultTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 真实的任务执行入口
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private boolean runTask(List<ABMessageDTO> messageDTOList){
|
||||
int count = 0; // 设置请求失败计数
|
||||
// 数据实体进行映射转换 - 目前是只有一条给
|
||||
ABMessageCovDTO abMessageCovDTO = new ABMessageCovDTO();
|
||||
abMessageCovDTO.setData(messageDTOList);
|
||||
// 补充其他的下游请求字段
|
||||
abMessageCovDTO.setNonce_str((System.currentTimeMillis()/1000));
|
||||
abMessageCovDTO.setApp_id(appId);
|
||||
abMessageCovDTO.setSignature(SecureUtil.sha1("app_id="+abMessageCovDTO.getApp_id()+"&nonce_str="+abMessageCovDTO.getNonce_str()));
|
||||
// 数据实体转成Json 不忽略空kv 有序
|
||||
JSONObject jsonObject = JSONUtil.parseObj(abMessageCovDTO, false, true);
|
||||
// 请求的响应处理
|
||||
// todo 失败重发请求3次
|
||||
while (count <= 3){
|
||||
// 调用HTTP请求发送数据
|
||||
HttpResponse httpResponse = sendReq(jsonObject);
|
||||
if (httpResponse.isOk() && httpResponse.body().contains("成功")){
|
||||
break;
|
||||
}else{
|
||||
count ++;
|
||||
log.info("========== [request fail, response is {} ] ==========", httpResponse.body());
|
||||
}
|
||||
}
|
||||
if (count > 4) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用HTTP请求发送数据
|
||||
*
|
||||
* @param jsonObject
|
||||
* @return
|
||||
*/
|
||||
private HttpResponse sendReq(JSONObject jsonObject){
|
||||
HttpResponse httpResponse = HttpRequest
|
||||
.post(customerUrl)
|
||||
.body(jsonObject.toString())
|
||||
.execute();
|
||||
return httpResponse;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
{
|
||||
"properties": [
|
||||
{
|
||||
"name": "ab.customer.app_id",
|
||||
"type": "java.lang.String",
|
||||
"description": "Description for ab.customer.app_id."
|
||||
}
|
||||
] }
|
@ -1,10 +1,49 @@
|
||||
server:
|
||||
port: 8090
|
||||
|
||||
|
||||
|
||||
|
||||
spring:
|
||||
profiles:
|
||||
active: dev
|
||||
# 序列化忽略null的k-v配置
|
||||
jackson:
|
||||
default-property-inclusion: non_null
|
||||
|
||||
---
|
||||
# 端口
|
||||
server:
|
||||
port: 8090
|
||||
# 环境
|
||||
spring:
|
||||
profiles: dev
|
||||
# 数据库相关配置
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3306/push?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
|
||||
username: root
|
||||
password: root
|
||||
# 下游请求配置信息
|
||||
ab:
|
||||
customer:
|
||||
url: https://sandbox.openapi.ppke.com.cn/
|
||||
app_id: app_id
|
||||
thread_pool:
|
||||
corePoolSize:
|
||||
maxPoolSize:
|
||||
queueCapacity:
|
||||
ThreadNamePrefix:
|
||||
|
||||
---
|
||||
# 端口
|
||||
server:
|
||||
port: 8090
|
||||
# 环境
|
||||
spring:
|
||||
profiles: prod
|
||||
# 数据库相关配置
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3306/push?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
|
||||
username: root
|
||||
password: Yuyou@2020
|
||||
# 下游请求配置信息
|
||||
ab:
|
||||
customer:
|
||||
url: https://openapi.ppke.com.cn/
|
||||
app_id: app_id
|
Loading…
Reference in New Issue