From 75a9df9f9fb27947f003a508fdd11a2c881a0f77 Mon Sep 17 00:00:00 2001 From: lzh Date: Fri, 9 Jan 2026 17:41:37 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E3=80=90ops=E3=80=91=E5=BC=82?= =?UTF-8?q?=E6=AD=A5=E4=BB=BB=E5=8A=A1=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/ops/config/AsyncConfiguration.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 viewsh-module-ops/viewsh-module-ops-biz/src/main/java/com/viewsh/module/ops/config/AsyncConfiguration.java 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); + }; + } +}