project-reactor

Mono vs Flux in Reactive Stream

六眼飞鱼酱① 提交于 2019-11-30 00:42:40
As per the documentation: Flux is a stream which can emit 0..N elements: Flux<String> fl = Flux.just("a", "b", "c"); Mono is a stream of 0..1 elements: Mono<String> mn = Mono.just("hello"); And as both are the implementations of the Publisher interface in the reactive stream. Can't we use only Flux in most of the cases as it also can emit 0..1, thus satisfying the conditions of a Mono? Or there are some specific conditions when only Mono needs to be used and Flux can not handle the operations? Please suggest. In many cases, you are doing some computation or calling a service and you expect

Spring 5 Web Reactive - How can we use WebClient to retrieve streamed data in a Flux?

旧时模样 提交于 2019-11-29 15:16:17
The current milestone (M4) documentation shows and example about how to retrieve a Mono using WebClient : WebClient webClient = WebClient.create(new ReactorClientHttpConnector()); ClientRequest<Void> request = ClientRequest.GET("http://example.com/accounts/{id}", 1L) .accept(MediaType.APPLICATION_JSON).build(); Mono<Account> account = this.webClient .exchange(request) .then(response -> response.body(toMono(Account.class))); How can we get streamed data (from a service that returns text/event-stream ) into a Flux using WebClient? Does it support automatic Jackson conversion?. This is how I did

Is it possible to start Mono's in parallel and aggregate the result

前提是你 提交于 2019-11-29 09:29:57
问题 I know it is possible to chain Mono's, for ex,... Mono<String> resultAMono = loadA(); Mono<String> resultBMono = resultA.flatMap(resultA -> loadB()); This will chain and resultBMono will run when resultAMono returns.... So my question is, is it possible to start 2 Mono's in parallel and when both returns continue with another Mono? I think it will look something like this... Mono<String> resultAMono = loadA(); Mono<String> resuktBMono = loadB(); Mono<Tuple2<Stirng, String> tupleMono = Mono

what does Mono.defer() do?

帅比萌擦擦* 提交于 2019-11-29 08:02:17
I've come across Mono.defer() in some Spring webflux code I looked up the method in the docs but don't understand the explanation: "Create a Mono provider that will supply a target Mono to subscribe to for each Subscriber downstream" please could I have an explanation and an example. Is there a place with a bunch of Reactor example code (their unit tests?) that I might reference. thanks It is a bit of an oversimplification but conceptually Reactor sources are either lazy or eager. More advanced ones, like an HTTP request, are expected to be lazily evaluated. On the other side the most simple

ParallelFlux vs flatMap() for a Blocking I/O task

若如初见. 提交于 2019-11-29 07:17:12
问题 I have a Project Reactor chain which includes a blocking task (a network call, we need to wait for response). I'd like to run multiple blocking tasks concurrently. It seems like either ParallelFlux or flatMap() could be used, bare-bone examples: Flux.just(1) .repeat(10) .parallel(3) .runOn(Schedulers.elastic()) .doOnNext(i -> blockingTask()) .sequential() .subscribe() or Flux.just(1) .repeat(10) .flatMap(i -> Mono.fromCallable(() -> {blockingTask(); return i;}).subscribeOn(Schedulers.elastic(

How to set event-loop pool size in Spring Webflux / WebClient?

一个人想着一个人 提交于 2019-11-29 02:52:25
问题 In multi-reactor framework such as Vert.X we can set the number of event-loop threads, e.g.: final VertxOptions vertxOptions = new VertxOptions(); vertxOptions.setEventLoopPoolSize(16); final Vertx myVertx = Vertx.vertx(vertxOptions); How to do the equivalent in Spring Boot 2 WebFlux / WebClient? 回答1: You have two options: Override ReactiveWebServerFactory bean with a customizer that applies event loop resources config: @Bean public ReactiveWebServerFactory reactiveWebServerFactory() {

Mono vs Flux in Reactive Stream

那年仲夏 提交于 2019-11-28 19:50:34
问题 As per the documentation: Flux is a stream which can emit 0..N elements: Flux<String> fl = Flux.just("a", "b", "c"); Mono is a stream of 0..1 elements: Mono<String> mn = Mono.just("hello"); And as both are the implementations of the Publisher interface in the reactive stream. Can't we use only Flux in most of the cases as it also can emit 0..1, thus satisfying the conditions of a Mono? Or there are some specific conditions when only Mono needs to be used and Flux can not handle the operations

Issue with Project Reactor's or() operator usage

别说谁变了你拦得住时间么 提交于 2019-11-28 11:20:54
问题 I would like to chain Mono s and emit the first non-empty of them. I thought the or() operator was designed for this purpose. Here is my chain of Mono s: first one is empty and second one should emit "hello". @Test void orTest() { Mono<String> chain = Mono.<String>empty().or(Mono.just("hello")); StepVerifier.create( chain ) .expectNext("hello") .verifyComplete(); } However, I get the following failure: java.lang.AssertionError: expectation "expectNext(hello)" failed (expected: onNext(hello);

block()/blockFirst()/blockLast() are blocking error when calling bodyToMono AFTER exchange()

僤鯓⒐⒋嵵緔 提交于 2019-11-28 05:02:56
问题 I am trying to use Webflux to stream a generated file to another location, however, if the generation of the file ran into an error, the api returns success, but with a DTO detailing the errors while generating the file instead of the file itself. This is using a very old and poorly designed api so please excuse the use of post and the api design. The response from the api call (exchange()) is a ClientResponse. From here I can either convert to a ByteArrayResource using bodyToMono which can

How to convert Mono<List<String>> into Flux<String>

坚强是说给别人听的谎言 提交于 2019-11-28 02:36:13
问题 I'm converting small project written in RxJava 1.x to Reactor 3.x. All is good, except that I could not find out how to replace flatMap(Observable::from) with appropriate counterpart. I have Mono<List<String>> and I need to convert it to Flux<String> . Thanks 回答1: In Reactor 3, the from operator has been specialized into a few variants, depending on the original source (array, iterable, etc...). Use yourMono.flatMapMany(Flux::fromIterable) in your case. 回答2: I think that probably Flux: