您好,我想知道如何使用 react 堆项目的“zipwhen”功能,因为在文档中不清楚。
我的问题是,接下来我要对Rest/Web服务进行一系列调用,问题是,由于我现在拥有它,所以我总是调用所有服务,但是我想对其进行参数化,以便在不调用时不总是调用所有服务。取决于使用zipwhen的需求。
我真的不知道是否可以使用zipwhen。
就像是 :
我有一个类服务,在其中我可以调用不同的Web服务(因为这是一个示例,因此始终相同,但实际上它将用于n个不同的Web服务)
@Service
public class FuenteRiesgoService {
@Autowired
SmartBearWsClient smartBearWsClient;
public Mono<GetCurrentTimeResponse> fuenteWS1() {
return Mono.fromCallable(() -> {
Random rn = new Random();
System.out.println("Llamando a fuente WS1 ..... on " + Thread.currentThread().getName());
Thread.sleep(rn.nextInt(50 - 1 + 1) + 1 * 10000);
GetCurrentTimeResponse getCurrentTimeResponse = smartBearWsClient.getCurrentTime(new GetCurrentTime());
System.out.println("Respuesta fuente WS1 " + getCurrentTimeResponse.getGetCurrentTimeResult() + " on "
+ Thread.currentThread().getName());
return getCurrentTimeResponse;
}).subscribeOn(Schedulers.parallel());
}
public Mono<GetCurrentTimeResponse> fuenteWS2(boolean call) {
return Mono.fromCallable(() -> {
Random rn = new Random();
System.out.println("Llamando a fuente WS2 ..... on " + Thread.currentThread().getName());
Thread.sleep(rn.nextInt(50 - 1 + 1) + 1 * 10000);
GetCurrentTimeResponse getCurrentTimeResponse = smartBearWsClient.getCurrentTime(new GetCurrentTime());
System.out.println("Respuesta fuente WS2 " + getCurrentTimeResponse.getGetCurrentTimeResult() + " on "
+ Thread.currentThread().getName());
return getCurrentTimeResponse;
}).subscribeOn(Schedulers.parallel());
}
public Mono<GetCurrentTimeResponse> fuenteWS3() {
return Mono.fromCallable(() -> {
Random rn = new Random();
System.out.println("Llamando a fuente WS3 ..... on " + Thread.currentThread().getName());
Thread.sleep(rn.nextInt(50 - 1 + 1) + 1 * 10000);
GetCurrentTimeResponse getCurrentTimeResponse = smartBearWsClient.getCurrentTime(new GetCurrentTime());
System.out.println("Respuesta fuente WS3 " + getCurrentTimeResponse.getGetCurrentTimeResult() + " on "
+ Thread.currentThread().getName());
return getCurrentTimeResponse;
}).subscribeOn(Schedulers.parallel());
}
public Mono<GetCurrentTimeResponse> fuenteWS4() {
return Mono.fromCallable(() -> {
Random rn = new Random();
System.out.println("Llamando a fuente WS4 ..... on " + Thread.currentThread().getName());
Thread.sleep(rn.nextInt(50 - 1 + 1) + 1 * 10000);
GetCurrentTimeResponse getCurrentTimeResponse = smartBearWsClient.getCurrentTime(new GetCurrentTime());
System.out.println("Respuesta fuente WS4 " + getCurrentTimeResponse.getGetCurrentTimeResult() + " on "
+ Thread.currentThread().getName());
return getCurrentTimeResponse;
}).subscribeOn(Schedulers.parallel());
}
}
通过此返回,它的作用是始终调用4个Web服务。
return fuenteRiesgoService.fuenteWS1()
.zipWith(fuenteRiesgoService.fuenteWS2(), (a, b) -> {
return " - WS1 : " + a.getGetCurrentTimeResult().toString() + " - WS2 : "
+ b.getGetCurrentTimeResult().toString();
}).zipWith(fuenteRiesgoService.fuenteWS3(), (a, b) -> {
return a + " - WS3 : " + b.getGetCurrentTimeResult().toString();
}).zipWith(fuenteRiesgoService.fuenteWS4(), (a, b) -> {
System.out.println("Fuentes obtenidas " + a + " - WS4 : " + b.getGetCurrentTimeResult().toString()
+ " on " + Thread.currentThread().getName());
return a + " - WS4 : " + b.getGetCurrentTimeResult().toString();
});
但是我想做的是能够不通过zipwhen参数调用自己和其他人。
例如,调用ws2和ws4而不是ws3,因此它对ws3不起任何作用
boolean callws2 = true;
boolean callws3 = false;
boolean callws4 = true;
return fuenteRiesgoService.fuenteWS1()
.zipWhen(if(callws2 = true) -> fuenteRiesgoService.fuenteWS2(), (a, b) -> {
return " - WS1 : " + a.getGetCurrentTimeResult().toString() + " - WS2 : "
+ b.getGetCurrentTimeResult().toString();
}).zipWhen(if(callws3 = true) -> fuenteRiesgoService.fuenteWS3(), (a, b) -> {
return a + " - WS3 : " + b.getGetCurrentTimeResult().toString();
}).zipWhen(if(callws4 = true) -> fuenteRiesgoService.fuenteWS4(), (a, b) -> {
System.out.println("Fuentes obtenidas " + a + " - WS4 : " + b.getGetCurrentTimeResult().toString()
+ " on " + Thread.currentThread().getName());
return a + " - WS4 : " + b.getGetCurrentTimeResult().toString();
});
最佳答案
Project Reactor提供了许多运算符(operator),有时很难弄清楚哪个是最佳解决方案-功能性, react 灵敏的思维方式与此息息相关。zipWhen
是组合结果的特定运算符,但与zipWith
不同,您需要先查看第一个结果,然后才能进行第二个调用。让我们在这里举个例子:准备一个假期。您需要先预订酒店,然后才能预订出租车去那里。
Mono<HotelBooking> first = bookHotel(startDate, EndDate);
// you need the chosen hotel location before you can book a taxi,
// but you still need both bookings in the end
Mono<Tuple2<HotelBooking, TaxiBooking>> result = first
.zipWhen(booking -> bookTaxi(airport, booking.getLocation());
看来您的示例不需要您从中间结果中获取信息。如果逻辑是同步且非阻塞的,则常规应用程序逻辑应该是首选方式。
Mono<String> result = fuenteRiesgoService.fuenteWS1();
if (callws2) {
result = result.zipWith(fuenteRiesgoService.fuenteWS2(), combinatorFunction);
}
if (callws3) {
result = result.zipWith(fuenteRiesgoService.fuenteWS3(), combinatorFunction);
}
return result;
关于java - 如何在条件中使用 "zipWhen"?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48803609/