Scala 2.12在这里使用Akka。当我的一个演员收到特定类型的消息(比如sentence = "This snowy weather is so cold"
# Split the sentence into a list of the words
words = sentence.split(" ")
# Get the index of the word you want to replace
word_to_replace_index = words.index("cold")
# Replace the target word with the new word based on the index
words[word_to_replace_index] = "awesome"
# Generate a new sentence
new_sentence = ' '.join(words)
)时,我希望它将该消息转发给少数其他演员,完全按原样:
我试过了:
Fizzbuzz
虽然编译并没有抛出任何异常,但它不起作用(3个演员都没有收到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 ! _
}
}
消息)。有什么想法吗?
答案 0 :(得分:2)
在接收块中,收集变量中的msg,然后将该msg转发给其他actor。请参考以下代码: -
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
}
}
这应该可以解决您的问题。如果有任何疑问仍然存在,请告诉我。