Pre Merge pull request !384 from imalasong/pr/1
commit
2255bbda98
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue