From 8ba2ccb6ace9079e972b8f901e6e231c1288b077 Mon Sep 17 00:00:00 2001 From: luna156 Date: Tue, 20 Aug 2024 16:25:22 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=8B=9C=EA=B0=84=EA=B9=8C=EC=A7=80=20=EC=B9=B4=EC=9A=B4?= =?UTF-8?q?=ED=8A=B8=EB=8B=A4=EC=9A=B4=20=EC=8B=9C=EA=B0=84=EC=9D=84=20sse?= =?UTF-8?q?=EB=A1=9C=20=EC=A0=84=EC=86=A1=ED=95=98=EB=8A=94=20api=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ArrivalEventSseController.java | 25 +++++++++++++++++++ .../event/service/ArrivalEventService.java | 20 +++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/main/java/com/softeer/podoarrival/event/controller/ArrivalEventSseController.java diff --git a/src/main/java/com/softeer/podoarrival/event/controller/ArrivalEventSseController.java b/src/main/java/com/softeer/podoarrival/event/controller/ArrivalEventSseController.java new file mode 100644 index 0000000..1975a14 --- /dev/null +++ b/src/main/java/com/softeer/podoarrival/event/controller/ArrivalEventSseController.java @@ -0,0 +1,25 @@ +package com.softeer.podoarrival.event.controller; + +import com.softeer.podoarrival.event.service.ArrivalEventService; +import io.swagger.v3.oas.annotations.Operation; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Flux; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/arrival") +@Slf4j +public class ArrivalEventSseController { + private final ArrivalEventService arrivalEventService; + + @GetMapping(value = "/time", produces = MediaType.TEXT_EVENT_STREAM_VALUE) + @Operation(summary = "선착순 서버시간 SSE Api") + public Flux streamServerTime() { + return arrivalEventService.streamServerTime(); + } +} diff --git a/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventService.java b/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventService.java index af9615b..ed0c113 100644 --- a/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventService.java +++ b/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventService.java @@ -5,7 +5,11 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import reactor.core.publisher.Flux; +import java.time.Duration; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; import java.util.concurrent.CompletableFuture; @Slf4j @@ -14,8 +18,24 @@ public class ArrivalEventService { private final ArrivalEventReleaseService arrivalEventReleaseServiceRedisImpl; + private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); public CompletableFuture applyEvent(AuthInfo authInfo) { return arrivalEventReleaseServiceRedisImpl.applyEvent(authInfo); } + + /** + * SSE로 서버시간기준 이벤트 시작 시간까지 남은 시간을 전송 + * @return Flux sse로 전송하게되는 이벤트까지 남은 시간 + */ + public Flux streamServerTime() { + return Flux.concat( + Flux.just(0L), // Emit initial value immediately + Flux.interval(Duration.ofSeconds(20))) + .map(sequence -> { + LocalTime startTime = ArrivalEventReleaseServiceJavaImpl.getStartTime(); + long seconds = Duration.between(LocalTime.now(), startTime).getSeconds(); + return Math.max(seconds, 0); + }); + } } From adc13381620490567a7e9e36b04c29a5e9cc9918 Mon Sep 17 00:00:00 2001 From: luna156 Date: Tue, 20 Aug 2024 16:28:52 +0900 Subject: [PATCH 2/3] =?UTF-8?q?refactor:=20=ED=95=84=EC=9A=94=EC=97=86?= =?UTF-8?q?=EB=8A=94=20=EC=BD=94=EB=93=9C=20=EC=A0=9C=EA=B1=B0=20=EB=B0=8F?= =?UTF-8?q?=20=EC=A3=BC=EC=84=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../podoarrival/event/service/ArrivalEventService.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventService.java b/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventService.java index ed0c113..7c9f9d5 100644 --- a/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventService.java +++ b/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventService.java @@ -9,7 +9,6 @@ import java.time.Duration; import java.time.LocalTime; -import java.time.format.DateTimeFormatter; import java.util.concurrent.CompletableFuture; @Slf4j @@ -18,15 +17,20 @@ public class ArrivalEventService { private final ArrivalEventReleaseService arrivalEventReleaseServiceRedisImpl; - private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + /** + * 선착순 응모용 service. + * arrivalEventReleaseServiceJavaImpl과 arrivalEventReleaseServiceRedisImpl을 ArrivalEventReleaseService에 갈아끼우면 해당 방식으로 작동하게 된다. + * @param authInfo 사용자 jwt 토큰 정보 + * @return 선착순 응모 async method에 대한 CompletableFuture 타입 반환 + */ public CompletableFuture applyEvent(AuthInfo authInfo) { return arrivalEventReleaseServiceRedisImpl.applyEvent(authInfo); } /** * SSE로 서버시간기준 이벤트 시작 시간까지 남은 시간을 전송 - * @return Flux sse로 전송하게되는 이벤트까지 남은 시간 + * @return sse로 전송하게되는 이벤트까지 남은 시간 */ public Flux streamServerTime() { return Flux.concat( From cfc22ce6f974d5fed5fdd7c28deb647386f0fb8f Mon Sep 17 00:00:00 2001 From: luna156 Date: Tue, 20 Aug 2024 16:30:48 +0900 Subject: [PATCH 3/3] =?UTF-8?q?refactor:=20=EC=A0=81=EC=A0=88=ED=95=9C=20m?= =?UTF-8?q?ethod=EB=AA=85=EC=9C=BC=EB=A1=9C=20=EC=9D=B4=EB=A6=84=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../event/controller/ArrivalEventSseController.java | 4 ++-- .../podoarrival/event/service/ArrivalEventService.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/softeer/podoarrival/event/controller/ArrivalEventSseController.java b/src/main/java/com/softeer/podoarrival/event/controller/ArrivalEventSseController.java index 1975a14..bd8e1d5 100644 --- a/src/main/java/com/softeer/podoarrival/event/controller/ArrivalEventSseController.java +++ b/src/main/java/com/softeer/podoarrival/event/controller/ArrivalEventSseController.java @@ -19,7 +19,7 @@ public class ArrivalEventSseController { @GetMapping(value = "/time", produces = MediaType.TEXT_EVENT_STREAM_VALUE) @Operation(summary = "선착순 서버시간 SSE Api") - public Flux streamServerTime() { - return arrivalEventService.streamServerTime(); + public Flux streamLeftSecondsToEventTime() { + return arrivalEventService.streamLeftSecondsToEventTime(); } } diff --git a/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventService.java b/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventService.java index 7c9f9d5..d35e739 100644 --- a/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventService.java +++ b/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventService.java @@ -32,7 +32,7 @@ public CompletableFuture applyEvent(AuthInfo auth * SSE로 서버시간기준 이벤트 시작 시간까지 남은 시간을 전송 * @return sse로 전송하게되는 이벤트까지 남은 시간 */ - public Flux streamServerTime() { + public Flux streamLeftSecondsToEventTime() { return Flux.concat( Flux.just(0L), // Emit initial value immediately Flux.interval(Duration.ofSeconds(20)))