Reputation: 127
I am new to Akka and making several test files to practice Akka concepts. The following test seemed simple enough, but for whatever reason I am unable to obtain the value of the future if I receive the future from an Actor. I based all my assumptions on the few examples from the akka documentation ( http://akka.io/docs/akka/1.3-RC2/scala/futures.html#futures-scala ) For example:
This works like a wizard:
val f = Future{ 1 + 4 }
f onComplete
{
_.value.get.fold(
v => throw new Exception("My Exception"),
println(_)
)
}
This does not:
class FutureDemo extends Actor
{
def receive =
{
case (a: Int, b: Int) =>
a + b
}
}
val fa1 = actorOf[FutureDemo].start()
val future = fa1 ? (1, 2)
future onComplete
{
_.value.get.fold(
v => throw new Exception("My Exception"),
println(_)
)
}
I was very pleased to know I was the only one that has ever had this issue due to the extreme simplicity of futures (lucky me). Would anyone assist in opening my eyes to the violently obvious issue?
I should note that I had tried to process both inside another actor, and in a simple main method. Each of which failed in the same awesome way which included no notification whatsoever. If the onComplete is replaced with a simple println(future.get)
I eventually get a timeout exception (Exception in thread "main" akka.dispatch.FutureTimeoutException: Futures timed out after [4996] milliseconds). I should also note that I did attempt to try with the 1.1 and 1.2 released versions of akka as well.
Thanks!
Upvotes: 2
Views: 325
Reputation: 15472
It is indeed but one tiny detail you are missing: your actor does not reply! May I suggest the following:
class FutureDemo extends Actor {
def receive = {
case (a: Int, b: Int) => self.reply(a + b)
}
}
Upvotes: 5