feat: 优化设备状态更新事件处理,采用异步方式以提升性能,使用专用线程池避免HTTP请求阻塞,并记录处理耗时
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -11,6 +11,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import org.springframework.context.event.EventListener;
|
import org.springframework.context.event.EventListener;
|
||||||
import org.springframework.dao.CannotAcquireLockException;
|
import org.springframework.dao.CannotAcquireLockException;
|
||||||
|
import org.springframework.scheduling.annotation.Async;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
@@ -187,17 +188,24 @@ public class DeviceTaskUpdateService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 监听设备状态更新事件 - 优化版本
|
* 监听设备状态更新事件 - 异步处理版本
|
||||||
* 移除事务注解,避免长事务导致的锁等待
|
* 使用专用线程池,避免阻塞HTTP请求线程
|
||||||
* @param event 设备状态更新事件
|
* @param event 设备状态更新事件
|
||||||
*/
|
*/
|
||||||
|
@Async("deviceDetectionExecutor")
|
||||||
@EventListener
|
@EventListener
|
||||||
public void handleDeviceStatusUpdatedEvent(DeviceStatusUpdatedEvent event) {
|
public void handleDeviceStatusUpdatedEvent(DeviceStatusUpdatedEvent event) {
|
||||||
log.debug("收到设备状态更新事件,开始处理任务更新");
|
String threadName = Thread.currentThread().getName();
|
||||||
|
log.info("收到设备状态更新事件,开始异步处理任务更新 [线程: {}]", threadName);
|
||||||
|
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
try {
|
try {
|
||||||
batchUpdateTasksByDeviceStatus(event.getDeviceStatus());
|
batchUpdateTasksByDeviceStatus(event.getDeviceStatus());
|
||||||
|
long duration = System.currentTimeMillis() - startTime;
|
||||||
|
log.info("设备状态更新处理完成,耗时: {} ms [线程: {}]", duration, threadName);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("处理设备状态更新事件时发生异常", e);
|
long duration = System.currentTimeMillis() - startTime;
|
||||||
|
log.error("处理设备状态更新事件时发生异常,耗时: {} ms [线程: {}]", duration, threadName, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user