|
|
@@ -4,29 +4,31 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
-import org.springframework.scheduling.annotation.EnableAsync;
|
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
|
|
+/**
|
|
|
+ * @author Administrator
|
|
|
+ */
|
|
|
@Configuration
|
|
|
@Slf4j
|
|
|
public class ThreadPoolTaskConfig {
|
|
|
|
|
|
- private static final int corePoolSize = 10; // 核心线程数(默认线程数)
|
|
|
- private static final int maxPoolSize = 100; // 最大线程数
|
|
|
- private static final int keepAliveTime = 10; // 允许线程空闲时间(单位:默认为秒)
|
|
|
- private static final int queueCapacity = 200; // 缓冲队列数
|
|
|
- private static final String threadNamePrefix = "Async-Service-"; // 线程池名前缀
|
|
|
+ private static final int CORE_POOL_SIZE = 10;
|
|
|
+ private static final int MAX_POOL_SIZE = 100;
|
|
|
+ private static final int KEEP_ALIVE_TIME = 10;
|
|
|
+ private static final int QUEUE_CAPACITY = 200;
|
|
|
+ private static final String THREAD_NAME_PREFIX = "Async-Service-";
|
|
|
|
|
|
- @Bean("taskExecutor") // bean的名称,默认为首字母小写的方法名
|
|
|
- public ThreadPoolTaskExecutor getAsyncExecutor(){
|
|
|
+ @Bean
|
|
|
+ public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
|
|
|
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
|
|
- executor.setCorePoolSize(corePoolSize);
|
|
|
- executor.setMaxPoolSize(maxPoolSize);
|
|
|
- executor.setQueueCapacity(queueCapacity);
|
|
|
- executor.setKeepAliveSeconds(keepAliveTime);
|
|
|
- executor.setThreadNamePrefix(threadNamePrefix);
|
|
|
+ executor.setCorePoolSize(CORE_POOL_SIZE);
|
|
|
+ executor.setMaxPoolSize(MAX_POOL_SIZE);
|
|
|
+ executor.setQueueCapacity(QUEUE_CAPACITY);
|
|
|
+ executor.setKeepAliveSeconds(KEEP_ALIVE_TIME);
|
|
|
+ executor.setThreadNamePrefix(THREAD_NAME_PREFIX);
|
|
|
|
|
|
// 线程池对拒绝任务的处理策略
|
|
|
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
|
|
@@ -38,9 +40,9 @@ public class ThreadPoolTaskConfig {
|
|
|
/**
|
|
|
* 执行定时任务
|
|
|
*/
|
|
|
- @Bean(name = "scheduledExecutorService")
|
|
|
+ @Bean
|
|
|
public ScheduledExecutorService scheduledExecutorService() {
|
|
|
- return new ScheduledThreadPoolExecutor(corePoolSize,
|
|
|
+ return new ScheduledThreadPoolExecutor(CORE_POOL_SIZE,
|
|
|
new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build()) {
|
|
|
@Override
|
|
|
protected void afterExecute(Runnable r, Throwable t) {
|