Application.java 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package controllers;
  2. import play.libs.Json;
  3. import play.mvc.*;
  4. import static play.libs.Akka.future;
  5. import java.util.concurrent.Callable;
  6. import org.codehaus.jackson.node.ObjectNode;
  7. import org.codehaus.jackson.map.ObjectMapper;
  8. import views.html.*;
  9. import models.*;
  10. import java.util.*;
  11. import java.util.concurrent.*;
  12. public class Application extends Controller {
  13. private static final int TEST_DATABASE_ROWS = 10000;
  14. //http://stackoverflow.com/questions/3907929/should-i-make-jacksons-objectmapper-as-static-final
  15. private static ObjectMapper objectMapper = new ObjectMapper();
  16. public static Result json() {
  17. return async(
  18. future(new Callable<Result>() {
  19. public Result call() {
  20. ObjectNode result = objectMapper.createObjectNode();
  21. result.put("message", "Hello World!");
  22. return ok(result);
  23. }
  24. })
  25. );
  26. }
  27. public static Result db(final Integer queries) {
  28. return async(
  29. future(new Callable<Result>() {
  30. public Result call() {
  31. final Random random = ThreadLocalRandom.current();
  32. final World[] worlds = new World[queries];
  33. for (int i = 0; i < queries; i++) {
  34. worlds[i] = World.find.byId((long)(random.nextInt(TEST_DATABASE_ROWS) + 1));
  35. }
  36. return ok(Json.toJson(worlds));
  37. }
  38. })
  39. );
  40. }
  41. }