From a8cdd37871464fb6fd8ab6ada2e3c7988378ead0 Mon Sep 17 00:00:00 2001 From: xiaochangbai <704566072@qq.com> Date: Wed, 20 Nov 2024 12:13:50 +0800 Subject: [PATCH] =?UTF-8?q?opt:=E4=BC=98=E5=8C=96=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/executor/ThreadFactoryImpl.java | 31 ++++++++++ .../common/log/service/AsyncLogService.java | 56 ++++++++++++++++++- 2 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/executor/ThreadFactoryImpl.java diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/executor/ThreadFactoryImpl.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/executor/ThreadFactoryImpl.java new file mode 100644 index 00000000..e5c722e4 --- /dev/null +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/executor/ThreadFactoryImpl.java @@ -0,0 +1,31 @@ +package com.ruoyi.common.core.executor; + +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.atomic.AtomicLong; + +/** + * 线程工厂 + * + * @author ruoyi + */ +public class ThreadFactoryImpl implements ThreadFactory { + private final AtomicLong threadIndex = new AtomicLong(0); + private final String threadNamePrefix; + private final boolean daemon; + + public ThreadFactoryImpl(final String threadNamePrefix) { + this(threadNamePrefix, false); + } + + public ThreadFactoryImpl(final String threadNamePrefix, boolean daemon) { + this.threadNamePrefix = threadNamePrefix; + this.daemon = daemon; + } + + @Override + public Thread newThread(Runnable r) { + Thread thread = new Thread(r, threadNamePrefix + this.threadIndex.incrementAndGet()); + thread.setDaemon(daemon); + return thread; + } +} diff --git a/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/service/AsyncLogService.java b/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/service/AsyncLogService.java index eb9a13aa..224da9f7 100644 --- a/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/service/AsyncLogService.java +++ b/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/service/AsyncLogService.java @@ -1,5 +1,8 @@ package com.ruoyi.common.log.service; +import com.ruoyi.common.core.executor.ThreadFactoryImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @@ -7,6 +10,8 @@ import com.ruoyi.common.core.constant.SecurityConstants; import com.ruoyi.system.api.RemoteLogService; import com.ruoyi.system.api.domain.SysOperLog; +import java.util.concurrent.*; + /** * 异步调用日志服务 * @@ -15,15 +20,62 @@ import com.ruoyi.system.api.domain.SysOperLog; @Service public class AsyncLogService { + + private static final Logger log = LoggerFactory.getLogger(AsyncLogService.class); + + private final static ExecutorService logSaveExecutorService = new ThreadPoolExecutor(2,10,10, TimeUnit.SECONDS, + new LinkedBlockingQueue<>(1000),new ThreadFactoryImpl("AsyncLogServiceExecutorService",true), + new RejectedExecutionHandler() { + //丢弃任务 + @Override + public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { + if (r instanceof AsyncLogService.RunData) { + SysOperLog msg = ((AsyncLogService.RunData) r).getData(); + log.error("保存日志繁忙,丢弃日志:{}",msg); + } + } + }); + @Autowired private RemoteLogService remoteLogService; /** * 保存系统日志记录 */ - @Async public void saveSysLog(SysOperLog sysOperLog) throws Exception { - remoteLogService.saveLog(sysOperLog, SecurityConstants.INNER); + RunData runData = new RunData(); + runData.setData(sysOperLog); + runData.setRunnable(()->{ + try { + remoteLogService.saveLog(sysOperLog, SecurityConstants.INNER); + } catch (Exception e) { + log.error("保存日志异常:{}", e.getMessage()); + throw new RuntimeException(e); + } + }); + logSaveExecutorService.execute(runData); + } + + + static class RunData implements Runnable { + SysOperLog data = null; + Runnable runnable; + @Override + public void run() { + runnable.run(); + } + + public SysOperLog getData() { + return data; + } + + public void setData(SysOperLog data) { + this.data = data; + } + + public void setRunnable(Runnable runnable) { + this.runnable = runnable; + } } }