如何开始一个Scala akka演员

如何开始一个Scala akka演员,scala,akka,Scala,Akka,下面的类在新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 acto

下面的类在新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)
下面的HelloWorld应该如何实现

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

以下课程摘自Scala-akka文档:


如果要使用主类,请执行以下操作:

import akka.actor.{ActorSystem, Props}
object Driver extends App {    
    val system = ActorSystem("System")
    val hw = system.actorOf(Props[HelloWorld], name = "hw")
}
这将创建一个新的actor系统,然后使用该actor系统创建HelloWorld actor

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

您需要编辑Main,如下所示。第二,在第5行中,您需要将其更改为
context.actorOf(Props(新迎宾者))
。这是因为您的
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])
    }

}

我认为你不需要为Greeter添加新的关键字。我相信道具已经为你做到了。如果有新的act()和start()属于旧的和不推荐使用的Scala actors,则Akka是另一个actor实现,请参阅位于的文档。@RolandKuhn感谢链接,经过一些挖掘,此文档证明是有用的:
import akka.actor.{ActorSystem, Props}
object Driver extends App {    
    val system = ActorSystem("System")
    val hw = system.actorOf(Props[HelloWorld], name = "hw")
}
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])
    }

}
val greeter = context.actorOf(Props(new Greeter), "greeter")//line 5