Merge branch 'master' of https://github.com/priyeshmashelkar/tutorials
This commit is contained in:
commit
8e3fdf0657
|
@ -1,22 +0,0 @@
|
|||
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;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
public class StockClient {
|
||||
|
||||
public Flux<Stock> getStockUpdates(String stockCode) {
|
||||
WebClient client = WebClient.create("localhost:8080");
|
||||
RequestHeadersSpec<?> request = client.get()
|
||||
.uri("/rtes/stocks/" + stockCode)
|
||||
.accept(MediaType.TEXT_EVENT_STREAM);
|
||||
return request.retrieve()
|
||||
.bodyToFlux(Stock.class)
|
||||
.log();
|
||||
}
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
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<Stock> getStocks(@PathVariable String code) {
|
||||
BigDecimal startingPrice = new BigDecimal("100");
|
||||
Flux<Stock> stockFlux = Flux.fromStream(Stream.generate(() -> new Stock(code, getLatestPrice(startingPrice))));
|
||||
Flux<Long> emmitFlux = Flux.interval(Duration.ofSeconds(1));
|
||||
return Flux.zip(stockFlux, emmitFlux)
|
||||
.map(Tuple2::getT1);
|
||||
}
|
||||
|
||||
private BigDecimal getLatestPrice(BigDecimal startingPrice) {
|
||||
BigDecimal priceChange = BigDecimal.valueOf(new Random().nextDouble());
|
||||
return new Random().nextBoolean() ? startingPrice.add(priceChange) : startingPrice.subtract(priceChange);
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
package com.baeldung.reactive.model;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class Stock {
|
||||
|
||||
private String code;
|
||||
|
||||
private BigDecimal price;
|
||||
|
||||
public Stock(String code, BigDecimal price) {
|
||||
this.code = code;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public BigDecimal getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(BigDecimal price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue