Skip to content

Commit

Permalink
Merge pull request #74 from SWM-SMART/dev
Browse files Browse the repository at this point in the history
id null 수정 및 30초 주기 SSE 알림
  • Loading branch information
noparamin authored Nov 21, 2023
2 parents 7f45a69 + de9d06e commit 5faa9a7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/smart/watchboard/WatchboardApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long> id = jwtService.extractUserId(accessToken);
String extractedAccessToken = jwtService.extractAccessToken(accessToken);
Optional<Long> id = jwtService.extractUserId(extractedAccessToken);
Long userId = id.orElse(null);

// s3에 오디오 파일 저장
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long> id = jwtService.extractUserId(accessToken);
String extractedAccessToken = jwtService.extractAccessToken(accessToken);
Optional<Long> id = jwtService.extractUserId(extractedAccessToken);
Long userId = id.orElse(null);

if (!checkPageLimit(countPdfPage(pdfFile))) {
Expand All @@ -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<KeywordsBodyDto> responseEntity = requestService.requestPdfKeywords(path);
// keywordService.createKeywords(responseEntity, documentId);
//
// ResponseEntity<SummaryDto> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,4 +25,9 @@ public void deleteById(Long id) {
public SseEmitter get(Long id) {
return emitters.get(id);
}

public List<Long> getAllKeys() {
return emitters.keySet().stream()
.collect(Collectors.toList());
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/smart/watchboard/service/SseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ public void notify(Long documentId, List<String> keywords) {
sendToClient(documentId, keywords);
}

public void notifyCycle() {
sendCycle();
}

private void sendCycle() {
List<Long> 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) {
Expand Down

0 comments on commit 5faa9a7

Please sign in to comment.