From 24fb054f45c53937b99cb7dec389a14dee099d96 Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Wed, 26 Nov 2025 10:27:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=A2=E7=BA=A2=E5=8C=85=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../redpacket/GrabRoomRedPacketCmdExe.java | 4 +- .../other/app/config/RocketAsyncConfig.java | 39 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/redpacket/GrabRoomRedPacketCmdExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/redpacket/GrabRoomRedPacketCmdExe.java index 5f3dfe5e..544cbf4b 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/redpacket/GrabRoomRedPacketCmdExe.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/redpacket/GrabRoomRedPacketCmdExe.java @@ -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); diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/config/RocketAsyncConfig.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/config/RocketAsyncConfig.java index 45cb6de9..b84a3dfe 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/config/RocketAsyncConfig.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/config/RocketAsyncConfig.java @@ -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; + } + }