Tôi không chắc chắn đó là những gì bạn đang tìm kiếm, nhưng có khá một giải pháp đơn giản trong mẫu sao chổi ngày đêm, mà bạn có thể thích ứng với sử dụng diễn viên AKKA. Nó sử dụng iframe vô hạn thay vì bỏ phiếu dài. Tôi đã sử dụng một phiên bản thích nghi cho một ứng dụng phức tạp hơn làm nhiều cuộc gọi DB và tính toán dài trong các diễn viên AKKA và nó hoạt động tốt.
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"))
}
Lưu ý rằng fromCallback này không cho phép bạn kết hợp điều tra viên với "andThen", có trong phiên bản cốp play2 một phương pháp generateM có thể phù hợp hơn nếu bạn muốn sử dụng kết hợp.
Đó không phải là bỏ phiếu dài, nhưng nó hoạt động tốt.