Skip to content

Commit 21b30b1

Browse files
committed
代码优化,自定义线程池优化
1 parent 64e608b commit 21b30b1

5 files changed

Lines changed: 55 additions & 175 deletions

File tree

‎eladmin-system/src/main/java/me/zhengjie/config/thread/AsyncTaskExecutePool.java‎

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package me.zhengjie.config.thread;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.Primary;
6+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
7+
import java.util.concurrent.Executor;
8+
import java.util.concurrent.ThreadPoolExecutor;
9+
10+
/**
11+
* 创建自定义的线程池
12+
* @author Zheng Jie
13+
* @description
14+
* @date 2023-06-08
15+
**/
16+
@Configuration
17+
public class CustomExecutorConfig {
18+
19+
/**
20+
* 自定义线程池,用法 @Async
21+
* @return Executor
22+
*/
23+
@Bean
24+
@Primary
25+
public Executor elAsync() {
26+
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
27+
executor.setCorePoolSize(AsyncTaskProperties.corePoolSize);
28+
executor.setMaxPoolSize(AsyncTaskProperties.maxPoolSize);
29+
executor.setQueueCapacity(AsyncTaskProperties.queueCapacity);
30+
executor.setThreadNamePrefix("el-async-");
31+
executor.setKeepAliveSeconds(AsyncTaskProperties.keepAliveSeconds);
32+
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
33+
executor.initialize();
34+
return executor;
35+
}
36+
37+
/**
38+
* 自定义线程池,用法 @Async("otherAsync")
39+
* @return Executor
40+
*/
41+
@Bean
42+
public Executor otherAsync() {
43+
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
44+
executor.setCorePoolSize(15);
45+
executor.setQueueCapacity(50);
46+
executor.setKeepAliveSeconds(AsyncTaskProperties.keepAliveSeconds);
47+
executor.setThreadNamePrefix("el-task-");
48+
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
49+
executor.initialize();
50+
return executor;
51+
}
52+
}

‎eladmin-system/src/main/java/me/zhengjie/config/thread/TheadFactoryName.java‎

Lines changed: 0 additions & 64 deletions
This file was deleted.

‎eladmin-system/src/main/java/me/zhengjie/config/thread/ThreadPoolExecutorUtil.java‎

Lines changed: 0 additions & 46 deletions
This file was deleted.

‎eladmin-system/src/main/java/me/zhengjie/modules/quartz/utils/ExecutionJob.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import cn.hutool.extra.template.TemplateConfig;
2020
import cn.hutool.extra.template.TemplateEngine;
2121
import cn.hutool.extra.template.TemplateUtil;
22-
import me.zhengjie.config.thread.ThreadPoolExecutorUtil;
2322
import me.zhengjie.domain.vo.EmailVo;
2423
import me.zhengjie.modules.quartz.domain.QuartzJob;
2524
import me.zhengjie.modules.quartz.domain.QuartzLog;
@@ -33,12 +32,13 @@
3332
import org.quartz.JobExecutionContext;
3433
import org.slf4j.Logger;
3534
import org.slf4j.LoggerFactory;
35+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
3636
import org.springframework.scheduling.quartz.QuartzJobBean;
3737
import java.util.*;
3838
import java.util.concurrent.*;
3939

4040
/**
41-
* 参考人人开源,https://gitee.com/renrenio/renren-security
41+
* 参考人人开源,<a href="https://gitee.com/renrenio/renren-security">...</a>
4242
* @author /
4343
* @date 2019-01-07
4444
*/
@@ -47,7 +47,7 @@ public class ExecutionJob extends QuartzJobBean {
4747
private final Logger logger = LoggerFactory.getLogger(this.getClass());
4848

4949
// 此处仅供参考,可根据任务执行情况自定义线程池参数
50-
private final static ExecutorService executor = ThreadPoolExecutorUtil.getPoll("el-quartz-job");
50+
private final ThreadPoolTaskExecutor executor = SpringContextHolder.getBean("elAsync");
5151

5252
@Override
5353
public void executeInternal(JobExecutionContext context) {

0 commit comments

Comments
 (0)