如何开始一个 Scala akka 演员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18032455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start a Scala akka actor
提问by blue-sky
Below class is causing an error at line new HelloWorld :
下面的类在 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)
So I try : val hw = actorOf(new HelloWorld)But this causes a compiler error :
所以我尝试: val hw = actorOf(new HelloWorld)但这会导致编译器错误:
not found: value actorOf
How should HelloWorld below be implemented ?
下面的HelloWorld应该如何实现?
Reading other Scala docs an act method is requried to be defined within the class that extends Actor and then invoke the start method on this class, is there a reason for using actorOf instead of defining an act method ?
阅读其他 Scala 文档,需要在扩展 Actor 的类中定义一个行为方法,然后在这个类上调用 start 方法,是否有理由使用 actorOf 而不是定义一个行为方法?
Below class is taken from Scala akka docs http://doc.akka.io/docs/akka/2.2.0/scala.html:
下面的课程取自 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
}
}
回答by Jatin
You need to edit your main as shown below. Secondly, in line-5, you need to change it to context.actorOf(Props(new Greeter)). This is because your Greeterdoes not have applyfunction defined, hence you need to manually create Greeter object yourself.
您需要编辑您的主要内容,如下所示。其次,在第 5 行,您需要将其更改为context.actorOf(Props(new Greeter)). 这是因为您Greeter没有apply定义函数,因此您需要自己手动创建 Greeter 对象。
Working code below:
工作代码如下:
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])
}
}
回答by some some
If you want to use your main class do the following:
如果要使用主类,请执行以下操作:
import akka.actor.{ActorSystem, Props}
object Driver extends App {
val system = ActorSystem("System")
val hw = system.actorOf(Props[HelloWorld], name = "hw")
}
Which will create a new actor system and then create the HelloWorld actor using that actor system.
这将创建一个新的演员系统,然后使用该演员系统创建 HelloWorld 演员。
You can also follow the akka instructions: set Akka.Main as the main class and give the program "com.example.HelloWorld" as argument.
您还可以按照 akka 说明进行操作:将 Akka.Main 设置为主类,并将程序“com.example.HelloWorld”作为参数。
回答by Shiva Komat
val greeter = context.actorOf(Props(new Greeter), "greeter")//line 5
I don't think you need to have a new keyword there for Greeter. I believe the Props does that for you already. If anything having a new should be a
我认为您不需要为 Greeter 设置一个新关键字。我相信 Props 已经为你做到了。如果任何有新的东西应该是

