2

I have multiple Monos that are required for a task as shown below

Mono<A> aMono = aService.getMonoA();
Mono<B> bMono = bService.getMonoB();
Mono<C> cMono = cService.getMonoC();
Mono<Task> task = taskService.executeTask(b, c);

Now my method has following structure

Working code without zipWith

public Mono<MyTask> getMyTask() {
    try {
      
      Mono<A> aMono = aService.getMonoA();

      return aMono.flatMap(
          a -> {
              return bService.getMonoB()
                  .flatMap(
                      b ->
                          taskService.executeTask(a, b));
          });
    } catch (Exception e) {
      log.error("Execute Task: " + e.getMessage());
      return Mono.error(e);
    }
  }

Now if I call the above method with subscribe then the executeTask gets executed

taskService.getMyTask().subscribe();

But as soon as I use zipWith as shown below with an additional mono the execute Task does not get executed and I am not sure why. Is there an additional subscribe needed somewhere Not working with zipWith and an additional Mono

  public Mono<MyTask> getMyTask() {
    try {
      
      Mono<A> aMono = aService.getMonoA();

      return aMono.flatMap(
          a -> {
             Mono<C> cMono = cService.getMonoC();
              return bService.getMonoB()
                  .zipWith(cMono)
                  .flatMap(
                      r ->
                          taskService.executeTask(a, r.getT1(), r.getT2()));
          });
    } catch (Exception e) {
      log.error("Unable to execute Task: " + e.getMessage());
      return Mono.error(e);
    }
  }

Any insight would be appreciated. Note, the actual names of methods are different. Used these method names just for explanation purpose

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
Sandeep Nair
  • 436
  • 2
  • 15

1 Answers1

2

Ok, I will answer my own question. I made stupid mistake to not verify whether the Monos I am zipping with is empty or not and apparently one of them was.

Also this post gave me a hint Spring Reactor: Mono.zip fails on empty Mono

Sandeep Nair
  • 436
  • 2
  • 15