File tree Expand file tree Collapse file tree
spring-5-reactive/src/main/java/com/baeldung/reactive Expand file tree Collapse file tree Original file line number Diff line number Diff line change 66
77import com .baeldung .reactive .model .Stock ;
88
9- public class StockClient {
10-
11- public void getStockUpdates (String stockCode ) {
12- WebClient client = WebClient .create ("localhost:9111" );
13- RequestHeadersSpec <?> request = client .get ().uri ("/rtes/stocks/" +stockCode ).accept (MediaType .TEXT_EVENT_STREAM );
14- request .retrieve ().bodyToFlux (Stock .class ).toStream ().forEach (System .out ::println );
15- }
9+ import reactor .core .publisher .Flux ;
1610
17- public static void main (String [] args ) throws InterruptedException {
18- new StockClient ().getStockUpdates ("GOOGL" );
11+ public class StockClient {
1912
20- while (true ) {
21- Thread .sleep (1000L );
22- }
23- }
13+ public Flux <Stock > getStockUpdates (String stockCode ) {
14+ WebClient client = WebClient .create ("localhost:8080" );
15+ RequestHeadersSpec <?> request = client .get ()
16+ .uri ("/rtes/stocks/" + stockCode )
17+ .accept (MediaType .TEXT_EVENT_STREAM );
18+ return request .retrieve ()
19+ .bodyToFlux (Stock .class )
20+ .log ();
21+ }
2422}
Original file line number Diff line number Diff line change @@ -23,16 +23,14 @@ public class StockReactiveController {
2323 @ GetMapping (produces = MediaType .TEXT_EVENT_STREAM_VALUE , value = "/stocks/{code}" )
2424 public Flux <Stock > getStocks (@ PathVariable String code ) {
2525 BigDecimal startingPrice = new BigDecimal ("100" );
26- Flux <Stock > stockFlux = Flux .fromStream (Stream .generate (() -> new Stock (
27- code ,
28- new Random ().nextBoolean () ?
29- startingPrice .add (BigDecimal .valueOf (new Random ().nextDouble ())):
30- startingPrice .subtract (BigDecimal .valueOf (new Random ().nextDouble ())))));
26+ Flux <Stock > stockFlux = Flux .fromStream (Stream .generate (() -> new Stock (code , getLatestPrice (startingPrice ))));
3127 Flux <Long > emmitFlux = Flux .interval (Duration .ofSeconds (1 ));
32- return Flux .zip (stockFlux , emmitFlux ).map (Tuple2 ::getT1 );
28+ return Flux .zip (stockFlux , emmitFlux )
29+ .map (Tuple2 ::getT1 );
3330 }
3431
35- public static void main (String [] args ) {
36-
32+ private BigDecimal getLatestPrice (BigDecimal startingPrice ) {
33+ BigDecimal priceChange = BigDecimal .valueOf (new Random ().nextDouble ());
34+ return new Random ().nextBoolean () ? startingPrice .add (priceChange ) : startingPrice .subtract (priceChange );
3735 }
3836}
You can’t perform that action at this time.
0 commit comments