java - AKKA actor创建错误
问题描述
class Master(val host: String, val port: Int) extends Actor 第一种写法val masterActor=new Master(host, port)val master = actorSystem.actorOf(Props(masterActor), 'Master')
Exception in thread 'main' akka.actor.ActorInitializationException: You cannot create an instance of [test.rpc.Master] 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:167)at akka.actor.Actor$class.$init$(Actor.scala:423)at test.rpc.Master.<init>(Master.scala:13)at test.rpc.Master$.main(Master.scala:106)at test.rpc.Master.main(Master.scala)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:606)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
You cannot create an instance of [test.rpc.Master] explicitly using the constructor (new). You have to use one of the ’actorOf’ factory methods to create a new actor.
错误的代码是:val masterActor=new Master(host, port) 这行代码在执行的时候出错了,抛出了异常
第二中写法这种写法就没有错误:val master = actorSystem.actorOf(Props(new Master(host, port)), 'Master')
第二种写法中也会是首先执行new Master(host,port),为什么第二种写法中就没有抛出异常呢???
以上两种写法都是会首先执行new Master的。
已知:actor的创建需要有继承体系,实例是不能直接使用new 来创建的。而是使用系统的actorOf方法来创建,注意Actor之间有层次关系(和人类社会一样)。
问题: 第二中写法也是直接new了actor 就没报错,原因是什么那
main方法主要代码:
def main(args: Array[String]): Unit = { val host = args(0) val port = args(1).toInt println(host) println(port) val config = ...... val actorSystem = ActorSystem('MasterSystem', config)//第一中方式,自己new,结果就是运行不起来,直接报错 val masterActor= new Master(host, port) val master = actorSystem.actorOf(Props(masterActor), 'Master') //第二种方式, 这行代码中也有new Master,但是程序可以正常运行,不会出错val master = actorSystem.actorOf(Props(new Master(host, port)), 'Master') actorSystem.awaitTermination() }
问题解答
回答1:第二种哪里是直接new了,不是actorSystem的工厂方法创建的么?把你的main方法所有代码贴出来
相关文章:
1. html5 - iframe src可以引入其他域名或者IP吧iframe src可以是其他域名过IP吧2. javascript - 给js写的盒子添加css样式,css样式没起作用。3. javascript - 如何将psd裁切下来的图片非常清晰的宣示出来4. javascript - vuex中action应该怎么分发事件5. html5 - 请问一下写H5的时候 你们都是兼容那些手机6. html5 - ElementUI table中el-table-column怎么设置百分比显示。7. mysqli函数8. web前端页面中实现表格效果,这个表格是可编辑的9. javascript - h5页面中iframe无法唤起iOS应用?10. 数据挖掘 - 如何用python实现《多社交网络的影响力最大化问题分析》中的算法?
![php-_server-php_self - nginx $_SERVER[’PHP_SELF’] 得到重复路径是什么原因?](http://www.haobala.com/attached/image/news/202309/1123091c0c.png)
网公网安备