Pre Merge pull request !384 from imalasong/pr/1

pull/384/MERGE
imalasong 6 months ago committed by Gitee
commit 2255bbda98
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

@ -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; 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.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; 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.RemoteLogService;
import com.ruoyi.system.api.domain.SysOperLog; import com.ruoyi.system.api.domain.SysOperLog;
import java.util.concurrent.*;
/** /**
* *
* *
@ -15,15 +20,62 @@ import com.ruoyi.system.api.domain.SysOperLog;
@Service @Service
public class AsyncLogService 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 @Autowired
private RemoteLogService remoteLogService; private RemoteLogService remoteLogService;
/** /**
* *
*/ */
@Async
public void saveSysLog(SysOperLog sysOperLog) throws Exception 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…
Cancel
Save