0

I have a scenario with reactive/async call. I am using spring-boot-starter-webflux and using webclient to make external HTTP calls. My scenario is I have to make a call to callA() and then check its response ResponseA. If its ResponseA is ok than exit and return ResponseA. Otherwise create second request requestB using ResponseA and make a call to callB(). Then check its response ResponseB. If it is ok then return ResponseA otherwise doRetry on callA().

public Mono<ResponseA> callA(Request1 requestA) {
    // calling service-A using webclient
}
public Flux<ResponseB> callB(Request2 requestB) { 
    // calling service-B using webclient but to create requestB, I need ResponseA.
}
Toerktumlare
  • 12,548
  • 3
  • 35
  • 54
Ashish Sharma
  • 574
  • 7
  • 18
  • You just put an if statement in a flatMap – Toerktumlare Oct 12 '20 at 05:37
  • Inside flatMap, after adding condition, I want to make a call to callB() and check its response. For checking callB() response, I have to subscribe to it. But issue is subscribe() does not return anything. So I will not able to chain these two operations. I am looking for a way where I can make a chain of two operations. First I will check responseB then return responseA. – Ashish Sharma Oct 12 '20 at 12:09

1 Answers1

0

you just need to do some if-statements in a flatMap. Probably split it up into some better function names etc. No subscribing, no blocking.

callA(createNewRequest()).flatMap(response1 -> {

    // Validate response
    if(!isValidResponse(response)) {

        // if failed validation, create new request and do a new call
        var request = buildRequest(response);
        return callB(request).flatMap(response2 -> {

                // validate second response
                if(!isValidResponse(response2)) {

                     // failed validation return the first response.
                     return Mono.just(response1)
                }

                // otherwise recursively call again
                return callA(createNewRequest()); // Warning this can be an infinite loop
            }
    }

    // Validation passed
    return Mono.just(response);
}
Toerktumlare
  • 12,548
  • 3
  • 35
  • 54
  • Hi @Thomas, Thanks for looking into my issue. I am trying to use this logic but I stuck as last line is giving me below compile-time error. `no instance(s) of type variable(s) R exist so that Flux conforms to Mono extends R>` // Validation passed return Mono.just(response); <-- this statement – Ashish Sharma Oct 12 '20 at 19:08
  • it was not meant to be a straight off copy paste solution.... It means you are returning a Flux, when it wants a Mono, or the other way around... i recommend you read up on the basics of reactive programming. Returning correct stuff from functions is basic basic basic programming. – Toerktumlare Oct 12 '20 at 19:29
  • Thanks. I understand and correct it. I convert Flux< response2 > into Mono< response2>. – Ashish Sharma Oct 12 '20 at 21:18
  • if this post helped you then please concider marking it as the correct answer. – Toerktumlare Oct 13 '20 at 14:04