Added for eval article

This commit is contained in:
priyeshmashelkar 2018-05-23 18:44:15 +01:00
parent 7bba73c6eb
commit d5177c158e
2 changed files with 62 additions and 0 deletions

View File

@ -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);
}
}
}

View File

@ -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<Stock> getStocks(@PathVariable String code) {
BigDecimal startingPrice = new BigDecimal("100");
Flux<Stock> 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<Long> emmitFlux = Flux.interval(Duration.ofSeconds(1));
return Flux.zip(stockFlux, emmitFlux).map(Tuple2::getT1);
}
public static void main(String [] args) {
}
}