From d6903ce2ea0319f65fbfa10833ab343277c121cb Mon Sep 17 00:00:00 2001 From: "eko.zhan" Date: Wed, 11 Dec 2024 15:21:44 +0800 Subject: [PATCH] feat(sse): sse samples --- kbase-stack-sse/pom.xml | 28 ++++++++++++++++ .../com/ibothub/heap/sse/SseApplication.java | 20 +++++++++++ .../heap/sse/controller/HelloController.java | 33 +++++++++++++++++++ .../src/main/resources/application.properties | 1 + pom.xml | 1 + 5 files changed, 83 insertions(+) create mode 100644 kbase-stack-sse/pom.xml create mode 100644 kbase-stack-sse/src/main/java/com/ibothub/heap/sse/SseApplication.java create mode 100644 kbase-stack-sse/src/main/java/com/ibothub/heap/sse/controller/HelloController.java create mode 100644 kbase-stack-sse/src/main/resources/application.properties diff --git a/kbase-stack-sse/pom.xml b/kbase-stack-sse/pom.xml new file mode 100644 index 0000000..88f89d2 --- /dev/null +++ b/kbase-stack-sse/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + com.eastrobot + kbase-heap + 0.0.1-SNAPSHOT + + + kbase-stack-sse + + + 8 + 8 + UTF-8 + + + + + + org.springframework.boot + spring-boot-starter-webflux + + + + \ No newline at end of file diff --git a/kbase-stack-sse/src/main/java/com/ibothub/heap/sse/SseApplication.java b/kbase-stack-sse/src/main/java/com/ibothub/heap/sse/SseApplication.java new file mode 100644 index 0000000..6f49870 --- /dev/null +++ b/kbase-stack-sse/src/main/java/com/ibothub/heap/sse/SseApplication.java @@ -0,0 +1,20 @@ +/* + * powered by https://ekozhan.com + */ +package com.ibothub.heap.sse; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author eko.zhan + * @version v1.0 + * @since 2024/12/11 13:59 + */ +@SpringBootApplication +public class SseApplication { + + public static void main(String[] args) { + SpringApplication.run(SseApplication.class, args); + } +} diff --git a/kbase-stack-sse/src/main/java/com/ibothub/heap/sse/controller/HelloController.java b/kbase-stack-sse/src/main/java/com/ibothub/heap/sse/controller/HelloController.java new file mode 100644 index 0000000..7df69f1 --- /dev/null +++ b/kbase-stack-sse/src/main/java/com/ibothub/heap/sse/controller/HelloController.java @@ -0,0 +1,33 @@ +/* + * powered by https://ekozhan.com + */ +package com.ibothub.heap.sse.controller; + +import java.time.Duration; +import java.time.LocalDateTime; +import org.springframework.http.codec.ServerSentEvent; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Flux; + +/** + * @author eko.zhan + * @version v1.0 + * @since 2024/12/11 14:08 + */ +@RestController +@RequestMapping("/api/v1/sse") +public class HelloController { + + @GetMapping("/events") + public Flux> getEvents() { + return Flux.interval(Duration.ofSeconds(1)) + .map(sequence -> ServerSentEvent.builder() + .id(String.valueOf(sequence)) + .event("message") + .data("Event #" + sequence + " at " + LocalDateTime.now()) + .build()); + } + +} diff --git a/kbase-stack-sse/src/main/resources/application.properties b/kbase-stack-sse/src/main/resources/application.properties new file mode 100644 index 0000000..eb9db1a --- /dev/null +++ b/kbase-stack-sse/src/main/resources/application.properties @@ -0,0 +1 @@ +logging.level.root=debug \ No newline at end of file diff --git a/pom.xml b/pom.xml index 7a2b455..071cbff 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,7 @@ kbase-stack-xxl kbase-stack-screw kbase-stack-database + kbase-stack-sse pom