-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
마인드맵 생성 SSE 적용
- Loading branch information
Showing
5 changed files
with
134 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/com/smart/watchboard/repository/EmitterRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.smart.watchboard.repository; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class EmitterRepository { | ||
private final Map<Long, SseEmitter> emitters = new ConcurrentHashMap<>(); | ||
|
||
public void save(Long id, SseEmitter emitter) { | ||
emitters.put(id, emitter); | ||
} | ||
|
||
public void deleteById(Long id) { | ||
emitters.remove(id); | ||
} | ||
|
||
public SseEmitter get(Long id) { | ||
return emitters.get(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/java/com/smart/watchboard/service/SseService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.smart.watchboard.service; | ||
|
||
import com.smart.watchboard.repository.EmitterRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class SseService { | ||
private static final Long DEFAULT_TIMEOUT = 60L * 1000 * 60; | ||
private final EmitterRepository emitterRepository; | ||
private final FileService fileService; | ||
private final WhiteboardService whiteboardService; | ||
private final RequestService requestService; | ||
private final LectureNoteService lectureNoteService; | ||
private final STTService sttService; | ||
|
||
public SseEmitter subscribe(Long documentId) { | ||
SseEmitter emitter = createEmitter(documentId); | ||
|
||
sendToClientFirst(documentId, "EventStream Created. [documentId=" + documentId + "]"); | ||
return emitter; | ||
} | ||
|
||
public void notify(Long documentId, List<String> keywords) { | ||
sendToClient(documentId, keywords); | ||
} | ||
|
||
private void sendToClientFirst(Long documentId, Object data) { | ||
SseEmitter emitter = emitterRepository.get(documentId); | ||
if (emitter != null) { | ||
try { | ||
emitter.send(SseEmitter.event().id(String.valueOf(documentId)).name("sse").data(data)); | ||
} catch (IOException exception) { | ||
emitterRepository.deleteById(documentId); | ||
emitter.completeWithError(exception); | ||
} | ||
} | ||
} | ||
|
||
private void sendToClient(Long documentId, List<String> keywords) { | ||
SseEmitter emitter = emitterRepository.get(documentId); | ||
if (emitter != null) { | ||
try { | ||
if (whiteboardService.isPdfType(documentId)) { | ||
String path = fileService.getPdfUrl(documentId); | ||
ResponseEntity<String> body = requestService.requestPdfMindmap(path, documentId, keywords); | ||
emitter.send(SseEmitter.event().id(String.valueOf(documentId)).name("sse").data(body.getBody())); | ||
} else if (whiteboardService.isAudioType(documentId)) { | ||
String text = lectureNoteService.getText(documentId); | ||
ResponseEntity<String> body = requestService.requestSTTMindmap(text, documentId, keywords); | ||
emitter.send(SseEmitter.event().id(String.valueOf(documentId)).name("sse").data(body.getBody())); | ||
} | ||
} catch (IOException exception) { | ||
emitterRepository.deleteById(documentId); | ||
emitter.completeWithError(exception); | ||
} | ||
} | ||
} | ||
|
||
private SseEmitter createEmitter(Long documentId) { | ||
SseEmitter emitter = new SseEmitter(DEFAULT_TIMEOUT); | ||
emitterRepository.save(documentId, emitter); | ||
|
||
// Emitter가 완료될 때(모든 데이터가 성공적으로 전송된 상태) Emitter를 삭제한다. | ||
emitter.onCompletion(() -> emitterRepository.deleteById(documentId)); | ||
// Emitter가 타임아웃 되었을 때(지정된 시간동안 어떠한 이벤트도 전송되지 않았을 때) Emitter를 삭제한다. | ||
emitter.onTimeout(() -> emitterRepository.deleteById(documentId)); | ||
|
||
return emitter; | ||
} | ||
} |