Application.scala 852 B

123456789101112131415161718192021222324252627282930313233343536
  1. package controllers
  2. import play.api.Play.current
  3. import play.api.mvc._
  4. import play.api.libs.json.Json
  5. import play.api.libs.concurrent._
  6. import java.util.concurrent.ThreadLocalRandom
  7. import scala.concurrent._
  8. import models._
  9. object Application extends Controller {
  10. private val TEST_DATABASE_ROWS = 10000
  11. def json() = Action {
  12. Ok(Json.obj("message" -> "Hello World!"))
  13. }
  14. def db(queries: Int) = Action {
  15. import play.api.libs.concurrent.Execution.Implicits._
  16. val dbEc = Akka.system.dispatchers.lookup("akka.actor.db")
  17. Async {
  18. val random = ThreadLocalRandom.current()
  19. val worlds = Future.sequence( (for {
  20. _ <- (1 to queries).par
  21. } yield Future(World.findById(random.nextInt(TEST_DATABASE_ROWS) + 1))(dbEc)).toList)
  22. worlds map {
  23. w => Ok(Json.toJson(w))
  24. }
  25. }
  26. }
  27. }