I have four Services.
One will give hotels which is matching to the query.
One will give City which is matching to the query.
One will give Airports which is matching to the query.
One will give Metro stations which is matching to the query.
For each HTTP request, I will hit these four services and merge the response from four of the service and return.
So my Sample code is like
for(EntityGroups entityGroups: EntityGroups.values()){
Callable<Response> callable = new XCallable(entityTypetoFetch, searchQuery);
Future<List<Entity>> listFuture = executor.submit(callable);
entityTypeAndFutureMap.put(entityTypetoFetch, listFuture);
}
After this I get all response in for loop
trainStationList = entityTypeAndFutureMap.get(EntityGroups.TRAIN_STATION).get();
landmarkEntities = entityTypeAndFutureMap.get(EntityGroups.LANDMARK_GROUP).get();
cityEntities = entityTypeAndFutureMap.get(EntityGroups.CITY_GROUP).get();
hotelEntities = entityTypeAndFutureMap.get(EntityGroups.HOTEL_GROUP).get();
As I want all the list to merge and make the final response. I am making a blocking call. I just want to merge these list and return. If I use completablefuture here, will it help?
As of now my CPU uses goes very high as I increase the TPS. increasing machines is helping, but still is there any optimized way to solve this?
CompletableFuture
correctly may give you an order of magnitude capacity increase for your service. – Stamina