Application.java 3.5 KB

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