project-reactor

Proper way to create a Flux from a list of Mono's

有些话、适合烂在心里 提交于 2020-05-29 13:06:31
问题 Lets say I have a API operation that consumes a List of CustomObjects. For every one of those objects it calls a service method that creates a Mono. How do I create a Flux from those Mono objects in an idiomatic and therefore non-blocking way? What I've come up with for now is this. I changed the method names to better reflect their intended purpose. fun myApiMethod(@RequestBody customObjs: List<CustomObject>): Flux<CustomObject> { return Flux.create { sink -> customObjs.forEach { service

Proper way to create a Flux from a list of Mono's

匆匆过客 提交于 2020-05-29 13:06:23
问题 Lets say I have a API operation that consumes a List of CustomObjects. For every one of those objects it calls a service method that creates a Mono. How do I create a Flux from those Mono objects in an idiomatic and therefore non-blocking way? What I've come up with for now is this. I changed the method names to better reflect their intended purpose. fun myApiMethod(@RequestBody customObjs: List<CustomObject>): Flux<CustomObject> { return Flux.create { sink -> customObjs.forEach { service

publishOn vs subscribeOn in Project Reactor 3

久未见 提交于 2020-05-25 08:00:31
问题 I am using publishOn vs subscribeOn both on the same flux as follows: System.out.println("*********Calling Concurrency************"); List<Integer> elements = new ArrayList<>(); Flux.just(1, 2, 3, 4) .map(i -> i * 2) .log() .publishOn(Schedulers.elastic()) .subscribeOn(Schedulers.parallel()) .subscribe(elements::add); System.out.println("-------------------------------------"); Although, when i use both, nothing is printed in logs. But when i use only publishOn, i got the following info logs:

How REST endpoints are auto subscribed while calling from Browser/REST Client?

限于喜欢 提交于 2020-05-23 09:21:40
问题 In ProjectReactor or Reactive Streams, Nothing Happens Until You subscribe(). Reactive streams data flow will not happen unless until someone subscribe to it, but I see for all REST APIs like finds, save and inserts are not calling subscribe explicitly but data is flowing between producer and subscribers. @RestController class PersonController { private final PersonRepository repository; public PersonController(PersonRepository repository) { this.repository = repository; } @GetMapping("/all")

How to join tables in r2dbc?

◇◆丶佛笑我妖孽 提交于 2020-05-15 08:47:05
问题 In java reactor, r2dbc. I have two tables A, B. I also have repositories for them defined. How can i get data made up of A join B? I only come up with the following approach: call databaseClient.select from A and consequently in a loop call select from B. But i want more efficient and reactive way. How to do it? 回答1: TL;DR: Using SQL. Spring Data's DatabaseClient is an improved and reactive variant for R2DBC of what JdbcTemplate is for JDBC. It encapsulates various execution modes, resource

Can I use SpringMvc and webflux together?

天大地大妈咪最大 提交于 2020-05-09 19:38:43
问题 I would like to use 2 approaches(reactive and standard) in one project. I tried to migrate one REST API endpoint to reactive webflux and test performance before migrate rest of them. But it didn't work. I added router and handler for him, but until I didn't remove spring-boot-starter-web from dependencies and disable @RestController I got http 404 code all the time. Is it possible or not? Or should I migrate all project to reactive approach? 回答1: As explained in the Spring Boot reference

List directory content with Project Reactor and DirectoryStream

五迷三道 提交于 2020-05-09 14:11:10
问题 I'd like to use DirectoryStream with Project Reactor to list all the files in a directory. My try is: Path myDir = Paths.get("C:\\Users\\r.dacanal\\Documents\\Reply\\EDA\\logging-consumer\\input"); DirectoryStream<Path> directoryStream = Files.newDirectoryStream(myDir); Flux.fromIterable(directoryStream).doOnNext(s -> System.out.println(s)).subscribe(); But I'm getting the following Exception: Caused by: java.lang.IllegalStateException: Iterator already obtained at sun.nio.fs

What is the difference between Schedulers.newElastic and Schedulers.elastic methods?

允我心安 提交于 2020-04-13 06:15:43
问题 I am working on Flux and Mono and using them in multi threaded environment and using the Schedular which provide the worker thread. There are many options to start the Schedular using elastic, parallel and newElastic. Here is the code which i used: System.out.println("------ elastic --------- "); Flux.range(1, 10) .map(i -> i / 2) .publishOn(Schedulers.elastic()).log() .blockLast(); System.out.println("------ new elastic --------- "); Flux.range(1, 10) .map(i -> i / 2).log() .publishOn

What is the best way to validate request in a Spring Webflux functional application

本小妞迷上赌 提交于 2020-01-30 04:51:52
问题 In a traditional web application it is easy to validate the request body in the controller method, eg. ResponseEntity create(@Valid @ResponseBody Post post) { } If it is a MVC application, we can gather the errors by injecting a BindingResult , and decide if there is some validation errors from the input form. In the pages, there are some helpers existed for Freemarker and Thymeleaf to display the messages. But when I come to Webflux and try to use RouterFunction to define the routing in the

Kotlin to achieve multithread request hedging?

依然范特西╮ 提交于 2020-01-24 18:55:47
问题 Spring's reactor has an interesting feature : Hedging . It means spawning many requests and get the first returned result , and automatically clean other contexts. Josh Long recently has been actively promoting this feature. Googling Spring reactor hedging shows relative results. If anybody is curious , here is the sample code . In short , Flux.first() simplifies all the underlaying hassles , which is very impressive. I wonder how this can be achieved with Kotlin's coroutine and multithread ,