The following code uses 2 asks and initially seems like it supposed to 
print *"This should be printed in actor1 but most of the time wont because 
of a race condition with the receive method finishing"*. However there 
seems to be a race condition between the 'receive' method finishing and the 
future finishing and thus nothing is printed. Is this the intended behavior 
by akka? is this a bug? I am trying to avoid using 'ask' as much as 
possible and using 'tell' instead, but sometimes it's a must.

Also posted this on stackoverflow but figured here I'll get a better answer.
http://stackoverflow.com/questions/31387842/akka-ask-returns-nothing-when-tell-is-used-in-future

import akka.actor._
import akka.routing.SmallestMailboxPool
import akka.util.Timeout
import akka.pattern.ask
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Failure, Success}

object ActorsAskingStuff extends App {
  val system = ActorSystem("abc-system")
  val actor1 = system.actorOf(Props[Actor1].withRouter(SmallestMailboxPool(1)), 
"actor1")
  actor1 ! 5
}

class Actor1 extends Actor {
  implicit val timeout = Timeout(60 seconds)
  val actor2: ActorRef = 
context.actorOf(Props[Actor2].withRouter(SmallestMailboxPool(1)), "actor2")
  override def receive: Receive = {
    case _ =>
      println("actor1 was called")
      actor2 ? "hello" onComplete {
      case Success(a) => println(a)
      case Failure(b) => println("FAILURE")
    }
  }
}

class Actor2 extends Actor {
  implicit val timeout = Timeout(6 seconds)
  val actor3: ActorRef = 
context.actorOf(Props[Actor3].withRouter(SmallestMailboxPool(1)), "actor3")
  override def receive: Actor.Receive = {
    case _ =>
      println("actor2 was called")
      actor3 ? "hello" map {
        _ =>
          println("Actor2 completed the future")
          sender ! "This should be printed in actor1 but most of the time wont 
because of a race condition with the receive method finishing"
      }

      // uncomment this to make it work
      //Thread.sleep(100)
  }
}

class Actor3 extends Actor {
  override def receive: Actor.Receive = {
    case _ =>
      println("actor3 was called")
      sender ! "I'm actor3"
  }
}



-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to