¿Cómo funciona la API de Acción asíncrona en Play Framework 2.2.x para Scala?
Estaba tratando de crear una API asíncrona. Pero la respuesta muestra ejecución secuencial. Pasos realizados: abre la url en dos pestañas de cromo. Y golpearlos uno tras otro rápidamente. url ex: -localhost:9000/getStar
.
Pero el registro de ejecución es como: -
[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
(Server started, use Ctrl+D to stop and go back to the console...)
[success] Compiled in 107ms
[info] application - Application has started
[info] play - Application started (Dev)
[info] application - Async started ************************** :tarun
[info] application - Success Async call :1
[info] application - Success Async call :2
[info] application - Success Async call :3
[info] application - Success Async call :4
[info] application - Success Async call :5
[info] application - Success Async call :6
[info] application - Success Async call :7
[info] application - Success Async call :8
[info] application - Success Async call :9
[info] application - Async finished ************************** :tarun
[info] application - Async started ************************** :tarun1
[info] application - Success Async call :1
[info] application - Success Async call :2
[info] application - Success Async call :3
[info] application - Success Async call :4
[info] application - Success Async call :5
[info] application - Success Async call :6
[info] application - Success Async call :7
[info] application - Success Async call :8
[info] application - Success Async call :9
[info] application - Async finished ************************** :tarun1
El código para esto es:
package controllers
import play.Logger
import play.api.libs.json.Json
import play.api.mvc._
import scala.concurrent.Future
object StarController extends Controller {
import play.api.libs.concurrent.Execution.Implicits.defaultContext
def getStarAsync(name : String) = Action.async{
val futureResult = Future{
Logger.info("Async started ************************** :" + name)
val a = 0;
for( a <- 1 until 10) {
Thread.sleep(1000)
Logger.info("Success Async call :" + a.toString)
}
Logger.info("Async finished ************************** :" + name)
Map("success" -> Json.toJson(true), "msg" -> Json.toJson("Success Async by :" + name), "code" -> Json.toJson(200))
}
futureResult.map{ result =>
Ok(Json.toJson(result))
}
}
}
¿Alguien puede ayudarme a entender por qué la ejecución de la secuencia fue secuencial incluso con una llamada asincrónica?