如何压缩 8 种以上不同类型的单声道
How to zip more than 8 different types of mono
我正在尝试同时拨打 9 api 电话。所有这些调用都会 return 不同的响应对象。在此之前我们有 8 个 api 调用,因为这些单声道是不同的类型,所以我使用 Mono.zip 如下所示。
Mono<ResponseEntity<Service1Response>> monoService1 = callService1();
Mono<ResponseEntity<Service2Response>> monoService2 = callService2();
...
Mono<ResponseEntity<Service3Response>> monoService7 = callService7();
Mono<ResponseEntity<Service4Response>> monoService8 = callService8();
MixResponse mix = Mono.zip(monoService1, monoService2, monoService3, monoService4, monoService5, monoService6, monoService7, monoService8).flatMap(response -> {
MixResponse mixResp = new MixResponse();
mixResp.setResponse1(response.getT1().getBody());
mixResp.setResponse2(response.getT2().getBody());
mixResp.setResponse3(response.getT3().getBody());
mixResp.setResponse4(response.getT4().getBody());
mixResp.setResponse5(response.getT5().getBody());
mixResp.setResponse6(response.getT6().getBody());
mixResp.setResponse7(response.getT7().getBody());
mixResp.setResponse8(response.getT8().getBody());
return Mono.just(mixResp);
})).block();
但现在我们又多了一项服务,Mono.zip 最多只支持 8 个单声道。除了 Mono.zip 之外,还有什么我可以在我的情况下使用的吗?对不起,如果这个问题看起来很愚蠢。我是 spring-webflux 的新手。提前致谢。
您可以使用 Mono.zip
将 Iterable
作为参数和组合器函数来组合结果并转换为特定类型:
<R> Mono<R> zip(final Iterable<? extends Mono<?>> monos, Function<? super Object[], ? extends R> combinator)
List<Mono<? extends ResponseEntity<?>>> requests = List.of(
monoService1,
monoService2,
monoService3,
monoService4
);
Mono.zip(requests, responses -> {
MixResponse mixResp = new MixResponse();
mixResp.setResponse1(((ResponseEntity<Service1Response>) responses[0]).getBody());
mixResp.setResponse2(((ResponseEntity<Service2Response>) responses[1]).getBody());
mixResp.setResponse3(((ResponseEntity<Service3Response>) responses[2]).getBody());
mixResp.setResponse4(((ResponseEntity<Service4Response>) responses[3]).getBody());
return mixResp;
});
我正在尝试同时拨打 9 api 电话。所有这些调用都会 return 不同的响应对象。在此之前我们有 8 个 api 调用,因为这些单声道是不同的类型,所以我使用 Mono.zip 如下所示。
Mono<ResponseEntity<Service1Response>> monoService1 = callService1();
Mono<ResponseEntity<Service2Response>> monoService2 = callService2();
...
Mono<ResponseEntity<Service3Response>> monoService7 = callService7();
Mono<ResponseEntity<Service4Response>> monoService8 = callService8();
MixResponse mix = Mono.zip(monoService1, monoService2, monoService3, monoService4, monoService5, monoService6, monoService7, monoService8).flatMap(response -> {
MixResponse mixResp = new MixResponse();
mixResp.setResponse1(response.getT1().getBody());
mixResp.setResponse2(response.getT2().getBody());
mixResp.setResponse3(response.getT3().getBody());
mixResp.setResponse4(response.getT4().getBody());
mixResp.setResponse5(response.getT5().getBody());
mixResp.setResponse6(response.getT6().getBody());
mixResp.setResponse7(response.getT7().getBody());
mixResp.setResponse8(response.getT8().getBody());
return Mono.just(mixResp);
})).block();
但现在我们又多了一项服务,Mono.zip 最多只支持 8 个单声道。除了 Mono.zip 之外,还有什么我可以在我的情况下使用的吗?对不起,如果这个问题看起来很愚蠢。我是 spring-webflux 的新手。提前致谢。
您可以使用 Mono.zip
将 Iterable
作为参数和组合器函数来组合结果并转换为特定类型:
<R> Mono<R> zip(final Iterable<? extends Mono<?>> monos, Function<? super Object[], ? extends R> combinator)
List<Mono<? extends ResponseEntity<?>>> requests = List.of(
monoService1,
monoService2,
monoService3,
monoService4
);
Mono.zip(requests, responses -> {
MixResponse mixResp = new MixResponse();
mixResp.setResponse1(((ResponseEntity<Service1Response>) responses[0]).getBody());
mixResp.setResponse2(((ResponseEntity<Service2Response>) responses[1]).getBody());
mixResp.setResponse3(((ResponseEntity<Service3Response>) responses[2]).getBody());
mixResp.setResponse4(((ResponseEntity<Service4Response>) responses[3]).getBody());
return mixResp;
});