Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在阻止Spring MVC应用程序中进行并发WebClient调用?_Java_Spring_Spring Webflux_Project Reactor_Spring Webclient - Fatal编程技术网

Java 如何在阻止Spring MVC应用程序中进行并发WebClient调用?

Java 如何在阻止Spring MVC应用程序中进行并发WebClient调用?,java,spring,spring-webflux,project-reactor,spring-webclient,Java,Spring,Spring Webflux,Project Reactor,Spring Webclient,我正在尝试对5个不同的后端系统进行并行RESTAPI调用。每个RESTAPI都有不同的端点和不同的响应类型。我试图通过使用Webclient来实现这一点。我不知道如何“阻止”每个api调用同时发出的Mono 这是我的示例代码 Mono<ClientResponse> userDetails = getUserDetails().subscribeOn(Schedulers.parallel()); Mono<ClientResponse> userAccountDetai

我正在尝试对5个不同的后端系统进行并行RESTAPI调用。每个RESTAPI都有不同的端点和不同的响应类型。我试图通过使用Webclient来实现这一点。我不知道如何“阻止”每个api调用同时发出的Mono

这是我的示例代码

Mono<ClientResponse> userDetails = getUserDetails().subscribeOn(Schedulers.parallel());
Mono<ClientResponse> userAccountDetails = getUserAccountDetails().subscribeOn(Schedulers.parallel());

//getUserDetails and getUserAccountDetails does an exchange() and not retrieve() as i need access to the headers.

Tuple2<ClientResponse, ClientResponse> tuple = Mono.zip(userDetails, userAccountDetails).block();

tuple.getT1().bodyToMono(String.class).block()
tuple.getT2().bodyToMono(String.class).block()
Mono userDetails=getUserDetails().subscribeOn(Schedulers.parallel());
Mono userAccountDetails=getUserAccountDetails().subscribeOn(Schedulers.parallel());
//getUserDetails和getUserAccountDetails执行exchange()而不是retrieve(),因为我需要访问标题。
Tuple2 tuple=Mono.zip(userDetails,userAccountDetails.block();
tuple.getT1()
tuple.getT2()

这种方法的问题是,即使我压缩了ClientResponse,我仍然必须为每个项目调用一个块。

您只需将
bodyToMono
方法调用移动到第一行和第二行:

Mono-userDetails=getUserDetails().flatMap(r->r.bodytomino(String.class));
Mono userAccountDetails=getUserAccountDetails().flatMap(r->r.bodytomino(String.class));
Tuple2 tuple=Mono.zip(userDetails,userAccountDetails.block();
字符串userDetailsResponse=tuple.getT1();
字符串userAccountDetailsResponse=tuple.getT2();

另外,请注意,我删除了subscribeOn(Schedulers.parallel())操作符。当您在后台使用
WebClient
时,不需要显式调度来实现并发性,您可以直接使用它。

如果您使用block,您将失去反应式编程的好处。更好的办法是,你认为你需要对收到的响应做什么,并异步处理这些响应。@VikasSachdeva该应用程序不是一个真正的反应式应用程序。我需要调用5个不同的RESTAPI。我想利用webclient使这些调用异步并获得性能提升。在进入下一阶段之前,我需要所有的回答。