Last active
August 29, 2015 14:08
-
-
Save hussachai/d4e19e8840efeabaedce to your computer and use it in GitHub Desktop.
ActorToEnumerator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pg.play | |
import play.api.libs.concurrent.Akka | |
import play.api.libs.concurrent.Execution.Implicits.defaultContext | |
import scala.concurrent.duration._ | |
import akka.actor._ | |
import play.api.test.FakeApplication | |
import play.api.libs.iteratee.Enumerator | |
import akka.pattern.ask | |
import scala.language.postfixOps | |
import akka.util.Timeout | |
import play.api.libs.iteratee.Iteratee | |
object ActorEnumerator extends App { | |
implicit val app = FakeApplication() | |
implicit val timeout = Timeout(1 second) | |
val actor: ActorRef = Akka.system.actorOf(Props(new FooActor)) | |
Akka.system.scheduler.schedule(5.seconds, 5.seconds, actor, "expire") | |
val enumerator = Enumerator.generateM((actor ? "ok").map{ r => | |
val s: Option[_] = r match { | |
case o: Option[_] => o | |
case a: Any => Option(a) | |
} | |
s | |
}) | |
val iteratee = Iteratee.foreach[Any]{v => println(v)} | |
enumerator |>> iteratee | |
} | |
class FooActor extends Actor { | |
var i = 0 | |
def receive = { | |
case s: String => { | |
Thread.sleep(500) | |
i = i + 1 | |
if(i >= 5){ | |
if(!sender.isTerminated){ | |
sender ! None | |
}else{ | |
println("Terminated") | |
self ! PoisonPill | |
} | |
}else{ | |
sender ! "hello"+i+" - "+Thread.currentThread().getId(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment