1

I read the topic How to use Spring WebClient to make multiple calls simultaneously? , but my case is a bit different. I'm calling 2 different external services using webclient, let's say from method Mono < Void > A() , followed by Mono < Void > B (). My goal is to extract data from A(), then pass it to B(). Is there correct way to avoid:

  • asynchronous call (which leads to Illegal arg exception, since B requesting args BEFORE A() complete);
  • blocking call, cause the system is reactive.

Is there are a standart way to achieve it?

tarmogoyf
  • 298
  • 3
  • 17

2 Answers2

1

First scenario:

Mono<> a = getFromAByWebClient();

and you want to send this data to call service B via a post or put request,

here, since mono is one object and you want to send it through api in a post or method, so must have that data with you, here you should wait until data is not come from first service, else it will hit the api with blank data or will result an exception.

Second scenario:

Since B is dependent on A, why not to call A service inside B service and get data.

Since in Spring reactive everything is stream, so can do operation with one data until others are on their way, but that operations which will be performed should have data.

Manish Bansal
  • 819
  • 2
  • 8
  • 19
  • It seems my case even simplier. I have 2 spring beans (and can convert into one, thats not the problem), so, actually, Im looking for Mono.zip() solution or something. Unfortunately , Im struggling learning reactive concepts. Well, thanks for the support, Ill try and share the outcome. – tarmogoyf Jun 01 '21 at 14:59
  • 1
    If you want to join output of two mono, you can use mono.mergeWith() method, or can merge it in Flux – Manish Bansal Jun 01 '21 at 16:22
0

Well, I was told how to refactor the code. The problem was fixed and for memorizing, here is the solution:

  • the original code returns

    Mono.fromRunnable(()->apply(param param));
    
  • method 'apply' subscribes on call of external resource:

      apply(param param) {
      service.callRemote(val x).subscribe();
    
      <---some bl --->
    
      };
    

So,it seems Like when first beanA.process() followed beanB.process(), reactive pipeline falls apart, and lambda from runnable() branches into separate thread.

What was changed: beanA and beanB methods apply return logic -

     Mono.just.flatMap(service.callRemote(val x)).then();

apply() has been removed, remote call wrapped into flatMap() and integrated into pipeline. Now it works as expected, sequentionally calling remote resource.

tarmogoyf
  • 298
  • 3
  • 17