Hi akka gurus:) Can you guide me in this one?
What I'm trying to do - Actor A asks Actor B for a message and wait's for one to arrive back. But, somehow Actor B gives back to A not one message, but 4 of them. Actor A Future
completes properly, but 3 of rest messages are counted as dead letters. Why? Is this right? I mean, Actor A has a proper handler, why the letters are dead then? :-(
[INFO] [11/22/2013 22:00:38.975] [ForkJoinPool-2-worker-7] [akka://actors/user/a] Got result pong [INFO] [11/22/2013 22:00:38.976] [actors-akka.actor.default-dispatcher-4] [akka://actors/deadLetters] Message [java.lang.String] from Actor[akka://actors/user/b#-759739990] to Actor[akka://actors/deadLetters] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'. ...Same message 2 more times...
Please take a look at the code.
package head_thrash
import akka.actor._
import akka.util.Timeout
import scala.concurrent.duration._
import akka.pattern.ask
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
object Main extends App {
val system = ActorSystem("actors")
val a = system.actorOf(Props[A], "a")
val b = system.actorOf(Props[B], "b")
a ! "ping"
system.awaitTermination()
}
class A extends Actor with ActorLogging {
implicit val timeout = Timeout(5.seconds)
def receive = {
case "ping" => {
val b = context.actorSelection("../b")
val future: Future[String] = ask(b, "ping").mapTo[String]
future.onSuccess {
case result: String ⇒ {
log.info("Got result " + result) // <-- got result pong here, that's okay
}
}
}
case "pong" => {
log.info("hmmm...")
}
}
}
class B extends Actor with ActorLogging {
def receive = {
case "ping" => {
sender ! "pong"
sender ! "pong" // <-- dead letter!
sender ! "pong" // <-- dead letter!
sender ! "pong" // <-- dead letter!
}
}
}
That really confuses me. Now you can ask - hey man, why do you need B to send many messages? Well, that is the part of more complicated case - A asks B for message. B answers. Then A waits for another message from B. The tricky part here is plain waiting after the Future completes - and I just can't make my mind to get that model fit into Akka basis.
But fow now, how can I get all 4 messages handled correctly, without dead letters? Thanks :-D