Commit 84c1824b authored by yanzg's avatar yanzg

修改公式和计算帮助类

parent 32c49f58
......@@ -30,6 +30,12 @@ public class ThreadNext {
int getNextTime();
}
/**
* 开始执行
*
* @param next
* @param error
*/
public static void start(final Next next, String error) {
if (next == null) {
return;
......@@ -42,6 +48,11 @@ public class ThreadNext {
}).start();
}
/**
* 下一次执行
*
* @param next
*/
private static void runNext(Next next) {
boolean isExecute = true;
Exception frontEx = null;
......
......@@ -23,14 +23,14 @@ public class LogVo extends BaseVo {
/**
* 工程实例名
*/
@ApiModelProperty(value = "项目名称", required = true, example = "tbd-order")
private String logSources;
@ApiModelProperty(value = "项目名称", required = true, example = "MQ|WEB|FEIGN")
private String actionKey;
/**
* 请求接口地址
*/
@ApiModelProperty(value = "接口地址", required = true, example = "/order/save")
private String interUrl;
@ApiModelProperty(value = "接口地址", required = true, example = "tbd-order/order/save")
private String actionSubKey;
/**
* 请求内容
......@@ -47,8 +47,8 @@ public class LogVo extends BaseVo {
/**
* 接口处理状态,是否有异常
*/
@ApiModelProperty(value = "执行状态", notes = "0表示成功,1表示失败", required = true, example = "0")
private int status;
@ApiModelProperty(value = "执行状态", notes = "0表示成功,其他状态表示失败", required = true, example = "0")
private String status;
/**
* 使用时间
......@@ -70,20 +70,20 @@ public class LogVo extends BaseVo {
this.logId = logId;
}
public String getLogSources() {
return logSources;
public String getActionKey() {
return actionKey;
}
public void setLogSources(String logSources) {
this.logSources = logSources;
public void setActionKey(String actionKey) {
this.actionKey = actionKey;
}
public String getInterUrl() {
return interUrl;
public String getActionSubKey() {
return actionSubKey;
}
public void setInterUrl(String interUrl) {
this.interUrl = interUrl;
public void setActionSubKey(String actionSubKey) {
this.actionSubKey = actionSubKey;
}
public String getContent() {
......@@ -102,11 +102,11 @@ public class LogVo extends BaseVo {
this.result = result;
}
public int getStatus() {
public String getStatus() {
return status;
}
public void setStatus(int status) {
public void setStatus(String status) {
this.status = status;
}
......
......@@ -211,6 +211,8 @@ public class ResponseResult<T> extends BaseVo {
}
}
public static final ResponseResult OK = new ResponseResult();
/**
* 处理错误
*
......
package com.yanzuoguang.cloud.aop;
import com.alibaba.fastjson.JSON;
import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.contants.ResultConstants;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.thread.ThreadNext;
import com.yanzuoguang.util.vo.LogVo;
import com.yanzuoguang.util.vo.ResponseResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServlet;
......@@ -21,14 +18,13 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
/**
* 基本处理拦截类
*
* @author 颜佐光
*/
public class BaseRequestAspect implements ThreadNext.Next {
public class BaseRequestAspect {
@Value("${spring.application.name}")
......@@ -53,26 +49,7 @@ public class BaseRequestAspect implements ThreadNext.Next {
private String filter;
@Autowired
protected ApplicationContext context;
protected LogFeign logFeign;
/**
* 缓存队列
*/
protected volatile LinkedBlockingQueue<LogVo> cache = new LinkedBlockingQueue<LogVo>();
public BaseRequestAspect() {
ThreadNext.start(this, "save log error");
}
/**
* 添加日志到缓存中,并不是立即添加,而是通过线程执行,防止对环境造成影响
*
* @param logVo
*/
public void addLog(LogVo logVo) {
cache.add(logVo);
}
private LogBase logBase;
/**
* 初始化日志Vo
......@@ -86,15 +63,15 @@ public class BaseRequestAspect implements ThreadNext.Next {
LogVo logInterVo = new LogVo();
logInterVo.setLogId(StringHelper.getNewID());
//平台名
logInterVo.setLogSources(applicationName);
logInterVo.setActionKey(applicationName);
//请求URL
logInterVo.setInterUrl(url);
logInterVo.setActionSubKey(url);
Object paraTo = getFirstDataParameter(joinPoint.getArgs());
//请求参数
logInterVo.setContent(JSON.toJSONString(paraTo));
//返回参数
logInterVo.setResult(JSON.toJSONString(responseResult));
logInterVo.setStatus(responseResult != null && responseResult.getCode() == ResultConstants.SUCCESS ? 1 : 0);
logInterVo.setStatus(responseResult != null ? responseResult.getCode() : ResultConstants.SUCCESS);
return logInterVo;
}
......@@ -224,7 +201,7 @@ public class BaseRequestAspect implements ThreadNext.Next {
if (logFlag) {
LogVo logVo = initLogInterVo(url, joinPoint, responseResult);
logVo.setUseTime((int) time);
addLog(logVo);
logBase.addLog(logVo);
}
} catch (Exception e) {
e.printStackTrace();
......@@ -251,37 +228,4 @@ public class BaseRequestAspect implements ThreadNext.Next {
boolean isLog = applicationName.matches(filter) || name.matches(filter) || url.matches(filter);
return isLog;
}
/**
* 执行下一个函数,出现异常会继续执行
*
* @return 是否继续执行
*/
@Override
public boolean next() {
while (cache.size() > 0) {
LogVo item = cache.poll();
if (item != null) {
try {
if (logFeign == null) {
logFeign = context.getBean(LogFeign.class);
}
logFeign.save(item);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
return true;
}
/**
* 沉睡时间
*
* @return
*/
@Override
public int getNextTime() {
return 1000;
}
}
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.util.thread.ThreadNext;
import com.yanzuoguang.util.vo.LogVo;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.concurrent.LinkedBlockingQueue;
/**
* 获取当前日志对象
*
* @author 颜佐光
*/
@Component
public class LogBase implements ThreadNext.Next, InitializingBean {
/**
* 缓存队列
*/
protected volatile LinkedBlockingQueue<LogVo> cache = new LinkedBlockingQueue<>();
@Autowired
protected LogFeign logFeign;
/**
* Invoked by a BeanFactory after it has set all bean properties supplied
* (and satisfied BeanFactoryAware and ApplicationContextAware).
* <p>This method allows the bean instance to perform initialization only
* possible when all bean properties have been set and to throw an
* exception in the event of misconfiguration.
*
* @throws Exception in the event of misconfiguration (such
* as failure to set an essential property) or if initialization fails.
*/
@Override
public void afterPropertiesSet() throws Exception {
ThreadNext.start(this, "save log error");
}
/**
* 添加日志到缓存中,并不是立即添加,而是通过线程执行,防止对环境造成影响
*
* @param logVo
*/
public void addLog(LogVo logVo) {
cache.add(logVo);
}
/**
* 执行下一个函数,出现异常会继续执行
*
* @return 是否继续执行
* @throws Exception 异常信息
*/
@Override
public boolean next() throws Exception {
while (cache.size() > 0) {
LogVo item = cache.poll();
if (item != null) {
try {
logFeign.save(item);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
return true;
}
/**
* 沉睡时间
*
* @return
*/
@Override
public int getNextTime() {
return 100;
}
}
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.thread.ThreadNext;
import com.yanzuoguang.util.vo.LogVo;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.*;
/**
* 当前线程日志编写
*
* @author 颜佐光
*/
@Component
public class LogLocal implements ThreadNext.Next, InitializingBean {
/**
* 缓存队列
*/
protected volatile Map<String, Timeout<LogVo>> cache = new Hashtable<>();
/**
* 1个请求最长时间
*/
@Value("${yzg.timeout:300000}")
private int timeout;
/**
* 超时状态
*/
public static final String MAX_TIME = "MAX_TIME";
public static final String MAX_TIME_NAME = "执行超时";
/**
* 日志基础
*/
@Autowired
private LogBase logBase;
/**
* Invoked by a BeanFactory after it has set all bean properties supplied
* (and satisfied BeanFactoryAware and ApplicationContextAware).
* <p>This method allows the bean instance to perform initialization only
* possible when all bean properties have been set and to throw an
* exception in the event of misconfiguration.
*
* @throws Exception in the event of misconfiguration (such
* as failure to set an essential property) or if initialization fails.
*/
@Override
public void afterPropertiesSet() throws Exception {
ThreadNext.start(this, "save log error");
}
/**
* 开始记录日志
*
* @param actionKey 功能名称
* @param actionSubKey 子功能名称 = 服务名称 + 地址
* @param content 请求内容
* @return
*/
public LogVo startLog(String actionKey, String actionSubKey, String content) {
// 生命日志对象爱嗯
LogVo log = new LogVo();
log.setLogId(StringHelper.getNewID());
log.setActionKey(actionKey);
log.setActionSubKey(actionSubKey);
log.setContent(content);
// 声明超时对象
Timeout<LogVo> time = new Timeout<>(log);
log.setCreateDate(DateHelper.getDateTimeString(new Date(time.getStart())));
cache.put(log.getLogId(), time);
return log;
}
/**
* 写入状态
*
* @param status
* @param result
* @return
*/
public void result(LogVo log, String status, String result) {
Timeout<LogVo> timeout = cache.get(log.getLogId());
result(timeout, log, status, result);
}
/**
* 写入状态
*
* @param status
* @param result
* @return
*/
public void result(Timeout timeout, LogVo log, String status, String result) {
if (timeout != null) {
long useTime = System.currentTimeMillis() - timeout.getStart();
log.setUseTime((int) useTime);
}
log.setStatus(status);
log.setResult(result);
if (!StringHelper.compare(status, MAX_TIME)) {
cache.remove(log.getLogId());
}
logBase.addLog(log);
}
/**
* 记录超时
*
* @return
*/
private void writeTimeout(Timeout<LogVo> timeout) {
result(timeout, timeout.getData(), MAX_TIME, MAX_TIME_NAME);
}
/**
* 执行下一个函数,出现异常会继续执行
*
* @return 是否继续执行
* @throws Exception 异常信息
*/
@Override
public boolean next() throws Exception {
List<String> keys = new ArrayList<>();
keys.addAll(cache.keySet());
for (String key : keys) {
Timeout<LogVo> timeout = cache.get(key);
if (timeout == null || !timeout.isMaxTime(this.timeout)) {
continue;
}
writeTimeout(timeout);
}
return true;
}
/**
* 沉睡时间
*
* @return
*/
@Override
public int getNextTime() {
return 100;
}
}
package com.yanzuoguang.cloud.aop;
/**
* 超时对象
*
* @param <T>
* @author 颜佐光
*/
public class Timeout<T extends Object> {
/**
* 开始时间
*/
private long start;
/**
* 结束时间
*/
private T data;
/**
* 构造函数
*
* @param data 数据
*/
public Timeout(T data) {
this.start = System.currentTimeMillis();
this.data = data;
}
/**
* 开始时间
*
* @return
*/
public long getStart() {
return start;
}
/**
* 数据
*
* @return
*/
public T getData() {
return data;
}
/**
* 是否达到最大时间
*
* @param maxTime 最大时间
* @return
*/
public boolean isMaxTime(long maxTime) {
long time = System.currentTimeMillis() - start;
return maxTime < time;
}
}
......@@ -91,7 +91,7 @@ public class YzgMqConsumer implements InitializingBean {
* @param channel
*/
@RabbitListener(queues = {YZG_MQ_SYSTEM_QUEUE}, concurrency = "10")
public void commentDataCreate(String json, Message message, Channel channel) {
public void yzgMqSystemQueue(String json, Message message, Channel channel) {
try {
MessageVo req = JsonHelper.deserialize(json, MessageVo.class);
mqService.message(req, true);
......@@ -162,7 +162,7 @@ public class YzgMqConsumer implements InitializingBean {
* @param channel
*/
@RabbitListener(queues = {YZG_MQ_CLEAR_TOKEN_QUEUE})
public void sendRemoveConsumer(String json, Message message, Channel channel) {
public void yzgMqClearTokenQueue(String json, Message message, Channel channel) {
try {
RegisterServerTokenReqVo req = JsonHelper.deserialize(json, RegisterServerTokenReqVo.class);
mqService.removeToken(req);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment