Skip to content

Commit 2b4eb7a

Browse files
authored
fix: sse 구독 반환(응답) 값 수정 (#174)
1 parent 0a3d3bf commit 2b4eb7a

File tree

6 files changed

+20
-14
lines changed

6 files changed

+20
-14
lines changed

src/main/java/com/somemore/notification/handler/NotificationHandlerImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.somemore.notification.repository.NotificationRepository;
66
import com.somemore.sse.domain.SseEvent;
77
import com.somemore.sse.domain.SseEventType;
8-
import com.somemore.sse.sender.SseSender;
8+
import com.somemore.sse.usecase.SseUseCase;
99
import lombok.RequiredArgsConstructor;
1010
import org.springframework.stereotype.Component;
1111

@@ -14,13 +14,13 @@
1414
public class NotificationHandlerImpl implements NotificationHandler {
1515

1616
private final NotificationRepository notificationRepository;
17-
private final SseSender sseSender;
17+
private final SseUseCase sseUseCase;
1818

1919
@Override
2020
public void handle(Notification notification) {
2121
notificationRepository.save(notification);
2222

23-
sseSender.send(createSseEvent(notification));
23+
sseUseCase.send(createSseEvent(notification));
2424
}
2525

2626
private static SseEvent<NotificationResponseDto> createSseEvent(Notification notification) {

src/main/java/com/somemore/sse/controller/SseController.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.somemore.sse.controller;
22

33
import com.somemore.auth.annotation.CurrentUser;
4-
import com.somemore.global.common.response.ApiResponse;
54
import com.somemore.sse.usecase.SseUseCase;
65
import io.swagger.v3.oas.annotations.tags.Tag;
76
import lombok.RequiredArgsConstructor;
87
import org.springframework.security.access.annotation.Secured;
98
import org.springframework.web.bind.annotation.GetMapping;
109
import org.springframework.web.bind.annotation.RequestMapping;
1110
import org.springframework.web.bind.annotation.RestController;
11+
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
1212

1313
import java.util.UUID;
1414

@@ -22,10 +22,9 @@ public class SseController {
2222

2323
@Secured({"ROLE_VOLUNTEER", "ROLE_CENTER"})
2424
@GetMapping("/subscribe")
25-
public ApiResponse<String> subscribe(
25+
public SseEmitter subscribe(
2626
@CurrentUser UUID userId) {
2727

28-
sseUseCase.subscribe(userId);
29-
return ApiResponse.ok("SSE 연결 성공");
28+
return sseUseCase.subscribe(userId);
3029
}
3130
}

src/main/java/com/somemore/sse/service/SseService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import lombok.RequiredArgsConstructor;
99
import lombok.extern.slf4j.Slf4j;
1010
import org.springframework.stereotype.Service;
11+
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
1112

1213
import java.util.UUID;
1314

@@ -20,9 +21,11 @@ public class SseService implements SseUseCase {
2021
private final SseSender sender;
2122

2223
@Override
23-
public void subscribe(UUID userId) {
24-
subscriptionManager.subscribe(userId);
24+
public SseEmitter subscribe(UUID userId) {
25+
SseEmitter sseEmitter = subscriptionManager.subscribe(userId);
2526
sendInitMessage(userId);
27+
28+
return sseEmitter;
2629
}
2730

2831
@Override
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.somemore.sse.subscriber;
22

3+
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
4+
35
import java.util.UUID;
46

57
public interface SseSubscriptionManager {
68

7-
void subscribe(UUID userId);
9+
SseEmitter subscribe(UUID userId);
810
}

src/main/java/com/somemore/sse/subscriber/SseSubscriptionManagerImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ public class SseSubscriptionManagerImpl implements SseSubscriptionManager {
1616
private final EmitterRepository emitterRepository;
1717
private static final Long TIMEOUT_MILLI_SEC = 600_000L;
1818

19-
public void subscribe(UUID userId) {
20-
initEmitter(createEmitterId(userId));
19+
public SseEmitter subscribe(UUID userId) {
20+
return initEmitter(createEmitterId(userId));
2121
}
2222

23-
private void initEmitter(String emitterId) {
23+
private SseEmitter initEmitter(String emitterId) {
2424
SseEmitter emitter = emitterRepository.save(emitterId, new SseEmitter(TIMEOUT_MILLI_SEC));
2525
setupLifeCycle(emitter, emitterId);
26+
return emitter;
2627
}
2728

2829
private String createEmitterId(UUID id) {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.somemore.sse.usecase;
22

33
import com.somemore.sse.domain.SseEvent;
4+
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
45

56
import java.util.UUID;
67

78
public interface SseUseCase {
89

9-
void subscribe(UUID userId);
10+
SseEmitter subscribe(UUID userId);
1011

1112
<T> void send(SseEvent<T> event);
1213
}

0 commit comments

Comments
 (0)