diff --git a/viewsh-module-ops/viewsh-module-ops-biz/src/main/java/com/viewsh/module/ops/config/AsyncConfiguration.java b/viewsh-module-ops/viewsh-module-ops-biz/src/main/java/com/viewsh/module/ops/config/AsyncConfiguration.java new file mode 100644 index 0000000..c6a9124 --- /dev/null +++ b/viewsh-module-ops/viewsh-module-ops-biz/src/main/java/com/viewsh/module/ops/config/AsyncConfiguration.java @@ -0,0 +1,61 @@ +package com.viewsh.module.ops.config; + +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.AsyncConfigurer; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; + +/** + * 异步任务配置 + *
+ * 用于处理工单通知、语音播报等异步任务 + * + * @author lzh + */ +@Slf4j +@Configuration +@EnableAsync +public class AsyncConfiguration implements AsyncConfigurer { + + /** + * 工单任务专用线程池 + */ + @Bean(name = "ops-task-executor") + public Executor opsTaskExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + // 核心线程数:系统核心数 + executor.setCorePoolSize(Runtime.getRuntime().availableProcessors()); + // 最大线程数:核心数 * 2 + executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 2); + // 队列容量 + executor.setQueueCapacity(100); + // 线程名前缀 + executor.setThreadNamePrefix("ops-task-"); + // 拒绝策略:由调用线程执行 + executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); + // 等待所有任务完成后再关闭线程池 + executor.setWaitForTasksToCompleteOnShutdown(true); + // 等待时间(秒) + executor.setAwaitTerminationSeconds(60); + executor.initialize(); + return executor; + } + + /** + * 异步异常处理器 + */ + @Override + public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { + return (throwable, method, params) -> { + log.error("[AsyncTask][异步任务执行失败] method={}, params={}, error={}", + method.getName(), params, throwable.getMessage(), throwable); + }; + } +}