|
|
@@ -0,0 +1,63 @@
|
|
|
+package com.mrxu.framework.boot.config;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
+
|
|
|
+import java.util.UUID;
|
|
|
+import java.util.concurrent.ThreadPoolExecutor;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 功能概要:[线程池配置类] <br>
|
|
|
+ *
|
|
|
+ * @author zzt
|
|
|
+ * @date 2022/02/26
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+public class ExecutorConfig {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 核心线程数大小
|
|
|
+ */
|
|
|
+ @Value("${mrxu.threadpool.core-pool-size:50}")
|
|
|
+ private int corePoolSize;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 最大线程数大小
|
|
|
+ */
|
|
|
+ @Value("${mrxu.threadpool.max-pool-size:100}")
|
|
|
+ private int maxPoolSize;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 队列大小
|
|
|
+ */
|
|
|
+ @Value("${mrxu.threadpool.queue-capacity:2048}")
|
|
|
+ private int queueCapacity;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 存好时间
|
|
|
+ */
|
|
|
+ @Value("${mrxu.threadpool.keep-alive-seconds:60}")
|
|
|
+ private int keepAliveSeconds;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 线程池
|
|
|
+ * @return 线程池
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public ThreadPoolTaskExecutor threadPool() {
|
|
|
+ //线程池配置
|
|
|
+ ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
|
|
|
+ threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
|
|
|
+ threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
|
|
|
+ threadPoolTaskExecutor.setQueueCapacity(queueCapacity);
|
|
|
+ String prefix = "threadpool-" + UUID.randomUUID().toString().replace("-", "") + "-";
|
|
|
+ threadPoolTaskExecutor.setThreadNamePrefix(prefix);
|
|
|
+ //采用拒绝策略->丢弃任务
|
|
|
+ threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
|
|
|
+ threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveSeconds);
|
|
|
+ threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
|
|
|
+ return threadPoolTaskExecutor;
|
|
|
+ }
|
|
|
+}
|