Skip to content

Commit

Permalink
增加线程池配置
Browse files Browse the repository at this point in the history
  • Loading branch information
xusheng1987 committed Jan 8, 2021
1 parent 5ed9d49 commit 626d5ec
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.flying.jeelite.modules.config;

import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
* 线程池配置
*
*/
@Configuration
public class ThreadPoolConfig {

@Bean("taskExecutor")
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);//核心线程数
executor.setMaxPoolSize(20);//最大线程数
executor.setQueueCapacity(200);//缓冲队列容量
executor.setKeepAliveSeconds(60);//允许线程空闲的时间60秒:当超过了核心线程之外的线程在空闲时间到达之后会被销毁
executor.setWaitForTasksToCompleteOnShutdown(true);//线程池关闭的时候等待所有任务都完成
executor.setAwaitTerminationSeconds(60);//设置线程池关闭时,线程池中任务的等待时间,如果超过这个时间任务还没有关闭就强制关闭
executor.setThreadNamePrefix("taskExecutor-");//线程池名的前缀
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());//拒绝策略
return executor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.core.task.TaskExecutor;
import org.springframework.web.method.HandlerMethod;

import com.google.common.collect.Lists;
Expand All @@ -37,6 +38,7 @@ public class LogUtils {

private static LogDao logDao = SpringContextHolder.getBean(LogDao.class);
private static MenuDao menuDao = SpringContextHolder.getBean(MenuDao.class);
private static TaskExecutor taskExecutor = SpringContextHolder.getBean(TaskExecutor.class);

/**
* 保存日志
Expand All @@ -63,7 +65,8 @@ public static void saveLog(HttpServletRequest request, Object handler, Exception
log.setCreateDate(new Date());
log.setCostTime(costTime);
// 异步保存日志
new SaveLogThread(log, handler, ex).start();
taskExecutor.execute(new SaveLogThread(log, handler, ex));
//new SaveLogThread(log, handler, ex).start();
// }
}

Expand Down

0 comments on commit 626d5ec

Please sign in to comment.