Commit aa671aaa authored by yanzg's avatar yanzg

修复等待时间

parent d22eb0ba
package com.yanzuoguang.util.thread; package com.yanzuoguang.util.thread;
import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.exception.ExceptionHelper; import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.DateHelper;
import java.util.Date; import java.util.Date;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.function.Predicate;
/** /**
* 线程帮助类 * 线程帮助类
*
* @author 颜佐光 * @author 颜佐光
*/ */
public class ThreadHelper { public class ThreadHelper {
...@@ -237,4 +239,23 @@ public class ThreadHelper { ...@@ -237,4 +239,23 @@ public class ThreadHelper {
}); });
} }
/**
* 等待条件是否满足
*
* @param waitMax 等待最大值
* @param waitItem 每次等待值
* @param cond 判断等待条件(已等待时间)
*/
public static void waitRun(long waitMax, int waitItem, Predicate<Long> cond) {
long waitStart = System.currentTimeMillis();
long waitTime = 0;
while (cond.test(waitTime)) {
// 等待线程同步,超过该时间则不继续等待
ThreadHelper.sleep(waitItem);
waitTime = System.currentTimeMillis() - waitStart;
if (waitTime > waitMax) {
break;
}
}
}
} }
...@@ -20,6 +20,8 @@ import org.springframework.stereotype.Component; ...@@ -20,6 +20,8 @@ import org.springframework.stereotype.Component;
@Component @Component
public class AspectLogResult { public class AspectLogResult {
public static final int WAIT_MAX = 30;
public static final int WAIT_ITEM = 10;
private final CloudConfig cloudConfig; private final CloudConfig cloudConfig;
private final LogLocal logLocal; private final LogLocal logLocal;
private final AspectLogBody aspectLogBody; private final AspectLogBody aspectLogBody;
...@@ -50,9 +52,9 @@ public class AspectLogResult { ...@@ -50,9 +52,9 @@ public class AspectLogResult {
public void responseLog(Class<?> cls, String tag, String url, boolean logFlag, long start, public void responseLog(Class<?> cls, String tag, String url, boolean logFlag, long start,
Object requestBody, Object result, Exception resultEx, LogVo log) { Object requestBody, Object result, Exception resultEx, LogVo log) {
try { try {
ThreadHelper.waitRun(WAIT_MAX, WAIT_ITEM, k -> StringHelper.isEmpty(log.getLogId()));
if (StringHelper.isEmpty(log.getLogId())) { if (StringHelper.isEmpty(log.getLogId())) {
// 等待线程同步,超过该时间则不继续等待 return;
ThreadHelper.sleep(30);
} }
String body = aspectLogBody.getBodyString(requestBody); String body = aspectLogBody.getBodyString(requestBody);
if (!StringHelper.compare(log.getContent(), body)) { if (!StringHelper.compare(log.getContent(), body)) {
......
...@@ -66,15 +66,16 @@ public class LogLocal implements ThreadNext.Next, InitializingBean { ...@@ -66,15 +66,16 @@ public class LogLocal implements ThreadNext.Next, InitializingBean {
* @param content 请求内容 * @param content 请求内容
*/ */
public void startLog(LogVo log, String actionKey, String actionSubKey, String content) { public void startLog(LogVo log, String actionKey, String actionSubKey, String content) {
// 声明超时对象
Timeout<LogVo> time = new Timeout<>(log);
log.setCreateDate(DateHelper.getDateTimeString(new Date(time.getStart())));
// 写入其他对象 // 写入其他对象
log.setActionKey(actionKey); log.setActionKey(actionKey);
log.setActionSubKey(actionSubKey); log.setActionSubKey(actionSubKey);
log.setContent(content); log.setContent(content);
// 声明超时对象
Timeout<LogVo> time = new Timeout<>(log);
log.setCreateDate(DateHelper.getDateTimeString(new Date(time.getStart())));
// 生命日志对象爱嗯 // 生命日志对象爱嗯
log.setLogId(StringHelper.getNewID()); log.setLogId(StringHelper.getNewID());
cache.put(log.getLogId(), time); cache.put(log.getLogId(), time);
} }
......
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