소스 검색

feat:自定义线程池不满足Scheduled

wangzaijun 4 주 전
부모
커밋
f9e620c8f6

+ 0 - 2
mo-daq/src/main/java/com/smppw/modaq/application/task/ParseSchedulerTask.java

@@ -10,14 +10,12 @@ import com.smppw.modaq.domain.service.TaskRecordService;
 import jakarta.annotation.PostConstruct;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
 import java.util.Date;
 
 @Component
-@EnableScheduling
 public class ParseSchedulerTask {
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
 

+ 1 - 1
mo-daq/src/main/java/com/smppw/modaq/infrastructure/config/AsyncSchedulerConfig.java

@@ -14,7 +14,7 @@ public class AsyncSchedulerConfig implements SchedulingConfigurer {
 
     private final Executor asyncExecutor;
 
-    public AsyncSchedulerConfig(@Qualifier("asyncExecutor") Executor asyncExecutor) {
+    public AsyncSchedulerConfig(@Qualifier("parseTaskScheduler") Executor asyncExecutor) {
         this.asyncExecutor = asyncExecutor;
     }
 

+ 27 - 21
mo-daq/src/main/java/com/smppw/modaq/infrastructure/config/ThreadPoolConfig.java

@@ -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;
+    }
 }