Ich bin nicht sicher, dass ist das, was Sie suchen, aber es ist durchaus eine einfache Lösung im Comet-Uhr Probe, dass Sie AKKA Akteure verwenden anpassen können. Es verwendet einen unendlichen Iframe anstelle eines langen Pollings. Ich habe eine angepasste Version für eine komplexere Anwendung verwendet, die mehrere DB-Aufrufe und lange Berechnungen in AKKA-Darstellern ausführt, und es funktioniert gut.
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"))
}
Beachten Sie, dass dieser fromCallback nicht Sie Aufzählungen erlaubt zu kombinieren mit „andthen“ gibt es in der Kofferraum Version von play2 eine generateM Methode, die besser geeignet sein könnte, wenn Sie Kombinationen verwenden mögen.
Es ist nicht lange polling, aber es funktioniert gut.