抢红包增加线程池
This commit is contained in:
parent
e05b8bed2f
commit
24fb054f45
@ -25,6 +25,7 @@ import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/**
|
||||
* 抢房间红包命令执行器(高并发核心)
|
||||
@ -38,6 +39,7 @@ public class GrabRoomRedPacketCmdExe {
|
||||
private final RoomRedPacketRecordGateway roomRedPacketRecordGateway;
|
||||
private final RoomRedPacketCacheService roomRedPacketCacheService;
|
||||
private final WalletGoldClient walletGoldClient;
|
||||
private final Executor redPacketAsyncExecutor;
|
||||
|
||||
/**
|
||||
* 执行抢红包(高并发场景)
|
||||
@ -90,7 +92,7 @@ public class GrabRoomRedPacketCmdExe {
|
||||
String recordId = IdWorkerUtils.getIdStr();
|
||||
CompletableFuture.runAsync(() -> {
|
||||
asyncProcessGrab(packetId, cmd.getReqSysOrigin().getOrigin(), userId, amount, remainCount, recordId);
|
||||
});
|
||||
}, redPacketAsyncExecutor);
|
||||
|
||||
log.info("抢红包成功 userId={} packetId={} amount={} remainCount={}",
|
||||
userId, packetId, amount, remainCount);
|
||||
|
||||
@ -97,4 +97,43 @@ public class RocketAsyncConfig {
|
||||
|
||||
return executor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 红包异步处理专用线程池
|
||||
* 场景:抢红包后的异步写库和钱包入账
|
||||
*/
|
||||
@Bean("redPacketAsyncExecutor")
|
||||
public Executor redPacketAsyncExecutor() {
|
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||
|
||||
// 核心线程数
|
||||
executor.setCorePoolSize(8);
|
||||
|
||||
// 最大线程数
|
||||
executor.setMaxPoolSize(20);
|
||||
|
||||
// 队列容量
|
||||
executor.setQueueCapacity(200);
|
||||
|
||||
// 线程名前缀
|
||||
executor.setThreadNamePrefix("red-packet-async-");
|
||||
|
||||
// 拒绝策略:重要任务,使用调用者运行
|
||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
||||
|
||||
// 线程空闲时间
|
||||
executor.setKeepAliveSeconds(60);
|
||||
|
||||
// 优雅关闭
|
||||
executor.setWaitForTasksToCompleteOnShutdown(true);
|
||||
executor.setAwaitTerminationSeconds(60);
|
||||
|
||||
executor.initialize();
|
||||
|
||||
log.info("红包异步线程池初始化完成: core={}, max={}, queue={}",
|
||||
executor.getCorePoolSize(), executor.getMaxPoolSize(), executor.getQueueCapacity());
|
||||
|
||||
return executor;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user