Application.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package controllers;
  2. import akka.dispatch.ExecutionContexts;
  3. import akka.dispatch.Futures;
  4. import models.World;
  5. import play.Play;
  6. import play.core.NamedThreadFactory;
  7. import play.libs.Akka;
  8. import play.libs.F;
  9. import play.libs.Json;
  10. import static play.libs.Akka.future;
  11. import org.codehaus.jackson.node.ObjectNode;
  12. import org.codehaus.jackson.map.ObjectMapper;
  13. import play.mvc.Controller;
  14. import play.mvc.Result;
  15. import scala.concurrent.ExecutionContext;
  16. import utils.Predicate;
  17. import utils.Predicated;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Random;
  21. import java.util.concurrent.*;
  22. public class Application extends Controller {
  23. private static final int MAX_QUERIES_PER_REQUEST = 20;
  24. private static final int TEST_DATABASE_ROWS = 10000;
  25. //http://stackoverflow.com/questions/3907929/should-i-make-jacksons-objectmapper-as-static-final
  26. private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
  27. private static final int partitionCount = Play.application().configuration().getInt("db.default.partitionCount");
  28. private static final int maxConnections =
  29. partitionCount * Play.application().configuration().getInt("db.default.maxConnectionsPerPartition");
  30. private static final int minConnections =
  31. partitionCount * Play.application().configuration().getInt("db.default.minConnectionsPerPartition");
  32. private static final ThreadPoolExecutor tpe = new ThreadPoolExecutor(minConnections, maxConnections,
  33. 0L, TimeUnit.MILLISECONDS,
  34. new LinkedBlockingQueue<Runnable>(),
  35. new NamedThreadFactory("dbEc"));
  36. private static final ExecutionContext dbEc = ExecutionContexts.fromExecutorService(tpe);
  37. // A predicate for checking our ability to service database requests is determined by ensuring that the request
  38. // queue doesn't fill up beyond a certain threshold. For convenience we use the max number of connections * the max
  39. // # of db requests per web request to determine this threshold. It is a rough check as we don't know how many
  40. // queries we're going to make or what other threads are running in parallel etc. Nevertheless, the check is
  41. // adequate in order to throttle the acceptance of requests to the size of the pool.
  42. public static class IsDbAvailable implements Predicate {
  43. @Override
  44. public boolean condition() {
  45. return (tpe.getQueue().size() < maxConnections * MAX_QUERIES_PER_REQUEST);
  46. }
  47. }
  48. public static Result json() {
  49. final ObjectNode result = OBJECT_MAPPER.createObjectNode();
  50. result.put("message", "Hello World!");
  51. return ok(result);
  52. }
  53. @Predicated(predicate = IsDbAvailable.class, failed = SERVICE_UNAVAILABLE)
  54. public static Result db(final Integer queries) {
  55. final Random random = ThreadLocalRandom.current();
  56. final List<F.Promise<? extends World>> promises = new ArrayList<F.Promise<? extends World>>(queries);
  57. for (int i = 0; i < queries; ++i) {
  58. // There's no convenience method for submitting a future on an EC in Java. There is
  59. // an issue that will address this though: https://github.com/playframework/Play20/issues/972
  60. // Meanwhile we call the Akka future directly and wrap its result in a promise.
  61. final F.Promise p = Akka.asPromise(Futures.future(
  62. findWorld(Long.valueOf(random.nextInt(TEST_DATABASE_ROWS) + 1)), dbEc));
  63. promises.add(p);
  64. }
  65. return async(F.Promise.sequence(promises).map(new F.Function<List<World>, Result>() {
  66. @Override
  67. public Result apply(List<World> worlds) {
  68. return ok(Json.toJson(worlds));
  69. }
  70. }));
  71. }
  72. private static Callable<World> findWorld(final Long id) {
  73. return new Callable<World>() {
  74. @Override
  75. public World call() {
  76. return World.find.byId(id);
  77. }
  78. };
  79. }
  80. }