scalarookie scalarookie - 1 year ago 61
Scala Question

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
. I tried to achieve this the following way:

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

This does not work. In fact,
are not equal, even though it's definitely
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?

Answer Source

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

or =

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 ( == "lhs") {
      // ....

with Pattern match guard

when(WaitFor1) {
  case Event(event: SomeEventType, _) (if == "lhs") =>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download