Reputation: 393
I am new to Spring WebFlux but the I am trying to achieve that is specified below.
First Service -> AuthService Mono<String> ->gives auth token
Second Service -> ServiceSecodn Uses output from above service
Third service -> Uses output from both above specified services.
Not able to express it using webFlux flatmap API
service1.dologin().flatmap(info->service2.apiCall(info))
I want to make a third service call service3.apiCall(loginInfo,infoFromSecondServiceCall)
Upvotes: 0
Views: 745
Reputation: 12184
Tuple2
in order to combine several outputsYou may keep all information all the way by holding it in one structure as in the following example:
service1.dologin()
.flatMap(lInfo ->
service2.apiCall(lInfo)
.map(result -> Tuples.of(lInfo, result))
)
.flatMap(lInfoAndResultTuple ->
service3.apiCall(
lInfoAndResultTuple.getT1(),
lInfoAndResultTuple.getT2()
)
)
In this example, we use additional structure to keep required data all the way down. The main advantage of that example is a flat stream. The main disadvantage of that technique is an additional structure which makes harder to operate on data within it.
In turn, to avoid redundant data structure, we may use closure technique and always have access to the parent closure data as it is shown in the following example:
service1.dologin()
.flatMap(lInfo ->
service2.apiCall(lInfo)
.flatMap(result ->
service3.apiCall(lInfo, result)
)
)
Comparing to the previous example, here we do not have to use an additional data structure in order to access upstream data. The Java Language provides us with build in access to the parents' closures data so that we may use it anytime in children's closures. The main advantage of that technique is the absence of redundant data structure. The main disadvantage is complex Flux/Closures structure which looks like well known Callback Hell
Upvotes: 1