|
@@ -1,36 +1,19 @@
|
|
|
package com.smppw.modaq.infrastructure.config;
|
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
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 org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
|
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
|
@Configuration
|
|
|
@EnableAsync
|
|
|
public class ThreadPoolConfig {
|
|
|
-
|
|
|
-// @Bean("valuationExecutor")
|
|
|
-// public ThreadPoolTaskExecutor valuationExecutor() {
|
|
|
-// ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
|
|
-// int cpuSize = Runtime.getRuntime().availableProcessors();
|
|
|
-// cpuSize = Math.max(cpuSize/2, 1);
|
|
|
-//
|
|
|
-// taskExecutor.setCorePoolSize(cpuSize);
|
|
|
-// taskExecutor.setMaxPoolSize(50);
|
|
|
-// taskExecutor.setQueueCapacity(1024 * 4);
|
|
|
-// taskExecutor.setKeepAliveSeconds(60);
|
|
|
-// taskExecutor.setThreadNamePrefix("valuationExecutor--");
|
|
|
-// taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
|
|
|
-// taskExecutor.setAwaitTerminationSeconds(60);
|
|
|
-//
|
|
|
-// // 修改拒绝策略为使用当前线程执行
|
|
|
-// taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
|
|
-// // 初始化线程池
|
|
|
-// taskExecutor.initialize();
|
|
|
-// return taskExecutor;
|
|
|
-// }
|
|
|
+ private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
|
|
|
@Bean("asyncExecutor")
|
|
|
public ThreadPoolTaskExecutor asyncExecutor() {
|
|
@@ -49,4 +32,27 @@ public class ThreadPoolConfig {
|
|
|
taskExecutor.initialize();
|
|
|
return taskExecutor;
|
|
|
}
|
|
|
+
|
|
|
+ @Bean(name = "parseTaskScheduler")
|
|
|
+ public ThreadPoolTaskScheduler taskScheduler() {
|
|
|
+ ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
|
|
|
+
|
|
|
+ // 核心配置参数
|
|
|
+ scheduler.setPoolSize(5); // 线程池大小
|
|
|
+ scheduler.setThreadNamePrefix("parse-scheduler-"); // 线程名前缀
|
|
|
+ scheduler.setAwaitTerminationSeconds(60); // 关闭时等待任务完成的时间
|
|
|
+ scheduler.setWaitForTasksToCompleteOnShutdown(true); // 关闭时等待任务完成
|
|
|
+ scheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝策略
|
|
|
+
|
|
|
+ // 可选配置
|
|
|
+ scheduler.setErrorHandler(t -> {
|
|
|
+ this.logger.error("定时任务执行异常: {}", t.getMessage());
|
|
|
+ // 这里可以添加自定义错误处理逻辑
|
|
|
+ });
|
|
|
+
|
|
|
+ scheduler.setRemoveOnCancelPolicy(true); // 取消后立即移除任务
|
|
|
+ scheduler.initialize(); // 初始化线程池
|
|
|
+
|
|
|
+ return scheduler;
|
|
|
+ }
|
|
|
}
|