如何开始Scala akka演员
发布时间:2020-12-16 18:45:01 所属栏目:安全 来源:网络整理
导读:下面的类在新的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 met
下面的类在新的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的类中定义act方法,然后在此类上调用start方法,是否有理由使用actorOf而不是定义act方法? 下面的课程来自Scala akka docs 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 } } 解决方法
您需要编辑主页,如下所示.其次,在第5行中,您需要将其更改为context.actorOf(Props(new Greeter)).这是因为您的Greeter没有定义应用函数,因此您需要自己手动创建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]) } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |