Google Groups

Java Async in Play 2.0 using Promise


Vladimir Vlach Apr 30, 2012 11:19 AM
Posted in group: play-framework
Hi,

I struggle to come up with Asynchronous architecture for my project using Promise using just Java in Play 2.0.

I'd like to fist request data from datastore in parallel - each is time consuming task and wait for all queries to complete:

// runs in serial fashion
public static Result showSomePage() {
  UserObject userObject1 = SlowQuery.getUser(440);
  CategoryObject categoryObject1 = SlowQuery.getCategory("root");

  return ok("some page to render");
}

How can I transform this into parallel execution using Play Async Framework?

This is what I could come up with but it does not wait for the results.

Promise<DataObject> userObject1 = SlowQuery.getUser(440);
Promise<DataObject> userObject2 = SlowQuery.getCategory(420);
async(userObject1.map(new Function<DataObject, Result>() {
public Result apply(DataObject res) {
Logger.debug("Got result (userObject1): " + res.toString());
return ok("Result Received");
}
}));
Logger.debug("finished, yet Result is still not available.");

This execution does not wait for promiseOfResult1 to be completed. I also need the result in main scope. How can I achieve this?

Thanks,
Vlad