java - AKKA actor創(chuàng)建錯(cuò)誤
問(wèn)題描述
class Master(val host: String, val port: Int) extends Actor 第一種寫(xiě)法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.
錯(cuò)誤的代碼是:val masterActor=new Master(host, port) 這行代碼在執(zhí)行的時(shí)候出錯(cuò)了,拋出了異常
第二中寫(xiě)法這種寫(xiě)法就沒(méi)有錯(cuò)誤:val master = actorSystem.actorOf(Props(new Master(host, port)), 'Master')
第二種寫(xiě)法中也會(huì)是首先執(zhí)行new Master(host,port),為什么第二種寫(xiě)法中就沒(méi)有拋出異常呢???
以上兩種寫(xiě)法都是會(huì)首先執(zhí)行new Master的。
已知:actor的創(chuàng)建需要有繼承體系,實(shí)例是不能直接使用new 來(lái)創(chuàng)建的。而是使用系統(tǒng)的actorOf方法來(lái)創(chuàng)建,注意Actor之間有層次關(guān)系(和人類(lèi)社會(huì)一樣)。
問(wèn)題: 第二中寫(xiě)法也是直接new了actor 就沒(méi)報(bào)錯(cuò),原因是什么那
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,結(jié)果就是運(yùn)行不起來(lái),直接報(bào)錯(cuò) val masterActor= new Master(host, port) val master = actorSystem.actorOf(Props(masterActor), 'Master') //第二種方式, 這行代碼中也有new Master,但是程序可以正常運(yùn)行,不會(huì)出錯(cuò)val master = actorSystem.actorOf(Props(new Master(host, port)), 'Master') actorSystem.awaitTermination() }
問(wèn)題解答
回答1:第二種哪里是直接new了,不是actorSystem的工廠方法創(chuàng)建的么?把你的main方法所有代碼貼出來(lái)
相關(guān)文章:
1. macos - mac下docker如何設(shè)置代理2. javascript - 學(xué)習(xí)網(wǎng)頁(yè)開(kāi)發(fā),關(guān)于head區(qū)域一段腳本的疑惑3. javascript - JS設(shè)置Video視頻對(duì)象的currentTime時(shí)出現(xiàn)了問(wèn)題,IE,Edge,火狐,都可以設(shè)置,反而chrom卻...4. angular.js - ng-grid 和tabset一起用時(shí),grid width默認(rèn)特別小5. javascript - 如何獲取未來(lái)元素的父元素在頁(yè)面中所有相同元素中是第幾個(gè)?6. Android下,rxJava+retrofit 并發(fā)上傳文件和串行上傳文件的效率為什么差不多?7. 熱切期待朱老師的回復(fù),網(wǎng)頁(yè)視頻在線播放器插件配置錯(cuò)誤8. mysql - AttributeError: ’module’ object has no attribute ’MatchType’9. javascript - 從mysql獲取json數(shù)據(jù),前端怎么處理轉(zhuǎn)換解析json類(lèi)型10. Whitelabel錯(cuò)誤頁(yè)面發(fā)生意外錯(cuò)誤(類(lèi)型=未找到,狀態(tài)= 404)/WEB-INF/views/home.jsp
