From e36a28cf85bc8926a379056468960bcdda04d21f Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Tue, 7 Apr 2026 18:07:13 +0800 Subject: [PATCH] =?UTF-8?q?refactor(live):=20=E6=B8=85=E7=90=86=E5=BF=83?= =?UTF-8?q?=E8=B7=B3=E4=B8=8E=E4=B8=8A=E9=BA=A6=E9=80=BB=E8=BE=91=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E6=AD=BB=E4=BB=A3=E7=A0=81=E5=92=8C=E6=97=A0=E6=95=88?= =?UTF-8?q?=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit LiveMicGoUpCmdExe: - 删除遗留的 main 方法和 createAgoraTokenGoUp 死方法(后者每次调用都 new 线程池,有资源泄漏风险) - handleRoomDailyTask 提前短路:非邀请类型或非房主邀请时直接 return,避免无效 HTTP 调用 - roomMemberDTO 查询后移到确认需要时才执行 UserHeartbeatListener: - 移除 userProfileClient 无效注入(逻辑已注释) - 删除 getRoomProfileDto 死方法(上次重构后已无调用) - 清理 StringUtils/RegexConstant/Profile/ResultResponse/RedisTemplate 等无用 import Co-Authored-By: Claude Sonnet 4.6 (cherry picked from commit 6f210e34c83a10721ea5f772f5951ed0547c8e46) --- .../app/command/mic/LiveMicGoUpCmdExe.java | 72 +++---------------- .../app/listener/UserHeartbeatListener.java | 24 ------- 2 files changed, 8 insertions(+), 88 deletions(-) diff --git a/rc-service/rc-service-live/live-application/src/main/java/com/red/circle/live/app/command/mic/LiveMicGoUpCmdExe.java b/rc-service/rc-service-live/live-application/src/main/java/com/red/circle/live/app/command/mic/LiveMicGoUpCmdExe.java index 145d05ba..025bfdd4 100644 --- a/rc-service/rc-service-live/live-application/src/main/java/com/red/circle/live/app/command/mic/LiveMicGoUpCmdExe.java +++ b/rc-service/rc-service-live/live-application/src/main/java/com/red/circle/live/app/command/mic/LiveMicGoUpCmdExe.java @@ -27,8 +27,7 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import java.util.Objects; -import java.util.concurrent.*; -import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.TimeUnit; /** * 上麦克风. @@ -77,20 +76,21 @@ public class LiveMicGoUpCmdExe { private void handleRoomDailyTask(MicUpDownCmd cmd) { try { + if (!cmd.eventTypeEqInvite()) { + return; + } RoomProfileDTO roomProfile = roomManagerClient.getById(cmd.getRoomId()).getBody(); if (Objects.isNull(roomProfile)) { return; } + if (!Objects.equals(roomProfile.getUserId(), cmd.getInviterId())) { + return; + } RoomMemberDTO roomMemberDTO = roomMemberClient.getMember(cmd.getRoomId(), cmd.getReqUserId()).getBody(); if (Objects.isNull(roomMemberDTO)) { return; } - - boolean invite = cmd.eventTypeEqInvite(); - if (invite && Objects.equals(roomProfile.getUserId(), cmd.getInviterId())) { - Long roomOwnerId = roomProfile.getUserId(); - handleInviteMicTask(roomOwnerId, cmd.requiredReqUserId(), RoomDailyTaskCode.ROOM_OWNER_INVITE_MIC); - } + handleInviteMicTask(roomProfile.getUserId(), cmd.requiredReqUserId(), RoomDailyTaskCode.ROOM_OWNER_INVITE_MIC); } catch (Exception exception){ log.error("handleRoomDailyTask exception: {}", exception.getMessage()); } @@ -126,60 +126,4 @@ public class LiveMicGoUpCmdExe { } } - /** - * 异步拿声网token,5分钟拿不到就返回默认一串字符串 - * @param cmd - * @return - */ - private String createAgoraTokenGoUp(MicUpDownCmd cmd) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - AtomicReference agoraToken = new AtomicReference<>("BaHrIypJdFWqwWtoZLToRucRyoGrVgYCONBFvGynmlJsevFYsWBfEIcaWUUGijQBjycTtmSvkmBINoSltJMmgiPeBnkaWErixaxg"); - Future future = executor.submit(() -> { - agoraToken.set(trtcClient.createAgoraTokenGoUp(cmd.getReqUserId(), cmd.getRoomId().toString()).getBody()); - }); - - try { - future.get(3, TimeUnit.SECONDS); // 等待最多3秒 - } catch (TimeoutException e) { - // 处理超时 - log.warn("上麦获取声网token超时: {}", e.getMessage()); - } catch (InterruptedException | ExecutionException e) { - // 处理其他异常 - log.warn("上麦获取声网token其他异常: {}", e.getMessage()); - } - - executor.shutdown(); - return agoraToken.get(); - } - - public static void main(String[] args) { - System.out.println("开始等待..."); - ExecutorService executor = Executors.newSingleThreadExecutor(); - AtomicReference agoraToken = new AtomicReference<>(""); - Future future = executor.submit(() -> { - // 等待6秒 - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - agoraToken.set("拿到值啦!!!!!"); - }); - - try { - future.get(5, TimeUnit.SECONDS); // 等待最多3秒 - } catch (TimeoutException e) { - // 处理超时 - log.warn("上麦获取声网token超时: {}", e); - } catch (InterruptedException | ExecutionException e) { - // 处理其他异常 - log.warn("上麦获取声网token其他异常: {}", e); - } - - executor.shutdown(); - System.out.println(agoraToken.get()); - System.out.println("结束..."); - } - - } diff --git a/rc-service/rc-service-live/live-application/src/main/java/com/red/circle/live/app/listener/UserHeartbeatListener.java b/rc-service/rc-service-live/live-application/src/main/java/com/red/circle/live/app/listener/UserHeartbeatListener.java index df9efc74..aeba216d 100644 --- a/rc-service/rc-service-live/live-application/src/main/java/com/red/circle/live/app/listener/UserHeartbeatListener.java +++ b/rc-service/rc-service-live/live-application/src/main/java/com/red/circle/live/app/listener/UserHeartbeatListener.java @@ -7,7 +7,6 @@ import com.red.circle.component.mq.service.Action; import com.red.circle.component.mq.service.ConsumerMessage; import com.red.circle.component.mq.service.MessageListener; import com.red.circle.component.redis.service.RedisService; -import com.red.circle.framework.dto.ResultResponse; import com.red.circle.live.app.dto.cmd.HeartbeatStatusEnum; import com.red.circle.live.domain.gateway.LiveMicrophoneGateway; import com.red.circle.live.domain.live.LiveMicrophone; @@ -23,7 +22,6 @@ import com.red.circle.other.inner.endpoint.live.RoomManagerClient; import com.red.circle.other.inner.endpoint.live.RoomMemberClient; import com.red.circle.other.inner.endpoint.task.RoomDailyTaskClient; import com.red.circle.other.inner.endpoint.user.user.UserOnlineClient; -import com.red.circle.other.inner.endpoint.user.user.UserProfileClient; import com.red.circle.other.inner.enums.live.RoomUserRolesEnum; import com.red.circle.other.inner.enums.task.RoomDailyTaskCode; import com.red.circle.other.inner.model.cmd.live.MemberRolesQryCmd; @@ -35,9 +33,7 @@ import com.red.circle.other.inner.model.dto.user.UserProfileDTO; import com.red.circle.tool.core.collection.CollectionUtils; import com.red.circle.tool.core.date.ZonedDateTimeAsiaRiyadhUtils; import com.red.circle.tool.core.parse.DataTypeUtils; -import com.red.circle.tool.core.regex.RegexConstant; import com.red.circle.tool.core.sequence.IdWorkerUtils; -import com.red.circle.tool.core.text.StringUtils; import java.util.List; import java.util.Map; import java.util.Objects; @@ -47,8 +43,6 @@ import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.context.annotation.Profile; -import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; /** @@ -63,7 +57,6 @@ import org.springframework.stereotype.Component; public class UserHeartbeatListener implements MessageListener { private final UserOnlineClient userOnlineClient; - private final UserProfileClient userProfileClient; private final RoomManagerClient roomManagerClient; private final LiveMicCacheService liveMicCacheService; private final LiveMicrophoneGateway liveMicrophoneGateway; @@ -207,23 +200,6 @@ public class UserHeartbeatListener implements MessageListener { refreshMicActiveTime(event, roomProfile, userProfile); } - private RoomProfileDTO getRoomProfileDto(Long roomId) { - String roomAccount = liveRoomCacheService.getRoomAccount(roomId); - if (Objects.isNull(roomAccount)) { - RoomProfileDTO roomProfile = roomManagerClient.getById(roomId).getBody(); - if (Objects.nonNull(roomProfile)) { - liveRoomCacheService.save(roomProfile.getRoomAccount(), roomId); - roomAccount = roomProfile.getRoomAccount(); - } - } - - RoomProfileDTO roomProfile = new RoomProfileDTO(); - roomProfile.setId(roomId); - roomProfile.setRoomAccount(roomAccount); - - return roomProfile; - } - private void addRoomOnlineUser(UserProfileDTO userProfile, RoomProfileDTO roomProfile) { liveMicrophoneGateway.addRoomOnlineUser(roomProfile.getId(),