|
| 1 | +package com.baeldung.reactive.serversentevents.consumer.controller; |
| 2 | + |
| 3 | +import java.time.LocalTime; |
| 4 | + |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | +import org.springframework.core.ParameterizedTypeReference; |
| 8 | +import org.springframework.http.MediaType; |
| 9 | +import org.springframework.http.codec.ServerSentEvent; |
| 10 | +import org.springframework.scheduling.annotation.Async; |
| 11 | +import org.springframework.web.bind.annotation.GetMapping; |
| 12 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 13 | +import org.springframework.web.bind.annotation.RestController; |
| 14 | +import org.springframework.web.reactive.function.client.WebClient; |
| 15 | + |
| 16 | +import reactor.core.publisher.Flux; |
| 17 | +import reactor.core.publisher.Mono; |
| 18 | + |
| 19 | +@RestController |
| 20 | +@RequestMapping("/sse-consumer") |
| 21 | +public class ClientController { |
| 22 | + |
| 23 | + private static Logger logger = LoggerFactory.getLogger(ClientController.class); |
| 24 | + private WebClient client = WebClient.create("http://localhost:8081/sse-server"); |
| 25 | + |
| 26 | + @GetMapping("/launch-sse-client") |
| 27 | + public String launchSSEFromSSEWebClient() { |
| 28 | + consumeSSE(); |
| 29 | + return "LAUNCHED EVENT CLIENT!!! Check the logs..."; |
| 30 | + } |
| 31 | + |
| 32 | + @GetMapping("/launch-flux-client") |
| 33 | + public String launchcFluxFromSSEWebClient() { |
| 34 | + consumeFlux(); |
| 35 | + return "LAUNCHED EVENT CLIENT!!! Check the logs..."; |
| 36 | + } |
| 37 | + |
| 38 | + @GetMapping("/launch-sse-from-flux-endpoint-client") |
| 39 | + public String launchFluxFromFluxWebClient() { |
| 40 | + consumeSSEFromFluxEndpoint(); |
| 41 | + return "LAUNCHED EVENT CLIENT!!! Check the logs..."; |
| 42 | + } |
| 43 | + |
| 44 | + @Async |
| 45 | + public void consumeSSE() { |
| 46 | + ParameterizedTypeReference<ServerSentEvent<String>> type = new ParameterizedTypeReference<ServerSentEvent<String>>() { |
| 47 | + }; |
| 48 | + |
| 49 | + Flux<ServerSentEvent<String>> eventStream = client.get() |
| 50 | + .uri("/stream-sse") |
| 51 | + .retrieve() |
| 52 | + .bodyToFlux(type); |
| 53 | + |
| 54 | + eventStream.subscribe(content -> logger.info("Current time: {} - Received SSE: name[{}], id [{}], content[{}] ", LocalTime.now(), content.event(), content.id(), content.data()), error -> logger.error("Error receiving SSE: {}", error), |
| 55 | + () -> logger.info("Completed!!!")); |
| 56 | + } |
| 57 | + |
| 58 | + @Async |
| 59 | + public void consumeFlux() { |
| 60 | + Flux<String> stringStream = client.get() |
| 61 | + .uri("/stream-flux") |
| 62 | + .accept(MediaType.TEXT_EVENT_STREAM) |
| 63 | + .retrieve() |
| 64 | + .bodyToFlux(String.class); |
| 65 | + |
| 66 | + stringStream.subscribe(content -> logger.info("Current time: {} - Received content: {} ", LocalTime.now(), content), error -> logger.error("Error retrieving content: {}", error), () -> logger.info("Completed!!!")); |
| 67 | + } |
| 68 | + |
| 69 | + @Async |
| 70 | + public void consumeSSEFromFluxEndpoint() { |
| 71 | + ParameterizedTypeReference<ServerSentEvent<String>> type = new ParameterizedTypeReference<ServerSentEvent<String>>() { |
| 72 | + }; |
| 73 | + |
| 74 | + Flux<ServerSentEvent<String>> eventStream = client.get() |
| 75 | + .uri("/stream-flux") |
| 76 | + .accept(MediaType.TEXT_EVENT_STREAM) |
| 77 | + .retrieve() |
| 78 | + .bodyToFlux(type); |
| 79 | + |
| 80 | + eventStream.subscribe(content -> logger.info("Current time: {} - Received SSE: name[{}], id [{}], content[{}] ", LocalTime.now(), content.event(), content.id(), content.data()), error -> logger.error("Error receiving SSE: {}", error), |
| 81 | + () -> logger.info("Completed!!!")); |
| 82 | + } |
| 83 | +} |
0 commit comments