No estoy seguro de que eso es lo que busca, pero no hay una solución bastante simple en la muestra cometa-reloj, que puede adaptarse a usar actores AKKA. Utiliza un iframe infinito en lugar de un largo sondeo. He utilizado una versión adaptada para una aplicación más compleja haciendo múltiples llamadas a bases de datos y cálculos largos en AKKA actores y funciona bien.
def enum = Action {
//get your actor
val myActorRef = Akka.system.actorOf(Props[TestActor])
//do some query to your DB here. Promise.timeout is to simulate a blocking call
def getDatabaseItem(id: Int): Promise[String] = { Promise.timeout("test", 10 milliseconds) }
//test iterator, you will want something smarter here
val items1 = 1 to 10 toIterator
// this is a very simple enumerator that takes ints from an existing iterator (for an http request parameters for instance) and do some computations
def myEnum(it: Iterator[Int]): Enumerator[String] = Enumerator.fromCallback[String] {() =>
if (!items1.hasNext)
Promise.pure[Option[String]](None) //we are done with our computations
else {
// get the next int, query the database and compose the promise with a further query to the AKKA actor
getDatabaseItem(items1.next).flatMap { dbValue =>
implicit val timeout = new Timeout(10 milliseconds)
val future = (myActorRef ? dbValue) mapTo manifest[String]
// here we convert the AKKA actor to the right Promise[Option] output
future.map(v => Some(v)).asPromise
}
}
}
// finally we stream the result to the infinite iframe.
// console.log is the javascript callback, you will want something more interesting.
Ok.stream(myEnum(items1) &> Comet(callback = "console.log"))
}
Tenga en cuenta que este fromCallback no permite que usted combine con enumeradores "andthen", existe en la versión maletero de un método play2 generateM que podría ser más apropiado si desea utilizar combinaciones.
No es un largo sondeo, pero funciona bien.