|
@@ -1,6 +1,9 @@
|
|
|
package controllers;
|
|
|
|
|
|
+import akka.dispatch.Futures;
|
|
|
import models.World;
|
|
|
+import play.api.libs.concurrent.Promise;
|
|
|
+import play.libs.Akka;
|
|
|
import play.libs.F;
|
|
|
import play.libs.Json;
|
|
|
|
|
@@ -10,6 +13,7 @@ import org.codehaus.jackson.node.ObjectNode;
|
|
|
import org.codehaus.jackson.map.ObjectMapper;
|
|
|
import play.mvc.Controller;
|
|
|
import play.mvc.Result;
|
|
|
+import scala.concurrent.ExecutionContext;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
@@ -22,10 +26,12 @@ public class Application extends Controller {
|
|
|
|
|
|
private static final int TEST_DATABASE_ROWS = 10000;
|
|
|
//http://stackoverflow.com/questions/3907929/should-i-make-jacksons-objectmapper-as-static-final
|
|
|
- private static final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
|
|
+
|
|
|
+ private static final ExecutionContext DB_EC = Akka.system().dispatchers().lookup("akka.actor.db");
|
|
|
|
|
|
public static Result json() {
|
|
|
- final ObjectNode result = objectMapper.createObjectNode();
|
|
|
+ final ObjectNode result = OBJECT_MAPPER.createObjectNode();
|
|
|
result.put("message", "Hello World!");
|
|
|
return ok(result);
|
|
|
}
|
|
@@ -38,7 +44,12 @@ public class Application extends Controller {
|
|
|
final Random random = ThreadLocalRandom.current();
|
|
|
final List<F.Promise<? extends World>> promises = new ArrayList<F.Promise<? extends World>>(queries);
|
|
|
for (int i = 0; i < queries; ++i) {
|
|
|
- promises.add(future(findWorld(Long.valueOf(random.nextInt(TEST_DATABASE_ROWS) + 1))));
|
|
|
+ // There's no convenience method for submitting a future on an EC in Java. There is
|
|
|
+ // an issue that will address this though: https://github.com/playframework/Play20/issues/972
|
|
|
+ // Meanwhile we call the Akka future directly and wrap its result in a promise.
|
|
|
+ final F.Promise p = Akka.asPromise(Futures.future(
|
|
|
+ findWorld(Long.valueOf(random.nextInt(TEST_DATABASE_ROWS) + 1)), DB_EC));
|
|
|
+ promises.add(p);
|
|
|
}
|
|
|
final List<World> worlds = F.Promise.sequence(promises).get(5L * queries, TimeUnit.SECONDS);
|
|
|
return ok(Json.toJson(worlds));
|