同时进行 WebClient 调用并仅获得第一个完成的
Make simultaneous WebClient calls and get only the first completed
我是 webflux 的新手,我想做以下事情:
我想用不同的参数值对相同的 url 发出并行的 http 请求,并在我得到第一个非空(非异常)结果时停止。
我正在按照这里的示例 https://www.baeldung.com/spring-webclient-simultaneous-calls
但我不知道当我得到结果时如何停止。有谁能够帮助我?
目前我有这样的东西:
RetrySpec retrySpec = Retry.max(3);
return webClient.get().uri("/getMyObject/{id}", id)
.retrieve()
.bodyToMono(MyObject.class)
.retryWhen(retrySpec);
}
public Flux<> getMyObjs(List<String> ids) {
return Flux.fromIterable(ids)
.parallel(Runtime.getRuntime().availableProcessors())
.runOn()
.flatMap(this::getMyObject)
.;//// Stop when I get first non exceptional value
}
试试 Flux 中的 next() 运算符。
public Mono<MyObject> getMyObjs(List<String> ids) {
return Flux.fromIterable(ids)
.parallel(Runtime.getRuntime().availableProcessors())
.runOn()
.flatMap(this::getMyObject)
.next();// Emit only the first item emitted by this Flux, into a new Mono. If called on an empty Flux, emits an empty Mono.
}
参考:https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#next--
但是,还要检查 firstWithSignal 和 firstWithValue 运算符。
https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#firstWithSignal-java.lang.Iterable-
https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#firstWithValue-java.lang.Iterable-
当我遇到这样的问题时,通常我会查看文档以从 Flux API.
中找到合适的运算符
我是 webflux 的新手,我想做以下事情: 我想用不同的参数值对相同的 url 发出并行的 http 请求,并在我得到第一个非空(非异常)结果时停止。 我正在按照这里的示例 https://www.baeldung.com/spring-webclient-simultaneous-calls 但我不知道当我得到结果时如何停止。有谁能够帮助我? 目前我有这样的东西:
RetrySpec retrySpec = Retry.max(3);
return webClient.get().uri("/getMyObject/{id}", id)
.retrieve()
.bodyToMono(MyObject.class)
.retryWhen(retrySpec);
}
public Flux<> getMyObjs(List<String> ids) {
return Flux.fromIterable(ids)
.parallel(Runtime.getRuntime().availableProcessors())
.runOn()
.flatMap(this::getMyObject)
.;//// Stop when I get first non exceptional value
}
试试 Flux 中的 next() 运算符。
public Mono<MyObject> getMyObjs(List<String> ids) {
return Flux.fromIterable(ids)
.parallel(Runtime.getRuntime().availableProcessors())
.runOn()
.flatMap(this::getMyObject)
.next();// Emit only the first item emitted by this Flux, into a new Mono. If called on an empty Flux, emits an empty Mono.
}
参考:https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#next--
但是,还要检查 firstWithSignal 和 firstWithValue 运算符。 https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#firstWithSignal-java.lang.Iterable- https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#firstWithValue-java.lang.Iterable-
当我遇到这样的问题时,通常我会查看文档以从 Flux API.
中找到合适的运算符