ThreadPoolTaskExecutorConfig.java
1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.huaheng.common.config;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ThreadPoolExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
/**
* @auth lipf
* @date 2020/7/9 14:16
*/
@Configuration
@Slf4j
public class ThreadPoolTaskExecutorConfig {
@Bean(name = "quartzTaskExecutor")
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
log.info("启动定时任务Quartz线程池");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 核心线程数
executor.setCorePoolSize(8);
// 最大线程数
executor.setMaxPoolSize(64);
// 任务队列大小
executor.setQueueCapacity(256);
// 线程前缀名
executor.setThreadNamePrefix("Quartz");
// 允许核心线程超时
executor.setAllowCoreThreadTimeOut(true);
// 线程的空闲时间
executor.setKeepAliveSeconds(300);
executor.setThreadFactory(new ThreadFactoryBuilder().setNameFormat("Quartz定时任务-runner-%d").build());
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setAwaitTerminationSeconds(30);
// 拒绝策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
// 线程初始化
// executor.initialize();
return executor;
}
}