pygumby
pygumby

Reputation: 6790

How to only process messages from child actors?

I built an Akka FSM that spawns two child actors in its constructor and only receives messages from one of them.

val lhsRef: ActorRef = context.actorOf(Props(new PrimitiveNode(tpe)), "lhs")
val rhsRef: ActorRef = context.actorOf(Props(new PrimitiveNode(tpe)), "rhs")

When dealing with incoming events, I want to filter for those that come from lhsNode. I tried to achieve this the following way:

when(WaitFor1) {
  case Event(event: SomeEventType, _) =>
    // Control flow reaches this point but...
    if (sender == lhsRef) {
      // ...it never enters the if clause.

This does not work. In fact, sender and lhsNodeare not equal, even though it's definitely lhsNode that sends messages to my FSM.

sender()        Actor[akka://default/user/$a#670517729]
lhsNode         Actor[akka://default/user/$a/$a/$b/lhs#-354253348]

What am I doing wrong?

Upvotes: 0

Views: 44

Answers (1)

Nagarjuna Pamu
Nagarjuna Pamu

Reputation: 14825

Compare only the names of the sender and lhs. Check if message is really sent by the lhsNode.

sender.path.name == "lhs"

or

sender.path.name = lhsNode.path.name

Ensure that the sender is the lhs actor only. forward the message if any other actor is involved in between.

Something like below

when(WaitFor1) {
  case Event(event: SomeEventType, _) =>
    // ...
    if (sender.path.name == "lhs") {
      // ....

with Pattern match guard

when(WaitFor1) {
  case Event(event: SomeEventType, _) (if sender.path.name == "lhs") =>

Upvotes: 1

Related Questions