Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get String from Mono<String> in reactive java

I have a method which accepts Mono as a param. All I want is to get the actual String from it. Googled but didn't find answer except calling block() over Mono object but it will make a blocking call so want to avoid using block(). Please suggest other way if possible. The reason why I need this String is because inside this method I need to call another method say print() with the actual String value. I understand this is easy but I am new to reactive programming.

Code:

public String getValue(Mono<String> monoString) {
    // How to get actual String from param monoString
    // and call print(String) method
}

public void print(String str) {
    System.out.println(str);
}
like image 634
nanosoft Avatar asked Nov 08 '17 12:11

nanosoft


People also ask

How do you get String out of mono?

Blocking way to get String from Mono<String> You can use the block() method to block the current thread indefinitely and wait for Mono to complete. If the Mono completes, then this method either returns the original value or null (Mono is empty). In case of any errors, then the exception is rethrown.

How do I get a list from Mono list?

Using block() is actually the only way to get the object from a Mono when it is emitted.

What is the difference between flux and Mono?

A Flux object represents a reactive sequence of 0.. N items, while a Mono object represents a single-value-or-empty (0..1) result. This distinction carries a bit of semantic information into the type, indicating the rough cardinality of the asynchronous processing.

Is Mono subscribe blocking?

As you know, Mono is an asynchronous call that executes in a non-blocking way.


6 Answers

Getting a String from a Mono<String> without a blocking call isn't easy, it's impossible. By definition. If the String isn't available yet (which Mono<String> allows), you can't get it except by waiting until it comes in and that's exactly what blocking is.

Instead of "getting a String" you subscribe to the Mono and the Subscriber you pass will get the String when it becomes available (maybe immediately). E.g.

myMono.subscribe(
  value -> System.out.println(value), 
  error -> error.printStackTrace(), 
  () -> System.out.println("completed without a value")
)

will print the value or error produced by myMono (type of value is String, type of error is Throwable). At https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html you can see other variants of subscribe too.

like image 153
Alexey Romanov Avatar answered Oct 17 '22 21:10

Alexey Romanov


According to the doc you can do:

String getValue(Mono<String> mono) {
    return mono.block();
}

be aware of the blocking call

like image 34
ΦXocę 웃 Пepeúpa ツ Avatar answered Oct 17 '22 22:10

ΦXocę 웃 Пepeúpa ツ


Finally what worked for me is calling flatMap method like below:

public void getValue(Mono<String> monoString)
{
   monoString.flatMap(this::print);
}
like image 43
nanosoft Avatar answered Oct 17 '22 21:10

nanosoft


What worked for me was the following:

monoString.subscribe(this::print);

like image 38
AjCodez Avatar answered Oct 17 '22 20:10

AjCodez


Simplest answer is:

 String returnVal = mono.block();
like image 3
KayV Avatar answered Oct 17 '22 20:10

KayV


Better

monoUser.map(User::getId) 
like image 1
Armen Arzumanyan Avatar answered Oct 17 '22 22:10

Armen Arzumanyan