【问题标题】:Instantiation in AKKA MicrokernelAKKA 微内核中的实例化
【发布时间】:2013-05-15 01:54:05
【问题描述】:

按照文档示例 (2.1.4),我在加载微内核的 actor 处理消息时遇到了问题,其中 Bootable 扩展类定义如下:

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 中提供的代码,我已将服务器端变成了微内核托管的 actor。

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")
}

【问题讨论】:

  • 我认为您不需要虚拟实例化。第一个示例存在什么类型的问题?
  • 虽然在第一个示例中连接成功,但随后停止两侧之一时的错误消息显示,没有任何消息被处理。一旦进行了虚拟实例化,无需进一步更改代码或配置文件,接收中的案例就会被命中并处理。
  • 如果您的HelloActor 代码与微内核示例不同,您能否发布它?
  • 我已经编辑了问题,添加了一个代码示例。

标签: scala akka microkernel


【解决方案1】:

如果您发布的代码确实准确,那么只需将joe 实例的实例移动到startup 操作中,而不是在可启动类的构造函数中:

def startup = {
  system.actorOf(Props[Joe], name = "joe")
}

与名称joe 绑定的actor 需要先启动,然后才能通过名称查找它并向其发送消息。这与在可引导类的构造函数中启动它基本上是一样的,但我相信约定要求在 startup 函数中进行所有参与者实例化,而不是可引导类主体(以及构造函数)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-13
    • 2018-03-06
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多