0
votes
object SenderTest extends App {
  val s = ActorSystem("test")
  val ar = s.actorOf(Props[A], "Aactor")
  ar ! "abc"
}

class A extends Actor {
  val sender_ = sender()
  override def receive: Receive = {
    case _: String => {
      println("inside A receive block")
      val b = context.actorOf(Props[B], "Bactor")
      b ! 12
    }
  }
}

class B extends Actor {
  val sender_ = sender()
  override def receive: Receive = {
    case _: Int => {
      println(sender_)
      println("inside B receive block")
      sender_ ! 22
    }
  }
}

I was expecting println(sender_) to print actor A information, but it's printing dead letters. As it's called from actor A, I don't understand why it's printing dead letters. How do I modify this so that I can get the sender's reference?

1
Related: stackoverflow.com/q/23327675/1531971 - user1531971
@jdv, not at all linked to it, in A it will be deadletter as its called from main App but in B it shouldn't be deadletters, that is my question - Lucky
It is related in terms of research. Your message could not be delivered, even after best effort. So this is where you have to start. Not too many people are going to debug this for you, so your chances of an actual answer go up if you show what steps you took to debug and research this. - user1531971
@jdv, println is the debugging point for your information, I am guessing if U had enough akka knowledge then u might have answered instead of criticising.. - Lucky
Oh, well. If you used a few println lines that don't do much than illustrate that the code is being reached than I guess you did everything humanly possible. Every question on this site is open for criticism in order to make it better. You are very welcome! Ironically, the link I gave had a hint for the exact answer given here. Have a super nice day, and welcome to the site. - user1531971

1 Answers

0
votes

Capture the sender inside the receive method:

class B extends Actor {
  override def receive: Receive = {
    case _: Int =>
      val sender_ = sender()
      println(sender_)
      println("inside B receive block")
      sender_ ! 22
  }
}

Note that actor A in its current state doesn't handle an Int (which is what B is sending to A).