如何使用“zipWhen”有条件?

时间:2018-02-15 09:17:28

标签: java spring project-reactor spring-webflux

您好我想知道如何使用reactor项目的“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();
});

1 个答案:

答案 0 :(得分:2)

Project Reactor提供了许多操作员,有时候很难找出哪个是最佳解决方案 - 功能性,反应性思维与此有很大关系。

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;