I am working on a service that returns a List<Response> object.
There are two kind of object that can build the response. One of them populates the content from internal data, the other reaches out to a service and gets the data.
for(Item item : items){
if(item is external){
call service and get result and populate Response and add to List
}
if(item is internal)
{
populate response object and add to list
}
}
Current impl is procedural and blocking, what i want is a non blocking design such that, if the item is external fire the call and continue with next item in the list. Then when the loop is done, i can wait for all to finish.
What would be a good approach to do this? I m also considering creating seperate classes per responsibility.
EDIT: the reason to introduce is to reduce latency hits.
ExecutorCompletionService(thread pool with callbacks that remembers submitted tasks) seems like a goof git:Where
externalCall(item)is defined as follows for clarity:Obviously once you go asynchronous the result list can have an arbitrary order.
Another approach would be to use ordinary
ExecutorServiceand have an intermediateList<Future<Response>>. The trick is to useAsyncResultwrapper to wrap internal responses (it createsFuturethat is immediately done and returns passed value).Now you can simply iterate over
futures.AsyncResultwill return immediately as the value was already computer when it was created (synchResponse). But you will have to wait forFutures returned from a thread pool.Remember that
Future.get()allows you to retrieve original exception. Also the order ofFutures in the list is the same as the order of original items, so if nthFuturefails, nth item initemslist was the cause.