Reputation: 625
Scala 2.12 using Akka here. When one of my actors receives a particular type of message (say, Fizzbuzz
), I want it to forward that message on to a small number of other actors, exactly as-is:
I tried:
class Foo extends Actor {
override def receive: Receive = {
case Bar =>
println("Bar!")
case Whitstle =>
println("Whistle!")
case Fizzbuzz =>
val actor1 = context.actorSelection("/user/a1")
val actor2 = context.actorSelection("/user/a2")
val actor3 = context.actorSelection("/user/a3")
actor1 ! _
actor2 ! _
actor3 ! _
}
}
And while that compiles and doesn't throw any exceptions, it doesn't work (none of the 3 actors ever receive the Fizzbuzz
message). Any ideas?
Upvotes: 0
Views: 187
Reputation: 3638
In the receive block, collect your msg in a variable and then forward that msg to other actors. Please refer the code below :-
class Foo extends Actor {
override def receive: Receive = {
case Bar =>
println("Bar!")
case Whitstle =>
println("Whistle!")
case msg : Fizzbuzz =>
val actor1 = context.actorSelection("/user/a1")
val actor2 = context.actorSelection("/user/a2")
val actor3 = context.actorSelection("/user/a3")
actor1 ! msg
actor2 ! msg
actor3 ! msg
}
}
This should solve your problem. Please let me know if any doubt persists.
Upvotes: 2