Browse Source

Merge pull request #162 from huntc/econ-async-java

Reduced unnecessary futures.
Patrick Falls 12 years ago
parent
commit
c7ca0f037e
1 changed files with 28 additions and 29 deletions
  1. 28 29
      play-java/app/controllers/Application.java

+ 28 - 29
play-java/app/controllers/Application.java

@@ -55,7 +55,6 @@ public class Application extends Controller {
         }
     }
 
-
     public static Result json() {
         final ObjectNode result = OBJECT_MAPPER.createObjectNode();
         result.put("message", "Hello World!");
@@ -64,34 +63,34 @@ public class Application extends Controller {
 
     @Predicated(predicate = IsDbAvailable.class, failed = SERVICE_UNAVAILABLE)
     public static Result db(final Integer queries) {
-        return async(
-                future(new Callable<Result>() {
-                    @Override
-                    public Result call() {
-                        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) {
-                            // 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)), dbEc));
-                            promises.add(p);
-                        }
-                        final List<World> worlds = F.Promise.sequence(promises).get();
-                        return ok(Json.toJson(worlds));
-                    }
-
-                    private Callable<World> findWorld(final Long id) {
-                        return new Callable<World>() {
-                            @Override
-                            public World call() {
-                                return World.find.byId(id);
-                            }
-                        };
-                    }
-                })
-        );
+        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) {
+            // 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)), dbEc));
+            promises.add(p);
+        }
+        return async(F.Promise.sequence(promises).map(new F.Function<List<World>, Result>() {
+
+            @Override
+            public Result apply(List<World> worlds) {
+                return ok(Json.toJson(worlds));
+            }
+
+        }));
 
     }
+
+    private static Callable<World> findWorld(final Long id) {
+        return new Callable<World>() {
+            @Override
+            public World call() {
+                return World.find.byId(id);
+            }
+        };
+    }
+
 }