diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/client/StockClient.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/client/StockClient.java new file mode 100644 index 0000000000..6c37c6b763 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/client/StockClient.java @@ -0,0 +1,24 @@ +package com.baeldung.reactive.client; + +import org.springframework.http.MediaType; +import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec; + +import com.baeldung.reactive.model.Stock; + +public class StockClient { + + public void getStockUpdates(String stockCode) { + WebClient client = WebClient.create("localhost:9111"); + RequestHeadersSpec request = client.get().uri("/rtes/stocks/"+stockCode).accept(MediaType.TEXT_EVENT_STREAM); + request.retrieve().bodyToFlux(Stock.class).toStream().forEach(System.out::println); + } + + public static void main(String[] args) throws InterruptedException { + new StockClient().getStockUpdates("GOOGL"); + + while(true) { + Thread.sleep(1000L); + } + } +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/StockReactiveController.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/StockReactiveController.java new file mode 100644 index 0000000000..4015d660b9 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/StockReactiveController.java @@ -0,0 +1,38 @@ +package com.baeldung.reactive.controller; + +import java.math.BigDecimal; +import java.time.Duration; +import java.util.Random; +import java.util.stream.Stream; + +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.reactive.model.Stock; + +import reactor.core.publisher.Flux; +import reactor.util.function.Tuple2; + +@RestController +@RequestMapping("/rtes") +public class StockReactiveController { + + @GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE, value = "/stocks/{code}") + public Flux getStocks(@PathVariable String code) { + BigDecimal startingPrice = new BigDecimal("100"); + Flux stockFlux = Flux.fromStream(Stream.generate(() -> new Stock( + code, + new Random().nextBoolean() ? + startingPrice.add(BigDecimal.valueOf(new Random().nextDouble())): + startingPrice.subtract(BigDecimal.valueOf(new Random().nextDouble()))))); + Flux emmitFlux = Flux.interval(Duration.ofSeconds(1)); + return Flux.zip(stockFlux, emmitFlux).map(Tuple2::getT1); + } + + public static void main(String [] args) { + + } +}