5

下面的类在 new HelloWorld 行导致错误:

Exception in thread "main" akka.actor.ActorInitializationException: You cannot create an instance of [HelloWorld] explicitly using the constructor (new). You have to use one of the 'actorOf' factory methods to create a new actor. See the documentation.
  at akka.actor.ActorInitializationException$.apply(Actor.scala:219)
  at akka.actor.Actor$class.$init$(Actor.scala:436)
  at HelloWorld.<init>(HelloWorld.scala:4)
  at Driver$.main(HelloWorld.scala:38)
  at Driver.main(HelloWorld.scala)

所以我尝试: val hw = actorOf(new HelloWorld) 但这会导致编译器错误:

not found: value actorOf

下面的HelloWorld应该如何实现?

阅读其他 Scala 文档,需要在扩展 Actor 的类中定义一个动作方法,然后在这个类上调用 start 方法,是否有理由使用 actorOf 而不是定义一个动作方法?

下面的课程取自 Scala akka 文档http://doc.akka.io/docs/akka/2.2.0/scala.html

import akka.actor.Actor
import akka.actor.Actor._
import akka.actor.Props

class HelloWorld extends Actor {

  override def preStart(): Unit = {
    // create the greeter actor
    val greeter = context.actorOf(Props[Greeter], "greeter")
    // tell it to perform the greeting
    greeter ! Greeter.Greet
  }
  def receive = {
    // when the greeter is done, stop this actor and with it the application
    case Greeter.Done => context.stop(self)
  }

  object Greeter {   

    case object Greet
    case object Done


  }
  class Greeter extends Actor {
    def receive = {
      case Greeter.Greet =>
        println("Hello World!")
        sender ! Greeter.Done
    }
  }


}

object Driver {

    def main(args: Array[String]) {
      new HelloWorld
    }

}
4

3 回答 3

8

您需要编辑您的主要内容,如下所示。其次,在第 5 行,您需要将其更改为context.actorOf(Props(new Greeter)). 这是因为您Greeter没有apply定义函数,因此您需要自己手动创建 Greeter 对象。

下面的工作代码:

import akka.actor.ActorSystem

class HelloWorld extends Actor {

  override def preStart(): Unit = {
    // create the greeter actor
    val greeter = context.actorOf(Props(new Greeter), "greeter")//line 5
    // tell it to perform the greeting
    greeter ! Greeter.Greet
  }
  def receive = {
    // when the greeter is done, stop this actor and with it the application
    case Greeter.Done => context.stop(self)
  }

  object Greeter {   

    case object Greet
    case object Done


  }
  class Greeter extends Actor {
    def receive = {
      case Greeter.Greet =>
        println("Hello World!")
        sender ! Greeter.Done
    }
  }


}

object Driver {

    def main(args: Array[String]) {
      val system = ActorSystem("Main")
      val ac = system.actorOf(Props[HelloWorld])
    }

}
于 2013-08-03T12:10:54.963 回答
4

如果你想使用你的主类,请执行以下操作:

import akka.actor.{ActorSystem, Props}
object Driver extends App {    
    val system = ActorSystem("System")
    val hw = system.actorOf(Props[HelloWorld], name = "hw")
}

这将创建一个新的演员系统,然后使用该演员系统创建 HelloWorld 演员。

您也可以按照 akka 说明进行操作:将 Akka.Main 设置为主类,并将程序“com.example.HelloWorld”作为参数。

于 2013-08-03T12:10:42.250 回答
2
val greeter = context.actorOf(Props(new Greeter), "greeter")//line 5

我认为您不需要为 Greeter 设置新的关键字。我相信道具已经为你做到了。如果有什么新的应该是

于 2013-10-21T14:26:33.750 回答