diff --git a/src/main/java/com/smart/watchboard/WatchboardApplication.java b/src/main/java/com/smart/watchboard/WatchboardApplication.java index 8eae708..9f027da 100644 --- a/src/main/java/com/smart/watchboard/WatchboardApplication.java +++ b/src/main/java/com/smart/watchboard/WatchboardApplication.java @@ -2,7 +2,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; + @SpringBootApplication +@EnableScheduling public class WatchboardApplication { public static void main(String[] args) { diff --git a/src/main/java/com/smart/watchboard/common/support/ScheduledTask.java b/src/main/java/com/smart/watchboard/common/support/ScheduledTask.java new file mode 100644 index 0000000..2dd5c78 --- /dev/null +++ b/src/main/java/com/smart/watchboard/common/support/ScheduledTask.java @@ -0,0 +1,17 @@ +package com.smart.watchboard.common.support; + +import com.smart.watchboard.service.SseService; +import lombok.RequiredArgsConstructor; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class ScheduledTask { + private final SseService sseService; + + @Scheduled(fixedRate = 30000) + public void executeNotifyTask() { + sseService.notifyCycle(); + } +} diff --git a/src/main/java/com/smart/watchboard/controller/AudioFileController.java b/src/main/java/com/smart/watchboard/controller/AudioFileController.java index 24efb22..d5fadb9 100644 --- a/src/main/java/com/smart/watchboard/controller/AudioFileController.java +++ b/src/main/java/com/smart/watchboard/controller/AudioFileController.java @@ -47,7 +47,8 @@ public class AudioFileController { @PostMapping("/{documentID}/audio") public ResponseEntity uploadAudioFile(@PathVariable(value = "documentID") long documentId, @RequestParam("audio") MultipartFile audioFile, @RequestHeader("Authorization") String accessToken) throws UnsupportedAudioFileException, IOException, DocumentException { - Optional id = jwtService.extractUserId(accessToken); + String extractedAccessToken = jwtService.extractAccessToken(accessToken); + Optional id = jwtService.extractUserId(extractedAccessToken); Long userId = id.orElse(null); // s3에 오디오 파일 저장 diff --git a/src/main/java/com/smart/watchboard/controller/LearningFileController.java b/src/main/java/com/smart/watchboard/controller/LearningFileController.java index 65b5de8..2052393 100644 --- a/src/main/java/com/smart/watchboard/controller/LearningFileController.java +++ b/src/main/java/com/smart/watchboard/controller/LearningFileController.java @@ -38,7 +38,8 @@ public class LearningFileController { @PostMapping("/{documentID}/pdf") public ResponseEntity uploadLearningFile(@PathVariable(value = "documentID") long documentId, @RequestParam("pdf") MultipartFile pdfFile, @RequestHeader("Authorization") String accessToken) throws UnsupportedAudioFileException, IOException { - Optional id = jwtService.extractUserId(accessToken); + String extractedAccessToken = jwtService.extractAccessToken(accessToken); + Optional id = jwtService.extractUserId(extractedAccessToken); Long userId = id.orElse(null); if (!checkPageLimit(countPdfPage(pdfFile))) { @@ -47,16 +48,9 @@ public ResponseEntity uploadLearningFile(@PathVariable(value = "documentID") S3Dto s3Dto = new S3Dto(pdfFile, documentId, userId, "pdf"); String path = awsS3Uploader.uploadFile(s3Dto); - //sseService.notifyKeywords(documentId, path); - //sseService.notifySummary(documentId, path); -// ResponseEntity responseEntity = requestService.requestPdfKeywords(path); -// keywordService.createKeywords(responseEntity, documentId); -// -// ResponseEntity summary = requestService.requestPdfSummary(path); -// summaryService.createSummary(documentId, summary.getBody().getSummary()); + whiteboardService.setDataType(documentId, "pdf"); - //return new ResponseEntity<>(responseEntity.getBody(), HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK); } diff --git a/src/main/java/com/smart/watchboard/repository/EmitterRepository.java b/src/main/java/com/smart/watchboard/repository/EmitterRepository.java index c98bffd..1b2c26f 100644 --- a/src/main/java/com/smart/watchboard/repository/EmitterRepository.java +++ b/src/main/java/com/smart/watchboard/repository/EmitterRepository.java @@ -4,8 +4,10 @@ import org.springframework.stereotype.Repository; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; +import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; @Repository @RequiredArgsConstructor @@ -23,4 +25,9 @@ public void deleteById(Long id) { public SseEmitter get(Long id) { return emitters.get(id); } + + public List getAllKeys() { + return emitters.keySet().stream() + .collect(Collectors.toList()); + } } diff --git a/src/main/java/com/smart/watchboard/service/SseService.java b/src/main/java/com/smart/watchboard/service/SseService.java index 94cd721..adf383c 100644 --- a/src/main/java/com/smart/watchboard/service/SseService.java +++ b/src/main/java/com/smart/watchboard/service/SseService.java @@ -51,6 +51,24 @@ public void notify(Long documentId, List keywords) { sendToClient(documentId, keywords); } + public void notifyCycle() { + sendCycle(); + } + + private void sendCycle() { + List keys = emitterRepository.getAllKeys(); + for (Long documentId : keys) { + SseEmitter emitter = emitterRepository.get(documentId); + try { + emitter.send(SseEmitter.event().id(String.valueOf(documentId)).name("ping").data("ping")); + } catch (IOException exception) { + emitterRepository.deleteById(documentId); + emitter.completeWithError(exception); + } + + } + } + private void sendToClientFirst(Long documentId, Object data) { SseEmitter emitter = emitterRepository.get(documentId); if (emitter != null) {