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); } } }