is there a way to wait a async method result in webflux code

0ggmr0 :

I use Spring webflux to develop with Intellij idea, and now I meet a problem is that in my method, I need to get a ip(String) from reactive mongo, than i will forward my request。
so i wrote this code

@Autowird
private XXRepository repository;

public Mono<Void> xxxx(ServerWebExchange exchange, String symbol) {
    StringBuilder builder = new StringBuilder();
    String ip = repository.findBySymbol(symbol)
                          .map(xxxxx)
                          .subscribe(builder::append)
                          .toString();
    WebClient.RequestBodySpec forwardRequestInfo = webClient.method(httpMethod)
                .uri(ip);

    xxxxxxx //setting http msg
    WebClient.RequestHeadersSpec<?> forwardRequest;
    return forwardRequest.exchange();
}

my question is that this code will execute on other thread, i can not get this ip in my method, because my method will not Waiting for this mongo execution

String ip = repository.findBySymbol(symbol)
                          .map(xxxxx)
                          .subscribe(builder::append)
                          .toString();

so is there any way i can get ip immediately in my method ?

Yauhen Balykin :

your construction is a very dirty hack, don't do that and try to avoid any side effect operations in the reactive streams.
So, you just need to chain your operators like this:

return repository.findBySymbol(symbol)
                      .map(xxxxx)
                      .map(ip -> webClient.method(httpMethod).uri(ip))
                      ...
                      flatMap(param -> forwardRequest.exchange())

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=378620&siteId=1