Skip to content

Instantly share code, notes, and snippets.

@geoffeg
Created October 12, 2011 03:39
Show Gist options
  • Save geoffeg/1280203 to your computer and use it in GitHub Desktop.
Save geoffeg/1280203 to your computer and use it in GitHub Desktop.
Futures and I do not understand each other
import java.util.Random
import akka.dispatch.Future
import akka.actor._
import Commands._
import collection.mutable.ListBuffer
object Commands {
trait Command
case class GetRandom() extends Command
case class GenRandomList() extends Command
}
class Secondary() extends Actor {
val randomGenerator = new Random()
override def receive = {
case GetRandom() =>
self reply randomGenerator.nextInt(100)
}
}
class Primary() extends Actor {
private val secondary = Actor.actorOf[Secondary]
override def receive = {
case GenRandomList() =>
val futures = new ListBuffer[Future[Integer]]
for (i <- 0 until 10) {
futures += secondary ? GetRandom
}
val futureWithList = Future.sequence(futures)
futureWithList.map { foo =>
println("Shouldn't foo be an integer now? " + foo)
}.get
}
override def preStart() = {
secondary.start()
}
}
// Goal is to fire off a bunch of calls to Secondary which returns futures
// Then, once all the futures complete, return the list to Starter
class Starter extends App {
println("Starting")
var master = Actor.actorOf(new Primary()).start()
master ! GenRandomList()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment