Forráskód Böngészése

The Java version now submits futures to the dbEc execution context

Christopher Hunt 12 éve
szülő
commit
a8a63bf841
1 módosított fájl, 11 hozzáadás és 5 törlés
  1. 11 5
      play-java/app/controllers/Application.java

+ 11 - 5
play-java/app/controllers/Application.java

@@ -1,6 +1,8 @@
 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;
@@ -24,12 +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 dbEc = Akka.system().dispatchers().lookup("akka.actor.db");
+    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);
     }
@@ -42,8 +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) {
-                            //FIXME: How do we express the use of dbEc here?
-                            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));