{"id":1000,"date":"2014-08-14T23:13:28","date_gmt":"2014-08-14T15:13:28","guid":{"rendered":"http:\/\/coderbee.net\/?p=1000"},"modified":"2014-08-26T22:24:31","modified_gmt":"2014-08-26T14:24:31","slug":"akka-actor-%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/akka\/20140814\/1000","title":{"rendered":"Akka Actor \u751f\u547d\u5468\u671f"},"content":{"rendered":"<p>\u672c\u6587\u4e3b\u8981\u5c55\u793a Akka Actor \u7684\u751f\u547d\u5468\u671f\u7ba1\u7406\u548c\u9ed8\u8ba4\u7684\u76d1\u7ba1\u7b56\u7565\u3002\u57fa\u4e8e Akka 2.3.4\uff0cScala 2.11\u3002<\/p>\n<h3>Akka Actor \u751f\u547d\u5468\u671f\u94a9\u5b50<\/h3>\n<p>Actor \u5b9e\u4f8b\u5316\u540e\u5c31\u7531 Actor \u8fd0\u884c\u65f6\u8c03\u5ea6\u6267\u884c\u3002<\/p>\n<p>Akka Actor \u5b9a\u4e49\u4e86\u4e0b\u5217\u7684\u751f\u547d\u5468\u671f\u56de\u8c03\u94a9\u5b50\uff1a<\/p>\n<ul>\n<li>preStart\uff1a\u5728 actor \u5b9e\u4f8b\u5316\u540e\u6267\u884c\uff0c\u91cd\u542f\u65f6\u4e0d\u4f1a\u6267\u884c\u3002<\/li>\n<li>postStop\uff1a\u5728 actor \u6b63\u5e38\u7ec8\u6b62\u540e\u6267\u884c\uff0c\u5f02\u5e38\u91cd\u542f\u65f6\u4e0d\u4f1a\u6267\u884c\u3002<\/li>\n<li>preRestart\uff1a\u5728 actor \u5f02\u5e38\u91cd\u542f\u524d\u4fdd\u5b58\u5f53\u524d\u72b6\u6001\u3002<\/li>\n<li>postRestart\uff1a\u5728 actor \u5f02\u5e38\u91cd\u542f\u540e\u6062\u590d\u91cd\u542f\u524d\u4fdd\u5b58\u7684\u72b6\u6001\u3002<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<h3>\u4f8b\u5b50<\/h3>\n<p>\u4e09\u4e2a\u6e90\u6587\u4ef6\uff1a<\/p>\n<p>Main.scala\uff1a\u521d\u59cb\u5316\u3001\u53d1\u9001\u6d88\u606f\u7ed9Actor\u3002<br \/>\nParentActor.scala \uff1a\u63a5\u6536\u4ece Main.scala \u91cc\u53d1\u9001\u7684\u6d88\u606f\uff0c\u521d\u59cb\u5316\u6216\u63a7\u5236 ChildActor\u3002<br \/>\nChildActor.scala\uff1a\u5b50 Actor\uff0c\u88ab ParentActor \u63a7\u5236\u3002<\/p>\n<h4>ChildActor.scala<\/h4>\n<pre><code class=\"scala\">package net.coderbee.akka.simple\n\nimport akka.actor.Actor\n\nclass ChildActor extends Actor {\n\n    override def receive() = {\n        case \"abc\" =&gt; println(\"get abc string \")\n        case \"exception\" =&gt; throw new NullPointerException()\n        case _ =&gt; println(\"children cann't handle unknown message\")\n    }\n\n    override def preStart {\n        println(\"actor:\" + self.path + \",child preStart .\")\n    }\n\n    override def postStop {\n        println(\"actor:\" + self.path + \",child postStop .\")\n    }\n\n    override def preRestart(reason: Throwable, message: Option[Any]) {\n        println(\"actor:\" + self.path + \",preRestart child, reason:\" + reason + \", message:\" + message)\n    }\n\n    override def postRestart(reason: Throwable) {\n        println(\"actor:\" + self.path + \",postRestart child, reason:\" + reason)\n    }\n}\n<\/code><\/pre>\n<h4>ParentActor.scala<\/h4>\n<pre><code class=\"scala\">package net.coderbee.akka.simple\n\nimport akka.actor.Actor\nimport akka.actor.ActorLogging\nimport akka.actor.Props\nimport akka.actor.ReceiveTimeout\nimport scala.concurrent.duration\nimport akka.actor.ActorSystem\n\nclass ParentActor extends Actor with ActorLogging {\n    println(\"start myactor \")\n\n\/\/    context.setReceiveTimeout(duration.Duration.apply(30, duration.SECONDS))\n    \/\/    val log = Logging(context.system, this)\n\n    def receive = {\n        case \"test\" =&gt; log.info(\"received test\")\n        case (\"newChild\", name: String) =&gt; context.actorOf(Props[ChildActor], name)\n        case (\"stop\", name: String) =&gt; {\n            val child = context.actorFor(self.path + \"\/\" + name);\n            context.stop(child)\n        }\n\/\/        case ReceiveTimeout =&gt; throw new RuntimeException(\"received timeout\"); \/\/ \u6bcf\u9694\u8d85\u65f6\u65f6\u95f4\u6ca1\u6536\u5230\u6d88\u606f\u5c31\u629b\u51fa\u5f02\u5e38\n        case \"suicide\" =&gt; \n        case x: Any =&gt; log.info(\"received unknown message :\" + x)\n    }\n\n    \/**\n     * \u5728 actor \u5b9e\u4f8b\u5316\u540e\u6267\u884c\uff0c\u91cd\u542f\u65f6\u4e0d\u4f1a\u6267\u884c\n     *\/\n    override def preStart {\n        println(\"actor:\" + self.path + \", parent preStart \")\n    }\n\n    \/**\n     * \u5728 actor \u6b63\u5e38\u7ec8\u6b62\u540e\u6267\u884c\uff0c\u5f02\u5e38\u91cd\u542f\u65f6\u4e0d\u4f1a\u6267\u884c\u3002\n     *\/\n    override def postStop {\n        println(\"actor:\" + self.path + \",parent postStop .\")\n    }\n\n    \/**\n     * \u5728 actor \u5f02\u5e38\u91cd\u542f\u524d\u4fdd\u5b58\u72b6\u6001\n     *\/\n    override def preRestart(reason: Throwable, message: Option[Any]) {\n        println(\"actor:\" + self.path + \", preRestart parent, reason:\" + reason + \", message:\" + message)\n    }\n\n    \/**\n     * \u5728 actor \u5f02\u5e38\u91cd\u542f\u540e\u6062\u590d\u72b6\u6001\n     *\/\n    override def postRestart(reason: Throwable) {\n        println(\"actor:\" + self.path + \", postRestart parent, reason:\" + reason)\n    }\n}\n<\/code><\/pre>\n<h4>Main.scala<\/h4>\n<pre><code class=\"scala\">package net.coderbee.akka.simple\n\nimport akka.actor.ActorSystem\nimport akka.actor.Props\nimport akka.actor.Actor\nimport akka.actor.Kill\nimport scala.actors.threadpool.TimeUnit\n\nobject Main extends App {\n    val sysname = \"mysystem\"\n    val system = ActorSystem(\"mysystem\")\n\n    val myActor = system.actorOf(Props[ParentActor], name = \"myactor\")\n\n    myActor ! (\"newChild\", \"child-1\")\n    myActor ! (\"newChild\", \"child-2\")\n    myActor ! \"test\"\n\n    val parent = system.actorFor(\"akka:\/\/mysystem\/user\/myactor\")\n    parent ! \"test\"\n\/\/    parent ! (\"stop\", \"child-2\")\n\n    val child2 = system.actorSelection(\"akka:\/\/mysystem\/user\/myactor\/child-2\")\n    child2 ! Kill     \/\/ \u6740\u6b7b child2\n\/\/    child2 ! \"exception\"\n    Thread.sleep(5000)     \/\/ \u7b49\u5f85 child2  \u88ab\u6740\u6b7b\n    myActor ! (\"newChild\", \"child-2\")\n\/\/    Thread.sleep(5000)\n\/\/    myActor ! (\"newChild\", \"child-2\")\n}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\uff08\u6b63\u5e38\u505c\u6b62\u3001\u542f\u52a8 Actor\uff09\u7684\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<pre><code>start myactor \nactor:akka:\/\/mysystem\/user\/myactor, parent preStart \nactor:akka:\/\/mysystem\/user\/myactor\/child-1,child preStart .\nactor:akka:\/\/mysystem\/user\/myactor\/child-2,child preStart .\n[INFO] [08\/14\/2014 22:58:34.214] [mysystem-akka.actor.default-dispatcher-5] [akka:\/\/mysystem\/user\/myactor] received test\n[INFO] [08\/14\/2014 22:58:34.214] [mysystem-akka.actor.default-dispatcher-5] [akka:\/\/mysystem\/user\/myactor] received test\n[ERROR] [08\/14\/2014 22:58:34.230] [mysystem-akka.actor.default-dispatcher-2] [akka:\/\/mysystem\/user\/myactor\/child-2] Kill (akka.actor.ActorKilledException)\nactor:akka:\/\/mysystem\/user\/myactor\/child-2,child postStop .\nactor:akka:\/\/mysystem\/user\/myactor\/child-2,child preStart .\n<\/code><\/pre>\n<p>\u5728 Main.scala \u91cc\uff0c\u5982\u679c\u4e0d\u7b49\u7684 child-2 \u88ab\u6740\u6b7b\u5c31\u521b\u5efa\u540c\u540d\u7684 actor\u5c06\u5bfc\u81f4\u540d\u4e3a myactor \u7684\u7236 actor \u5f02\u5e38\uff0c\u4f7f\u5b83\u91cd\u542f\uff0c\u800c\u6839\u636e Akka Actor \u7684\u76d1\u7ba1\u7b56\u7565\uff0c\u5b83\u4e5f\u4f1a\u91cd\u542f\u5b83\u7684\u5b50 Actor\uff0c\u6240\u4ee5 child-1 \u548cchild-2 \u4e5f\u4f1a\u88ab\u91cd\u542f\uff0c\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<pre><code>start myactor \nactor:akka:\/\/mysystem\/user\/myactor, parent preStart \nactor:akka:\/\/mysystem\/user\/myactor\/child-1,child preStart .\nactor:akka:\/\/mysystem\/user\/myactor\/child-2,child preStart .\n[INFO] [08\/14\/2014 22:59:35.742] [mysystem-akka.actor.default-dispatcher-4] [akka:\/\/mysystem\/user\/myactor\/child-2] Message [akka.actor.Kill$] from Actor[akka:\/\/mysystem\/deadLetters] to Actor[akka:\/\/mysystem\/user\/myactor\/child-2] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.\n[INFO] [08\/14\/2014 22:59:35.745] [mysystem-akka.actor.default-dispatcher-7] [akka:\/\/mysystem\/user\/myactor] received test\n[INFO] [08\/14\/2014 22:59:35.745] [mysystem-akka.actor.default-dispatcher-7] [akka:\/\/mysystem\/user\/myactor] received test\n[ERROR] [08\/14\/2014 22:59:35.759] [mysystem-akka.actor.default-dispatcher-2] [akka:\/\/mysystem\/user\/myactor] actor name [child-2] is not unique!\nakka.actor.InvalidActorNameException: actor name [child-2] is not unique!\n     at akka.actor.dungeon.ChildrenContainer$NormalChildrenContainer.reserve(ChildrenContainer.scala:130)\n     at akka.actor.dungeon.Children$class.reserveChild(Children.scala:77)\n     at akka.actor.ActorCell.reserveChild(ActorCell.scala:369)\n     at akka.actor.dungeon.Children$class.makeChild(Children.scala:202)\n     at akka.actor.dungeon.Children$class.actorOf(Children.scala:38)\n     at akka.actor.ActorCell.actorOf(ActorCell.scala:369)\n     at net.coderbee.akka.simple.ParentActor$$anonfun$receive$1.applyOrElse(ParentActor.scala:18)\n     at akka.actor.Actor$class.aroundReceive(Actor.scala:465)\n     at net.coderbee.akka.simple.ParentActor.aroundReceive(ParentActor.scala:10)\n     at akka.actor.ActorCell.receiveMessage(ActorCell.scala:516)\n     at akka.actor.ActorCell.invoke(ActorCell.scala:487)\n     at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:238)\n     at akka.dispatch.Mailbox.run(Mailbox.scala:220)\n     at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:393)\n     at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)\n     at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)\n     at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)\n     at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)\n\nactor:akka:\/\/mysystem\/user\/myactor, preRestart parent, reason:akka.actor.InvalidActorNameException: actor name [child-2] is not unique!, message:Some((newChild,child-2))\nstart myactor \nactor:akka:\/\/mysystem\/user\/myactor, postRestart parent, reason:akka.actor.InvalidActorNameException: actor name [child-2] is not unique!\nactor:akka:\/\/mysystem\/user\/myactor\/child-2,preRestart child, reason:akka.actor.InvalidActorNameException: actor name [child-2] is not unique!, message:None\nactor:akka:\/\/mysystem\/user\/myactor\/child-1,preRestart child, reason:akka.actor.InvalidActorNameException: actor name [child-2] is not unique!, message:None\nactor:akka:\/\/mysystem\/user\/myactor\/child-1,postRestart child, reason:akka.actor.InvalidActorNameException: actor name [child-2] is not unique!\nactor:akka:\/\/mysystem\/user\/myactor\/child-2,postRestart child, reason:akka.actor.InvalidActorNameException: actor name [child-2] is not unique!\n<\/code><\/pre>\n<p>\u4ece\u8f93\u51fa\u53ef\u4ee5\u770b\u5230\uff1a\u7236 Actor \u9996\u5148\u8c03\u7528 preRestart \uff0c\u7136\u540e \u88ab\u5b9e\u4f8b\u5316\uff0c\u518d\u8c03\u7528 postRestart\uff0c\u6700\u540e\u518d\u91cd\u542f\u5b83\u7684\u5b50 Actor\uff0c\u5b50 Actor \u4e5f\u9075\u5faa\u4e0a\u8ff0\u7684\u6b65\u9aa4\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4e3b\u8981\u5c55\u793a Akka Actor \u7684\u751f\u547d\u5468\u671f\u7ba1\u7406\u548c\u9ed8\u8ba4\u7684\u76d1\u7ba1\u7b56\u7565\u3002\u57fa\u4e8e Akk &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/akka\/20140814\/1000\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[175,235,184],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1000"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1000"}],"version-history":[{"count":3,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1000\/revisions"}],"predecessor-version":[{"id":1003,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1000\/revisions\/1003"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}