'CompletableFuture with different return type
I have to make rest api calls to different Downstream systems and done coding like below,
Class Investorservice {
public **Boolean** service1(String id){
Boolean status = call downstream with id and return the status
return status;
}
public **Boolean** service2(String id){
Boolean status = call downstream with id and return status
return status;
}
public **InputStream** service3(String id){
InputStream is = call downstream to get IS;
return is;
}
public **Boolean** service4(InputStream is){
Boolean status =// Pass the input stream to external system with rest api
return status;
}
}
public class RestClientController {
Investorservice obj;
public void rest service( @RequestParam String id){
// listfuture contains service 1&2 call which makes third party call, do some processing and return the status
List<CompletableFuture<Boolean>>listFuture = new ArrayList();
listFuture.add(obj.service1(id));
listFuture.add(obj.service2(id));
CompletableFuture<Void> allFuture = CompletableFuture.allOf(listFuture.toArray(new CompletableFuture[listFuture.size()]));
CompletableFuture<List<Boolean>> allCompFuture = allFuture.thenApply(future -> listFuture.stream()
.map(CompletableFuture::join).collect(Collectors.toList()));
List<Boolean> resultFuture= allCompFuture.get();
// How can I add service 3 call to listFuture as it's different return type adding serice3 gives compilation error.
// Then based on service 1&2 response in resultFuture(i.e if both are successful), I need to invoke Service 4 with output of Service 3
}
}
Now I'm stuck at adding service3 call to listFuture and invoking Service 4 on success of Service 1&2.
listFuture.add(obj.service3(id), executors); // giving compilation error due to different return type
How can I add service3 to listFuture and run Service 1, 2 & 3 all in parallel?
On Successful response for service1 and service2 (i.e both service response should be true), How can I collect and pass the Service3 output to Service 4 and make service4 call?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|