问题描述
我有一个基于Scala中Akka演员的非常简单的结构,但是我一直收到关于未送达消息的警告。这是主类的代码,Collector是扩展Actor的单独类:
I have a very simple structure based on Akka actors in Scala, but I keep on receiving warnings about undelivered messages. This is the code for the main class, Collector is a separate class extending Actor:
object Executor extends App {
class ExecutorMaster extends Actor {
def receive() = {
case _ => Executor.actorSystem.actorOf(Props[Collector], name = "Collector") ! true
}
}
val actorSystem = ActorSystem("ReadScheduler")
private val app = actorSystem.actorOf(Props[ExecutorMaster], name = "Executor")
app ! true
}
消息未传递到收集器,代码的结果是:
The message is not being delivered to the Collector, the result for the code is:
消息传递失败的原因是什么?
What can be the reason of this unsuccessful delivery of the message? Is there something that I keep on missing in the concept?
推荐答案
您应该使用层次结构-启动该 Collector
作为 ExecutorMaster
的子代。
You should use a hierarchy - launch that Collector
as a child of the ExecutorMaster
.
您在内部执行的操作 receive
方法正在尝试创建一个与另一个具有相同名称的演员,该演员是在第一个消息 ExecutorMaster
What you are doing inside the receive
method is attempting to create an actor having the same name as another one that gets created after the first message that the ExecutorMaster
receives.
考虑使用以下方法:
val collector = context.actorOf(Props[Collector], name = "Collector")
def receive = {
case _ => collector ! true
}
还应该使用 case对象
而不是用于标识 true
含义的原语。
You also ought to use a case object
and not a primitive to identify the meaning of true
.
这篇关于Akka Scala演员的死信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!