英文:
Convert traditional loop of invoking weclient into non blocking way
问题
我对响应式编程还不熟悉,我想将以下代码转换为非阻塞方式。
为了简单起见,我基于我的原始代码创建了一个示例伪代码。任何帮助将不胜感激。
public Mono<Response> getResponse(List<Provider> providers) {
return Flux.fromIterable(providers)
.flatMap(provider -> provider.invokeHttpCall()
.filter(response -> response.getMessage().equals("Success"))
.switchIfEmpty(Mono.defer(() -> Mono.empty())))
.next();
}
provider.invokeHttpCall()
方法
@Override
public Mono<Response> invokeHttpCall(){
WebClient webClient = WebClient.create();
return webClient.post()
.uri("/provider").accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(Response.class);
}
我尝试了几种策略来实现这个,但仍然没有成功。要么会调用所有的提供者,要么我需要阻塞 Web 客户端线程。
英文:
I am new to reactive programming and I want to transform the following code into non blocking way.
For the sake of simplicity, I created a sample pseudo code based from my original code. Any help will be appreciated.
public Mono<Response> getResponse(List<Provider> providers) {
for (Provider provider : providers) {
Response response = provider.invokeHttpCall().block();
if(response.getMessage() == "Success") {
return Mono.just(response);
}
continue;
}
return Mono.empty();
}
provider.invokeHttpCall()
method
@Override
public Mono<Response> invokeHttpCall(){
WebClient webClient = WebClient.create();
return webClient.post()
.uri("/provider").accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(Response.class);
}
I tried several tactics to implement this, but still no luck. Either all providers are invoked or I need to block the webclient thread.
答案1
得分: 0
reactive 是一种流(Stream)类型。请将其视为流(Stream)并以响应式方式编程。
我为您提供以下代码:
- 首先,使用
Flux.fromIterable()
从列表创建一个 Flux 流。 - 接下来,使用
flatmap()
和 Lambda 函数将invoke
发送到另一个新线程。 - 使用
filterWhen()
方法和 Lambda 表达式来获取 "Success" 响应并仅获取第一个 "Success" 元素。请参阅 filterwhen API 文档。 - 最后,只需使用
Mono.from()
包装Flux
,然后返回Mono
类型。
public Mono<Response> getResponse(List<Provider> providers) {
return Mono.from(Flux.fromIterable(providers)
.flatMap(provider ->
Mono.defer(() -> provider.invokeHttpCall())
.filterWhen(response -> response.getMessage().equals("Success")));
}
如果您想要查看结果并使用 println()
打印出来,请使用 .subscribe()
方法来执行它。
getResponse.subscribe(System.out::println);
英文:
reactive is a kind of Stream. Please think it as a Stream and program it reactively.
I give you such followed code.
- Firstly, use
Flux.fromIterable()
to create a flux stream from a List. - Next, use
flatmap()
and Lambda fuction to emit theinvoke
into another new thread. - use method
filterWhen()
and Lambda to get the "Success" response and just get the first "Success" elements. See filterwhen api Doc. - Finally, just use
Mono.from()
to wrap theFlux
and then return theMono
type.
public Mono<Response> getResponse(List<Provider> providers) {
return Mono.from(Flux.fromIterable(providers)
.flatmap(provider ->
Mono.defer(() -> provider.invokeHttpCall())
.filterWhen(response -> response.getMessage() == "Success");
}
if you want to see result and println()
.
Just use .subsribe()
method to excute it.
getResponse.subsribe(System.out::println);
答案2
得分: 0
Flux.fromIterable(providers)
.concatMap(Provider::invokeHttpCall) // 确保按顺序调用providers
.filter(response -> response.getMessage().equals("Success"))
.next()
英文:
Flux.fromIterable(providers)
.concatMap(Provider::invokeHttpCall) // ensures providers are called sequentially
.filter(response -> response.getMessage().equals("Success"))
.next()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论