This commit is contained in:
caozehui
2025-03-18 13:49:46 +08:00
parent 423fa581b4
commit 38cbb22eff

View File

@@ -2,7 +2,6 @@ package com.njcn.gather.system.log.aop;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.db.mybatisplus.constant.UserConstant;
@@ -13,7 +12,6 @@ import com.njcn.gather.system.log.pojo.po.SysLogAudit;
import com.njcn.gather.system.log.service.ISysLogAuditService;
import com.njcn.gather.user.user.pojo.po.SysUser;
import com.njcn.gather.user.user.service.ISysUserService;
import com.njcn.web.utils.HttpServletUtil;
import com.njcn.web.utils.RequestUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
@@ -26,11 +24,14 @@ import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
/**
* @author caozehui
@@ -104,25 +105,25 @@ public class LogAdvice implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
ExecutorService pool = new ThreadPoolExecutor(1, 1,
0L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(),
new ThreadFactoryBuilder().setNameFormat("Save-Logs-%d").setDaemon(true).build());
pool.submit(new Runnable() {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
SysLogAudit log = logQueue.poll(5, TimeUnit.MILLISECONDS);
if (log != null) {
List<SysLogAudit> logList = new ArrayList<>();
SysLogAudit log;
while ((log = logQueue.poll(5, TimeUnit.MILLISECONDS)) != null) {
log.setSort(sysLogAuditService.getMaxSort() + 1);
sysLogAuditService.save(log);
logList.add(log);
}
if (!logList.isEmpty()) {
sysLogAuditService.saveBatch(logList); // 假设有一个批量保存的方法
}
} catch (Exception e) {
throw new BusinessException(LogResponseEnum.LOG_RECORD_FAILED);
}
}
}
});
}).start();
}
}