diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/ReactiveEventClient.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/ReactiveEventClient.java new file mode 100644 index 0000000000..6e421d7f5f --- /dev/null +++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/ReactiveEventClient.java @@ -0,0 +1,29 @@ +package com.baeldung.reactive; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Flux; + +@SpringBootApplication +public class ReactiveEventClient implements CommandLineRunner { + public static void main(String[] args) { + System.setProperty("server.port","8090"); + SpringApplication.run(ReactiveEventClient.class, args); + } + + @Override + public void run(String... args) throws Exception { + WebClient webclient = WebClient.builder() + .defaultHeader(HttpHeaders.ACCEPT, MediaType.TEXT_EVENT_STREAM_VALUE) + .baseUrl("http://localhost:8080/events") + .build(); + + webclient.get().uri("/stream/").header(HttpHeaders.CONTENT_TYPE,MediaType.TEXT_HTML.toString()) + .retrieve().bodyToFlux(String.class) + .subscribe(System.out::println); + } +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/ReactiveEventServer.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/ReactiveEventServer.java new file mode 100644 index 0000000000..2efde58c80 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/ReactiveEventServer.java @@ -0,0 +1,21 @@ +package com.baeldung.reactive.controller; + + +import org.springframework.http.MediaType; +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; + +import java.time.Duration; + + +@RestController +@RequestMapping("/events") +public class ReactiveEventServer { + + @GetMapping(path = "/stream/" , produces=MediaType.TEXT_EVENT_STREAM_VALUE) + public Flux eventStream() { + return Flux.interval(Duration.ofSeconds(1)).map(tick -> {return "Event:" + tick;}); + } +} \ No newline at end of file