Created
August 18, 2013 10:40
-
-
Save sshark/6261010 to your computer and use it in GitHub Desktop.
Mulitple actors example using Future-s and Await.
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
import akka.actor._ | |
import akka.pattern.ask | |
import akka.util.Timeout | |
import scala.concurrent.forkjoin.ForkJoinPool | |
import scala.concurrent.{Future, ExecutionContext, Await} | |
import scala.concurrent.duration._ | |
import scala.util.Random | |
/** | |
* | |
* @author Lim, Teck Hooi | |
* | |
* | |
*/ | |
class ResponsiveActor extends Actor { | |
def receive = { | |
case x : Int => | |
Thread.sleep(Random.nextInt(500)) | |
println(x) | |
sender ! "done" | |
} | |
override def postStop() { | |
println("I have stopped.") | |
} | |
} | |
object FuturesAndAwaitExample extends App { | |
val actorSystem = ActorSystem("SystemStopExample") | |
val numberOfActors = 3 | |
val actors = for (i <- 0 until numberOfActors) yield actorSystem.actorOf(Props[ResponsiveActor]) | |
implicit val timeout = Timeout(15 seconds) | |
implicit val ec = ExecutionContext.fromExecutorService(new ForkJoinPool()) | |
val futures = (1 to 10 * numberOfActors) map {x => actors(x % 3) ? x} | |
Await.result(Future.sequence(futures), Duration.Inf) | |
println("Done") | |
actorSystem.shutdown | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment