有没有办法等待 webflux 代码中的异步方法结果
is there a way to wait a async method result in webflux code
我使用 Spring webflux 来开发 Intellij idea,现在我遇到一个问题是在我的方法中,我需要从 reactive mongo 获取一个 ip(String),比我会转发我的请求。
所以我写了这段代码
@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();
}
我的问题是这段代码将在其他线程上执行,我无法在我的方法中获取此 ip,
因为我的方法不会 Waiting for this mongo execution
String ip = repository.findBySymbol(symbol)
.map(xxxxx)
.subscribe(builder::append)
.toString();
那么有什么方法可以让我在我的方法中立即获取 ip 吗?
你的构造是一个非常肮脏的 hack,不要那样做 并尽量避免反应流中的任何副作用操作。
所以,你只需要像这样链接你的操作员:
return repository.findBySymbol(symbol)
.map(xxxxx)
.map(ip -> webClient.method(httpMethod).uri(ip))
...
flatMap(param -> forwardRequest.exchange())
我使用 Spring webflux 来开发 Intellij idea,现在我遇到一个问题是在我的方法中,我需要从 reactive mongo 获取一个 ip(String),比我会转发我的请求。
所以我写了这段代码
@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();
}
我的问题是这段代码将在其他线程上执行,我无法在我的方法中获取此 ip, 因为我的方法不会 Waiting for this mongo execution
String ip = repository.findBySymbol(symbol)
.map(xxxxx)
.subscribe(builder::append)
.toString();
那么有什么方法可以让我在我的方法中立即获取 ip 吗?
你的构造是一个非常肮脏的 hack,不要那样做 并尽量避免反应流中的任何副作用操作。
所以,你只需要像这样链接你的操作员:
return repository.findBySymbol(symbol)
.map(xxxxx)
.map(ip -> webClient.method(httpMethod).uri(ip))
...
flatMap(param -> forwardRequest.exchange())