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/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) {