From a3ba4591724aeda1399fbf9ed182f4b3a5355031 Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Fri, 14 Mar 2025 22:23:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(session):=20=E5=88=87=E6=8D=A2=E5=BD=93?= =?UTF-8?q?=E5=89=8D=E6=B4=BB=E5=8A=A8=E6=9C=BA=E6=9E=84=E5=B9=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20SSE=20=E8=BF=9E=E6=8E=A5-=20=E5=9C=A8=20LoginContro?= =?UTF-8?q?ller=20=E4=B8=AD=E6=B7=BB=E5=8A=A0=20switchActiveOrg=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=8C=E7=94=A8=E4=BA=8E=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E6=B4=BB=E5=8A=A8=E6=9C=BA=E6=9E=84=20-=20?= =?UTF-8?q?=E5=9C=A8=20SseService=20=E4=B8=AD=E5=A2=9E=E5=8A=A0=E5=BF=83?= =?UTF-8?q?=E8=B7=B3=E6=B6=88=E6=81=AF=E5=8F=91=E9=80=81=EF=BC=8C=E4=BF=9D?= =?UTF-8?q?=E6=8C=81=20SSE=20=E8=BF=9E=E6=8E=A5=E6=B4=BB=E8=B7=83=20-=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=20OrgBaseEntity=20=E5=92=8C=20OrgCommonEntit?= =?UTF-8?q?y=EF=BC=8C=E4=BD=BF=E7=94=A8=20activeOrganizationId=20=E6=9B=BF?= =?UTF-8?q?=E4=BB=A3=20currentOrganizationId-=20=E5=9C=A8=20CoalWashingDai?= =?UTF-8?q?lyAnalysisController=20=E4=B8=AD=E8=AE=BE=E7=BD=AE=20SseEmitter?= =?UTF-8?q?=20=E7=9A=84=E8=B6=85=E6=97=B6=E6=97=B6=E9=97=B4=E4=B8=BA=2010?= =?UTF-8?q?=20=E5=88=86=E9=92=9F=20-=20=E6=96=B0=E5=A2=9E=20TaskSchedulerC?= =?UTF-8?q?onfig=20=E9=85=8D=E7=BD=AE=E7=B1=BB=EF=BC=8C=E5=90=AF=E7=94=A8?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E8=B0=83=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coal/base/entity/OrgBaseEntity.java | 2 +- .../coal/base/entity/OrgCommonEntity.java | 4 +-- .../CoalWashingDailyAnalysisController.java | 5 ++- .../session/controller/LoginController.java | 8 +++++ .../spring/config/TaskSchedulerConfig.java | 8 +++++ .../coal/sse/service/SseService.java | 34 +++++++++++++++---- 6 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 src/main/java/cn/lihongjie/coal/spring/config/TaskSchedulerConfig.java diff --git a/src/main/java/cn/lihongjie/coal/base/entity/OrgBaseEntity.java b/src/main/java/cn/lihongjie/coal/base/entity/OrgBaseEntity.java index dc18e289..bd79cbdf 100644 --- a/src/main/java/cn/lihongjie/coal/base/entity/OrgBaseEntity.java +++ b/src/main/java/cn/lihongjie/coal/base/entity/OrgBaseEntity.java @@ -21,7 +21,7 @@ public class OrgBaseEntity extends BaseEntity { if (StringUtils.isEmpty(organizationId)) { this.organizationId = - Ctx.isLoggedIn() ? Ctx.currentOrganizationId() : organizationId; + Ctx.isLoggedIn() ? Ctx.activeOrganizationId() : organizationId; } super.prePersist(); } diff --git a/src/main/java/cn/lihongjie/coal/base/entity/OrgCommonEntity.java b/src/main/java/cn/lihongjie/coal/base/entity/OrgCommonEntity.java index 7cc42f8b..43e786a6 100644 --- a/src/main/java/cn/lihongjie/coal/base/entity/OrgCommonEntity.java +++ b/src/main/java/cn/lihongjie/coal/base/entity/OrgCommonEntity.java @@ -26,7 +26,7 @@ public class OrgCommonEntity extends CommonEntity { if (StringUtils.isEmpty(organizationId)) { this.organizationId = - Ctx.isLoggedIn() ? Ctx.currentOrganizationId() : organizationId; + Ctx.isLoggedIn() ? Ctx.activeOrganizationId() : organizationId; } super.prePersist(); } @@ -36,7 +36,7 @@ public class OrgCommonEntity extends CommonEntity { if (StringUtils.isEmpty(organizationId)) { this.organizationId = - Ctx.isLoggedIn() ? Ctx.currentOrganizationId() : organizationId; + Ctx.isLoggedIn() ? Ctx.activeOrganizationId() : organizationId; } super.preUpdate(); } diff --git a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/controller/CoalWashingDailyAnalysisController.java b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/controller/CoalWashingDailyAnalysisController.java index e7549aa2..fb38cf77 100644 --- a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/controller/CoalWashingDailyAnalysisController.java +++ b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/controller/CoalWashingDailyAnalysisController.java @@ -20,6 +20,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; +import java.util.concurrent.TimeUnit; + @RequestMapping("/coalWashingDailyAnalysis") @RestController @SysLog(module = "洗煤报告表") @@ -109,7 +111,8 @@ public class CoalWashingDailyAnalysisController extends BaseController { @PostMapping("/sse") public SseEmitter sse(@RequestBody IdRequest request) { - SseEmitter emitter = new SseEmitter(); + SseEmitter emitter = new SseEmitter(TimeUnit.MINUTES.toMillis(10)); +// SseEmitter emitter = new SseEmitter(TimeUnit.SECONDS.toMillis(10)); sseService.register("coalWashingDailyAnalysis." + request.getId(), emitter); diff --git a/src/main/java/cn/lihongjie/coal/session/controller/LoginController.java b/src/main/java/cn/lihongjie/coal/session/controller/LoginController.java index 614a35b1..8ac83128 100644 --- a/src/main/java/cn/lihongjie/coal/session/controller/LoginController.java +++ b/src/main/java/cn/lihongjie/coal/session/controller/LoginController.java @@ -8,6 +8,7 @@ import cn.lihongjie.coal.common.Ctx; import cn.lihongjie.coal.rabbitmq.RabbitMQService; import cn.lihongjie.coal.session.dto.CaptchaDto; import cn.lihongjie.coal.session.dto.LoginDto; +import cn.lihongjie.coal.session.dto.SwitchActiveOrgRequest; import cn.lihongjie.coal.session.service.SessionService; import cn.lihongjie.coal.trustDevice.dto.TrustDeviceDto; import cn.lihongjie.coal.trustDevice.service.TrustDeviceService; @@ -60,7 +61,14 @@ public class LoginController extends BaseController { return true; } + @PostMapping("/switchActiveOrg") + @SysLog(action = "切换机构") + public Boolean switchActiveOrg(@RequestBody SwitchActiveOrgRequest dto) { + service.switchActiveOrg(dto); + + return true; + } @PostMapping("/genCaptcha") diff --git a/src/main/java/cn/lihongjie/coal/spring/config/TaskSchedulerConfig.java b/src/main/java/cn/lihongjie/coal/spring/config/TaskSchedulerConfig.java new file mode 100644 index 00000000..fb61c50f --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/spring/config/TaskSchedulerConfig.java @@ -0,0 +1,8 @@ +package cn.lihongjie.coal.spring.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableScheduling; + +@Configuration +@EnableScheduling +public class TaskSchedulerConfig {} diff --git a/src/main/java/cn/lihongjie/coal/sse/service/SseService.java b/src/main/java/cn/lihongjie/coal/sse/service/SseService.java index 8a6b8d74..e99367d7 100644 --- a/src/main/java/cn/lihongjie/coal/sse/service/SseService.java +++ b/src/main/java/cn/lihongjie/coal/sse/service/SseService.java @@ -1,7 +1,7 @@ package cn.lihongjie.coal.sse.service; - import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.ImmutableMap; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; @@ -9,10 +9,13 @@ import lombok.extern.slf4j.Slf4j; import org.redisson.api.RTopic; import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.TaskScheduler; import org.springframework.stereotype.Service; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import java.io.IOException; +import java.time.Duration; +import java.util.concurrent.ScheduledFuture; @Service @Slf4j @@ -22,8 +25,26 @@ public class SseService { @Autowired ObjectMapper objectMapper; + @Autowired TaskScheduler taskScheduler; + public void register(String topicId, SseEmitter emitter) { + ScheduledFuture scheduledFuture = taskScheduler.scheduleAtFixedRate( + () -> { + try { + emitter.send( + objectMapper.writeValueAsString( + ImmutableMap.of( + "time", + System.currentTimeMillis(), + "event", + "heartbeat"))); + } catch (IOException e) { + throw new RuntimeException(e); + } + }, + Duration.ofMinutes(1)); + RTopic topic = redissonClient.getTopic(topicId); int addListener = topic.addListener( @@ -41,31 +62,32 @@ public class SseService { () -> { topic.removeListener(addListener); log.info("{} remove listener:{} onCompletion ", topicId, addListener); + scheduledFuture.cancel(true); }); emitter.onTimeout( () -> { topic.removeListener(addListener); log.info("{} remove listener:{} onTimeout ", topicId, addListener); + scheduledFuture.cancel(true); }); emitter.onError( (e) -> { topic.removeListener(addListener); log.info("{} remove listener:{} onError ", topicId, addListener, e); + + scheduledFuture.cancel(true); }); + + } @SneakyThrows public void broadcast(String s, Object event) { - - - RTopic topic = redissonClient.getTopic(s); topic.publish(event instanceof String ? event : objectMapper.writeValueAsString(event)); - - } }