project-reactor

WebFlux call `bodyToMono()` twice throws `IllegalStateException`

二次信任 提交于 2019-12-23 04:41:54
问题 I'm currently working on creating a logging ExchangeFilterFunction that can be used for any WebFlux WebClient to log both requests and responses. The issue I am running into is that when I attempt to log the body of the response I get the following: java.lang.IllegalStateException: Only one connection receive subscriber allowed. I understand why it's happening as Spring only allows one subscriber to the stream. My question is how can I obtain the body in two decoupled places? I have tried

rxJava buffer() with time that honours backpressure

流过昼夜 提交于 2019-12-23 03:21:26
问题 The versions of buffer operator that don't operate on time honour backpressure as per JavaDoc: http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html#buffer-int- However, any version of buffer that involves time based buffers doesn't support backpressure, like this one http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html#buffer-long-java.util.concurrent.TimeUnit-int- I understand this comes from the fact that once the time is ticking, you can't stop it similarly to,

What's the point of .switchIfEmpty() getting evaluated eagerly?

半世苍凉 提交于 2019-12-23 02:09:12
问题 Even if my stream is not empty, the fallback stream would always be created? What's the intent behind doing this? This is extremely non-idiomatic. On the other hand, . onErrorResume is evaluated lazily. Could someone please explain to me why . switchIsEmpty is evaluated eagerly? Here's the code: public static void main(String[] args) { Mono<Integer> m = Mono.just(1); m.flatMap(a -> Mono.delay(Duration.ofMillis(5000)).flatMap(p -> Mono.empty())) .switchIfEmpty(getFallback()) .doOnNext(a ->

How to set and handle timeout in Spring WebClient?

孤者浪人 提交于 2019-12-23 00:49:19
问题 Spring docs says it is required to configure http client for WebClient manually to set timeouts: https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-client-builder-reactor-timeout. But since WebClient returns reactive Mono, it's possible (api-wise) to apply .timeout method. Does it have the same effect? Moreover, when one uses .timeout method, Reactor's TimeoutException is expected. Will the same error appear in the stream if configuration is done

Should I rely on “mono of item” or “plain item” arguments when composing reactive chains?

自作多情 提交于 2019-12-22 17:24:21
问题 I am have two versions of a Webflux/Reactor handler class. This class mimics a user sign up use case - as far as the business logic is concerned. The first version of the class relies upon a Mono<User> whereas the second version uses a plain User . First version of the class : this is the version relying on a Mono<User> argument down the chain. Notice the top level public method createUser uses a userMono . @Component @RequiredArgsConstructor public class UserHandler { private final @NonNull

Should I rely on “mono of item” or “plain item” arguments when composing reactive chains?

ぐ巨炮叔叔 提交于 2019-12-22 17:22:56
问题 I am have two versions of a Webflux/Reactor handler class. This class mimics a user sign up use case - as far as the business logic is concerned. The first version of the class relies upon a Mono<User> whereas the second version uses a plain User . First version of the class : this is the version relying on a Mono<User> argument down the chain. Notice the top level public method createUser uses a userMono . @Component @RequiredArgsConstructor public class UserHandler { private final @NonNull

How to limit the number of active Spring WebClient calls

試著忘記壹切 提交于 2019-12-22 09:45:57
问题 I have a requirement where I read a bunch of rows (thousands) from a SQL DB using Spring Batch and call a REST Service to enrich content before writing them on a Kafka topic. When using the Spring Reactive webClient, how do I limit the number of active non-blocking service calls? Should I somehow introduce a Flux in the loop after I read data using Spring Batch? (I understand the usage of delayElements and that it serves a different purpose, as when a single Get Service Call brings in lot of

Exception Handling Reactive

梦想与她 提交于 2019-12-22 08:24:22
问题 How does one use a Mono.error(<Throwable>) but attach information from the body returned from a request? Is there a reactive object that extends Throwable that takes a Mono/Flux object, so the error being thrown will wait for the body to be accounted for? Or is there a way to add some sort of 'flag' onto an existing Mono object to make it fail instantly (to circumvent the requirement to be Throwable ) Example scenario below: import org.springframework.web.reactive.function.client.WebClient;

how to wait for all requests to complete with Spring 5 WebClient?

◇◆丶佛笑我妖孽 提交于 2019-12-22 08:24:19
问题 I have a simple Java program that sends multiple requests with Spring WebClient. Each returns a mono, and I am using response.subscribe() to check the result. However, my main thread of execution finishes before all requests are processed, unless I add a long Thread.sleep(). With CompletableFutures you can use: CompletableFuture.allOf(futures).join(); Is there a way to wait for all Mono's to complete ? 回答1: As explained in the Project Reactor documentation, nothing happens until you subscribe

How to limit request payload size using Spring Webflux?

时光毁灭记忆、已成空白 提交于 2019-12-22 00:17:37
问题 Using Spring Webflux, I need to limit the payload size of incoming HTTP requests (e.g. to say 5MB). Is this something configurable out-of-the-box? If not, which APIs can we use to implement this behavior (e.g. return a 413 if the payload is too big). 回答1: I guess you want to do this to prevent Denial Of Service attacks by malicious or misbehaving clients. The usual recommendation is to not directly connect HTTP application servers to the Internet, but instead via an HTTP reverse proxy server