Is there any better alternative (desing/performance/memory optimization) to what I have used below:
Problem statement: In J2EE environment, when a request comes my controller needs to fetch data from three different services (ex. 1st get weather data, 2nd gets traffic density, and the last one gets current no of aliens visited us). And if one service takes like 5 seconds (in best case) to fetch, then in synchronous manner they will take at least 15 Sec. But controller should not take more than 6-7 seconds. So end up calling in different threads. But there might be cases when a service takes more than 10 seconds (may be a worst case scenario).
So I thought of creating a thread wait, notify using an observable pattern with a small state maintaining. It was working, but not happy with the design.
Then I found Java 'ForkJoinPool'. For each request controller creates a new '
ForkJoinPool' instance. Created a custom class (sayForkService) extendingRecursiveActionwhich has aList<RecursiveAction>. And ForkService's compute method has
@Override
protected void compute() {
for (RecursiveAction recursiveAction : actions) {
recursiveAction.fork(); // async calling of all other service's compute method which actually fetches the data from the relevant service.
}
mPool.shutdown();
}
Presently using this and it perfoms good. Want to know is there a better way to do this?
SO Rocks!!
Aucun commentaire:
Enregistrer un commentaire