Mono switchIfEmpty() is always called

It’s because switchIfEmpty accepts Mono “by value”. Meaning that even before you subscribe to your mono, this alternative mono’s evaluation is already triggered. Imagine a method like this: Mono<String> asyncAlternative() { return Mono.fromFuture(CompletableFuture.supplyAsync(() -> { System.out.println(“Hi there”); return “Alternative”; })); } If you define your code like this: Mono<String> result = Mono.just(“Some payload”).switchIfEmpty(asyncAlternative()); It’ll always … Read more

How to call back async function from Rx Subscribe?

Ana Betts’ answer works in most scenarios, but if you want to block the stream while waiting for the async function to finish you need something like this: Observable.Interval(TimeSpan.FromSeconds(1)) .Select(l => Observable.FromAsync(asyncMethod)) .Concat() .Subscribe(); Or: Observable.Interval(TimeSpan.FromSeconds(1)) .Select(_ => Observable.Defer(() => asyncMethod().ToObservable())) .Concat() .Subscribe();

Can I use SpringMvc and webflux together?

As explained in the Spring Boot reference documentation, Spring Boot will auto-configure a Spring MVC application if both MVC and WebFlux are available. There are several reasons for this: Spring MVC can’t run on Netty both infrastructure will compete for the same job (for example, serving static resources, the mappings, etc) mixing both runtime models … Read more

Combine a list of Observables and wait until all completed

You can use flatMap in case you have dynamic tasks composition. Something like this: public Observable<Boolean> whenAll(List<Observable<Boolean>> tasks) { return Observable.from(tasks) //execute in parallel .flatMap(task -> task.observeOn(Schedulers.computation())) //wait, until all task are executed //be aware, all your observable should emit onComplete event //otherwise you will wait forever .toList() //could implement more intelligent logic. eg. check … Read more

How to customize SpringWebFlux WebClient JSON deserialization?

Here’s an example that customizes the ObjectMapper for JSON (de)serialization. Note that for streaming purposes, different encoders/decoders are being used but the principle remains the same for their configuration. ExchangeStrategies strategies = ExchangeStrategies .builder() .codecs(clientDefaultCodecsConfigurer -> { clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(new ObjectMapper(), MediaType.APPLICATION_JSON)); clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(new ObjectMapper(), MediaType.APPLICATION_JSON)); }).build(); WebClient webClient = WebClient.builder().exchangeStrategies(strategies).build();

How to mock Spring WebFlux WebClient?

We accomplished this by providing a custom ExchangeFunction that simply returns the response we want to the WebClientBuilder: webClient = WebClient.builder() .exchangeFunction(clientRequest -> Mono.just(ClientResponse.create(HttpStatus.OK) .header(“content-type”, “application/json”) .body(“{ \”key\” : \”value\”}”) .build()) ).build(); myHttpService = new MyHttpService(webClient); Map<String, String> result = myHttpService.callService().block(); // Do assertions here If we want to use Mokcito to verify if the … Read more