What is then, thenEmpty, thenMany and flatMapMany in spring webflux?

flatMap vs flatMapMany In functional programming, flatMap returns the same type than the type that bear the method, so for Mono<T>, flatMap returns a Mono. Which means that only one element can be emitted by the inner Publisher (or that it is truncated). We enforced that by having Mono#flatMap take a Function<T, Mono<R>>. As a … Read more

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

First, a few things that will help you understand the code snippet solving this use case. You should never call a blocking method within a method that returns a reactive type; you will block one of the few threads of your application and it is very bad for the application Anyway as of Reactor 3.2, … Read more

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

What kind of “EventBus” to use in Spring? Built-in, Reactor, Akka?

I’m not sure I can adequately answer your question in this small space. But I’ll give it a shot! 🙂 Spring’s ApplicationEvent system and Reactor are really quite distinct as far as functionality goes. ApplicationEvent routing is based on the type handled by the ApplicationListener. Anything more complicated than that and you’ll have to implement … Read more

Reactive Programming Advantages/Disadvantages

Well, Reactive Programming means you are doing all your IO bound tasks such as network calls asynchronously. For an instance say your application calls an external REST API or a database, you can do that invocation asynchronously. If you do so your current thread does not block. You can serve lots of requests by merely … Read more

How to correctly read Flux and convert it to a single inputStream

This is really not as complicated as other answers imply. The only way to stream the data without buffering it all in memory is to use a pipe, as @jin-kwon suggested. However, it can be done very simply by using Spring’s BodyExtractors and DataBufferUtils utility classes. Example: private InputStream readAsInputStream(String url) throws IOException { PipedOutputStream … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)