英文:
How do I wait for multiple Mono's to complete at once and get the value
问题
类似于 https://stackoverflow.com/questions/49396692/waiting-for-running-reactor-mono-instances-to-complete 的问题,但我希望在另一个 Mono 中获得结果。以下是我的代码。我尝试了使用 materialize 的方法,但效果不佳。
```java
@GetMapping("/bounced")
public Mono<Map<String, Object>> bounced(
@RequestHeader("X-B3-Traceid") String traceId,
@RequestHeader(HttpHeaders.AUTHORIZATION) String authorization
) {
final Mono<Map<String, Object>> sample = webClient.get()
.uri("http://sample:8080/")
.header(HttpHeaders.AUTHORIZATION, authorization)
.retrieve()
.bodyToMono(new ParameterizedTypeReference<>() {
});
final Mono<Map<String, Object>> httpGet = webClient.get()
.uri("http://httpbin.org/get")
.retrieve()
.bodyToMono(new ParameterizedTypeReference<>() {
});
final Mono<Map<String, Object>> anything = webClient.get()
.uri("http://httpbin.org/anything/foo")
.retrieve()
.bodyToMono(new ParameterizedTypeReference<>() {
});
/*
尝试了下面的方法,它会启动执行,但会在返回块中触发另一个“download”。
Mono.when(anything, sample, httpGet)
.subscribe();
.materialize()
.block();
*/
return Mono.just(Map.of("traceFromBounced", traceId,
"anything", anything.block(),
"sample", sample.block(),
"httpGet", httpGet.block()));
英文:
Similar in question to https://stackoverflow.com/questions/49396692/waiting-for-running-reactor-mono-instances-to-complete but I want to get the result ideally in another Mono. Here's the code I have. I tried the materialize solution but that didn't pan out.
@GetMapping("/bounced")
public Mono<Map<String, Object>> bounced(
@RequestHeader("X-B3-Traceid") String traceId,
@RequestHeader(HttpHeaders.AUTHORIZATION) String authorization
) {
final Mono<Map<String, Object>> sample = webClient.get()
.uri("http://sample:8080/")
.header(HttpHeaders.AUTHORIZATION, authorization)
.retrieve()
.bodyToMono(new ParameterizedTypeReference<>() {
});
final Mono<Map<String, Object>> httpGet = webClient.get()
.uri("http://httpbin.org/get")
.retrieve()
.bodyToMono(new ParameterizedTypeReference<>() {
});
final Mono<Map<String, Object>> anything = webClient.get()
.uri("http://httpbin.org/anything/foo")
.retrieve()
.bodyToMono(new ParameterizedTypeReference<>() {
});
/*
Tried this and it does start it up, but it triggers another "download" in the return block.
Mono.when(anything, sample, httpGet)
.subscribe();
.materialize()
.block();
*/
return Mono.just(Map.of("traceFromBounced", traceId,
"anything", anything.block(),
"sample", sample.block(),
"httpGet", httpGet.block()));
答案1
得分: 5
根据 @K.Nicholas 的评论,我让它正常运行了:
@GetMapping("/bounced")
public Mono<Map<String, Object>> bounced(
@RequestHeader("X-B3-Traceid") String traceId,
@RequestHeader(HttpHeaders.AUTHORIZATION) String authorization
) {
final Mono<Map<String, Object>> sample = webClient.get()
.uri("http://sample:8080/")
.header(HttpHeaders.AUTHORIZATION, authorization)
.retrieve()
.bodyToMono(new ParameterizedTypeReference<>() {
});
final Mono<Map<String, Object>> httpGet = webClient.get()
.uri("http://httpbin.org/get")
.retrieve()
.bodyToMono(new ParameterizedTypeReference<>() {
});
final Mono<Map<String, Object>> anything = webClient.get()
.uri("http://httpbin.org/anything/foo")
.retrieve()
.bodyToMono(new ParameterizedTypeReference<>() {
});
return Mono.zip(anything, sample, httpGet)
.map(t -> Map.of("traceFromBounced", traceId,
"anything", t.getT1(),
"sample", t.getT2(),
"httpGet", t.getT3()));
}
英文:
Based on @K.Nicholas' comment I got it working
@GetMapping("/bounced")
public Mono<Map<String, Object>> bounced(
@RequestHeader("X-B3-Traceid") String traceId,
@RequestHeader(HttpHeaders.AUTHORIZATION) String authorization
) {
final Mono<Map<String, Object>> sample = webClient.get()
.uri("http://sample:8080/")
.header(HttpHeaders.AUTHORIZATION, authorization)
.retrieve()
.bodyToMono(new ParameterizedTypeReference<>() {
});
final Mono<Map<String, Object>> httpGet = webClient.get()
.uri("http://httpbin.org/get")
.retrieve()
.bodyToMono(new ParameterizedTypeReference<>() {
});
final Mono<Map<String, Object>> anything = webClient.get()
.uri("http://httpbin.org/anything/foo")
.retrieve()
.bodyToMono(new ParameterizedTypeReference<>() {
});
return Mono.zip(anything, sample, httpGet)
.map(t -> Map.of("traceFromBounced", traceId,
"anything", t.getT1(),
"sample", t.getT2(),
"httpGet", t.getT3()));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论