在遵循了文档示例(2.1.4)之后,我在微内核加载的执行元处理消息时遇到了问题,其中可引导扩展类的定义如下:
class HelloKernel extends Bootable {
val system = ActorSystem("hellokernel")
def startup = {
system.actorOf(Props[HelloActor]) ! Start
}
def shutdown = {
system.shutdown()
}
}如果创建了一个伪实例(即不在代码中的其他任何地方使用),如下所示,消息将按预期进行处理。
class HelloKernel extends Bootable {
val system = ActorSystem("hellokernel")
val dummyActor = system.actorOf(Props[HelloActor])
def startup = {
system.actorOf(Props[HelloActor]) ! Start
}
def shutdown = {
system.shutdown()
}
}是否真的应该有一个虚拟实例化,或者这样做,我是否会造成一些副作用,从而导致消息被处理?
基于Thomas Letschert在Akka 2.1 minimal remote actor example中给出的代码,我已经将服务器端变成了一个微内核托管角色。
import akka.actor.Actor
import akka.actor.ActorLogging
import akka.actor.ActorSystem
import akka.actor.Props
import akka.kernel.Bootable
class Joe extends Actor {
def receive = {
case msg: String => println("joe received " + msg + " from " + sender)
case _ => println("Received unknown msg ")
}
}
class GreetServerKernel extends Bootable {
val system = ActorSystem("GreetingSystem")
val joe = system.actorOf(Props[Joe], name = "joe")
println(joe.path)
joe ! "local msg!"
println("Server ready")
def startup = {
}
def shutdown = {
println("PrimeWorker: Shutting Down")
system.shutdown
}
}在这种情况下,当删除消息时不会处理虚拟实例化,即
val joe = system.actorOf(Props[Joe], name = "joe")调用者代码是
import akka.actor._
import akka.actor.ActorDSL._
object GreetSender extends App {
implicit val system = ActorSystem("GreetingSystem")
val joe = system.actorFor("akka://GreetingSystem@127.0.0.1:2554/user/joe")
println(joe.path)
val a = actor(new Act {
whenStarting { joe ! "Hello Joe from remote" }
})
joe ! "Hello"
println("Client has sent Hello to joe")
}发布于 2013-05-21 00:48:37
如果您发布的代码确实是准确的,那么只需将joe实例的实例化移动到startup操作中,而不是可引导类的构造函数中:
def startup = {
system.actorOf(Props[Joe], name = "joe")
}绑定到名称joe的参与者需要先启动,然后有人才能按名称查找它并向其发送消息。这基本上与在bootable类的构造函数中启动它是一样的,但我认为约定要求在startup函数中执行所有参与者实例化,而不是在bootable类主体(以及构造函数)中执行。
https://stackoverflow.com/questions/16650674
复制相似问题