From 19a5fd715f425ec2f204a0d2519adf5cb2e66937 Mon Sep 17 00:00:00 2001 From: zyh <50652658+zyh530@users.noreply.github.com> Date: Fri, 3 Oct 2025 11:20:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=9B=B4=E6=96=B0=E4=BA=8B=E4=BB=B6=E5=A4=84?= =?UTF-8?q?=E7=90=86=EF=BC=8C=E9=87=87=E7=94=A8=E5=BC=82=E6=AD=A5=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E4=BB=A5=E6=8F=90=E5=8D=87=E6=80=A7=E8=83=BD=EF=BC=8C?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E4=B8=93=E7=94=A8=E7=BA=BF=E7=A8=8B=E6=B1=A0?= =?UTF-8?q?=E9=81=BF=E5=85=8DHTTP=E8=AF=B7=E6=B1=82=E9=98=BB=E5=A1=9E?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E8=AE=B0=E5=BD=95=E5=A4=84=E7=90=86=E8=80=97?= =?UTF-8?q?=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/config/AsyncConfig.java | 65 +++++++++++++++++++ .../service/link/DeviceTaskUpdateService.java | 16 +++-- 2 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/gameplatform/server/config/AsyncConfig.java diff --git a/src/main/java/com/gameplatform/server/config/AsyncConfig.java b/src/main/java/com/gameplatform/server/config/AsyncConfig.java new file mode 100644 index 0000000..d9a3488 --- /dev/null +++ b/src/main/java/com/gameplatform/server/config/AsyncConfig.java @@ -0,0 +1,65 @@ +package com.gameplatform.server.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 java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; + +/** + * 异步任务配置 + * 用于设备检测等后台任务的异步处理 + */ +@Configuration +@EnableAsync +public class AsyncConfig { + private static final Logger log = LoggerFactory.getLogger(AsyncConfig.class); + + /** + * 设备检测专用线程池 + * 避免阻塞HTTP请求处理线程 + */ + @Bean(name = "deviceDetectionExecutor") + public Executor deviceDetectionExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + + // 核心线程数:根据设备数量调整,建议2-4个 + executor.setCorePoolSize(3); + + // 最大线程数:高峰时可扩展 + executor.setMaxPoolSize(10); + + // 队列容量:允许排队的任务数 + executor.setQueueCapacity(100); + + // 线程名称前缀,方便日志追踪 + executor.setThreadNamePrefix("DeviceDetect-"); + + // 拒绝策略:队列满时由调用线程执行,确保任务不丢失 + executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); + + // 线程空闲时间(秒) + executor.setKeepAliveSeconds(60); + + // 允许核心线程超时 + executor.setAllowCoreThreadTimeOut(true); + + // 等待任务完成后再关闭 + executor.setWaitForTasksToCompleteOnShutdown(true); + + // 关闭等待时间(秒) + executor.setAwaitTerminationSeconds(60); + + executor.initialize(); + + log.info("设备检测线程池已初始化: coreSize={}, maxSize={}, queueCapacity={}", + executor.getCorePoolSize(), executor.getMaxPoolSize(), executor.getQueueCapacity()); + + return executor; + } +} + diff --git a/src/main/java/com/gameplatform/server/service/link/DeviceTaskUpdateService.java b/src/main/java/com/gameplatform/server/service/link/DeviceTaskUpdateService.java index 7a4f8e9..b1514e0 100644 --- a/src/main/java/com/gameplatform/server/service/link/DeviceTaskUpdateService.java +++ b/src/main/java/com/gameplatform/server/service/link/DeviceTaskUpdateService.java @@ -11,6 +11,7 @@ import org.slf4j.LoggerFactory; import org.springframework.context.event.EventListener; import org.springframework.dao.CannotAcquireLockException; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -187,17 +188,24 @@ public class DeviceTaskUpdateService { } /** - * 监听设备状态更新事件 - 优化版本 - * 移除事务注解,避免长事务导致的锁等待 + * 监听设备状态更新事件 - 异步处理版本 + * 使用专用线程池,避免阻塞HTTP请求线程 * @param event 设备状态更新事件 */ + @Async("deviceDetectionExecutor") @EventListener public void handleDeviceStatusUpdatedEvent(DeviceStatusUpdatedEvent event) { - log.debug("收到设备状态更新事件,开始处理任务更新"); + String threadName = Thread.currentThread().getName(); + log.info("收到设备状态更新事件,开始异步处理任务更新 [线程: {}]", threadName); + + long startTime = System.currentTimeMillis(); try { batchUpdateTasksByDeviceStatus(event.getDeviceStatus()); + long duration = System.currentTimeMillis() - startTime; + log.info("设备状态更新处理完成,耗时: {} ms [线程: {}]", duration, threadName); } catch (Exception e) { - log.error("处理设备状态更新事件时发生异常", e); + long duration = System.currentTimeMillis() - startTime; + log.error("处理设备状态更新事件时发生异常,耗时: {} ms [线程: {}]", duration, threadName, e); } } }