Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Akka Streamsで
Actorが作成されるのを確認

Akka Streamsで
Actorが作成されるのを確認

LT駆動開発29の発表資料です。
https://github.com/LTDD/Sessions/wiki/LT%E9%A7%86%E5%8B%95%E9%96%8B%E7%99%BA29

Akka Streamsでどんな風にActorが生成されていくのか気になったので、ActorSystemImpl#printTreeで出力して遊んだ話です。

http://qiita.com/eielh/items/fc8dd83989660460061a

Tomohiko Himura

September 04, 2016
Tweet

More Decks by Tomohiko Himura

Other Decks in Programming

Transcript

  1. アクターの定義 class HelloActor extends akka.actor.Actor { override def receive: Receive

    = { // receiveϝιουΛ࣮૷͢Δඞཁ͕͋Δ case _ => // Կ͔ϝοηʔδ͕͖ͨΒ println("Hello, World"); } }
  2. アクターへのメッセージ送信 • ActorRef ! メッセージ // Ϋϥε͔ΒPropsΛੜ੒ val HelloActorProps =

    akka.actor.Props[HelloActor] // ΞΫλʔγεςϜΛߏங val actorSystem = akka.actor.ActorSystem() // Ϣʔβ༻ͷ਌ΞΫλʔʹPropsΛݩʹΞΫλʔΛੜ੒ɻੜ੒ͨ͠ΞΫλʔͷࢀরΛฦ͢ val helloActorRef = actorSystem.actorOf(HelloActorProps) // ΞΫλʔʹ "world"ͱ͍͏ϝοηʔδΛૹ৴ helloActorRef ! "World" // HelloActorͷΠϯελϯεͰ͸ͳ͍ͷͰɺ receiveϝιου͸௚઀Α΂ͳ͍
  3. 出力してみる // package akka val actorSystem = akka.actor.ActorSystem() // printeNodeΛ͔ͭ͏ͨΊʹແཧ໼ཧActorSystemImplʹ

    val as = actorSystem.asInstanceOf[ActorSystemImpl] println(as.printTree); // ΞΫλʔγεςϜΛ࡞ͬͨ࣌఺ val helloActorRef = actorSystem.actorOf(HelloActorProps) println(as.printTree); // HelloActorΛҰͭ࡞੒ helloActorRef ! "World"
  4. アクターシステムを作った直後 -> / LocalActorRefProvider$$anon$1 class akka.actor.LocalActorRefProvider$Guardian s ⌊-> system LocalActorRef

    class akka.actor.LocalActorRefProvider$SystemGuardian st | ⌊-> deadLetterListener RepointableActorRef class akka.event.DeadLetterListene | ⌊-> eventStreamUnsubscriber-1 RepointableActorRef class akka.event.EventStrea | ⌊-> log1-Logging$DefaultLogger RepointableActorRef class akka.event.Logging$D ⌊-> user LocalActorRef class akka.actor.LocalActorRefProvider$Guardian status=0 n
  5. HelloActorを作った直後 -> / LocalActorRefProvider$$anon$1 class akka.actor.LocalActorRefProvider$Guardian s ⌊-> system LocalActorRef

    class akka.actor.LocalActorRefProvider$SystemGuardian st | ⌊-> deadLetterListener RepointableActorRef class akka.event.DeadLetterListene | ⌊-> eventStreamUnsubscriber-1 RepointableActorRef class akka.event.EventStrea | ⌊-> log1-Logging$DefaultLogger RepointableActorRef class akka.event.Logging$D ⌊-> user LocalActorRef class akka.actor.LocalActorRefProvider$Guardian status=0 1 ⌊-> $a RepointableActorRef class akka.HelloActor status=0 no children
  6. class HelloActor extends akka.actor.Actor { var child: akka.actor.ActorRef = _

    override def receive: Receive = { case str: String => // HelloActorͷதͰΞΫλʔΛੜ੒ child = context.actorOf(HelloActor.props) println(s"Hello, $str") } } object HelloActor { val props = akka.actor.Props[HelloActor] } object Sample extends App { val actorSystem = akka.actor.ActorSystem() val as = actorSystem.asInstanceOf[ActorSystemImpl] val helloActorRef = actorSystem.actorOf(HelloActor.props) println(as.printTree) helloActorRef ! “World” println(as.printTree) }
  7. 子が増えてる -> / LocalActorRefProvider$$anon$1 class akka.actor.LocalActorRefProvider$Guardian s ⌊-> system LocalActorRef

    class akka.actor.LocalActorRefProvider$SystemGuardian st | ⌊-> deadLetterListener RepointableActorRef class akka.event.DeadLetterListene | ⌊-> eventStreamUnsubscriber-1 RepointableActorRef class akka.event.EventStrea | ⌊-> log1-Logging$DefaultLogger RepointableActorRef class akka.event.Logging$D ⌊-> user LocalActorRef class akka.actor.LocalActorRefProvider$Guardian status=0 1 ⌊-> $a RepointableActorRef class akka.HelloActor status=0 1 children ⌊-> $a LocalActorRef class akka.HelloActor status=0 no children
  8. val actorSystem = akka.actor.ActorSystem() val as = actorSystem.asInstanceOf[ActorSystemImpl] println(as.printTree) //

    StreamSupervisorΞΫλʔ͕ੜ੒͞ΕΔ val materializer = akka.stream.ActorMaterializer()(actorSystem) println(as.printTree) // ·ͩͳʹ΋Ͱ͖ͳ͍ val graph = Source.fromIterator(() => Iterator.from(0)).to(Sink.ignore()) println(as.printTree) // ετϦʔϜΛಈ͔ͨ͢ΊʹΞΫλʔ͕Ͱ͖Δ graph.run()(materializer) println(as.printTree)
  9. val actorSystem = akka.actor.ActorSystem() val as = actorSystem.asInstanceOf[ActorSystemImpl] println(as.printTree) //

    StreamSupervisorΞΫλʔ͕ੜ੒͞ΕΔ val materializer = akka.stream.ActorMaterializer()(actorSystem) println(as.printTree) // ·ͩͳʹ΋Ͱ͖ͳ͍ val graph = Source.fromIterator(() => Iterator.from(0)).to(Sink.ignore()) println(as.printTree) // ετϦʔϜΛಈ͔ͨ͢ΊʹΞΫλʔ͕Ͱ͖Δ graph.run()(materializer) println(as.printTree) run されるまではmaterializerが必要ない materializerはActorSystemをもってる materializerはアクターをつくれる
  10. mapで処理を加えてみる val graph = Source.fromIterator(() => Iterator.from(0)) .map(_ * 2)

    .to(Sink.ignore()) ⌊VTFS-PDBM"DUPS3FGDMBTTBLLBBDUPS-PDBM"DUPS3FG1SPWJEFS(VBSEJBOTUBUVTDIJMESFO ⌊4USFBN4VQFSWJTPS3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQM4USFBN4VQFSWJTPST ⌊qPXVOLOPXOPQFSBUJPO3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQMGVTJOH"D
  11. Sink.actorRefを利用 val helloActor = actorSystem.actorOf(HelloActor.props) val graph = Source.fromIterator(() =>

    Iterator.from(0)) .map(_.toString) .to(Sink.actorRef[String](helloActor, ())) ⌊VTFS-PDBM"DUPS3FGDMBTTBLLBBDUPS-PDBM"DUPS3FG1SPWJEFS(VBSEJBOTUBUVTDIJMESFO ⌊B3FQPJOUBCMF"DUPS3FGDMBTTBLLB)FMMP"DUPSTUBUVTOPDIJMESFO ⌊4USFBN4VQFSWJTPS3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQM4USFBN4VQFSWJTPSTUBUVT ⌊qPXVOLOPXOPQFSBUJPO3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQMGVTJOH"DUPS(S ⌊qPXBDUPS3FG4JOL3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQM"DUPS3FG4JOL"DUPSTU
  12. Source.actorRefをつかってみる val helloActor = actorSystem.actorOf(HelloActor.props) val graph = Source.actorRef(1, OverflowStrategy.dropTail)

    .to(Sink.actorRef[String](helloActor, ())) ⌊VTFS-PDBM"DUPS3FGDMBTTBLLBBDUPS-PDBM"DUPS3FG1SPWJEFS(VBSEJBOTUBUVTDIJMESFO ⌊B3FQPJOUBCMF"DUPS3FGDMBTTBLLB)FMMP"DUPSTUBUVTOPDIJMESFO ⌊4USFBN4VQFSWJTPS3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQM4USFBN4VQFSWJTPSTUBUVT ⌊qPXBDUPS3FG4PVSDF3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQM"DUPS3FG4PVSDF"D ⌊qPXBDUPS3FG4JOL3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQM"DUPS3FG4JOL"DUPSTU
  13. 全部やってみる val graph = Source.actorRef[Int](1, OverflowStrategy.dropTail) .map(_*2) .map(_.toString) .to(Sink.actorRef[String](helloActor, ()))

    ⌊VTFS-PDBM"DUPS3FGDMBTTBLLBBDUPS-PDBM"DUPS3FG1SPWJEFS(VBSEJBOTUBUVTDIJMESFO ⌊B3FQPJOUBCMF"DUPS3FGDMBTTBLLB)FMMP"DUPSTUBUVTOPDIJMESFO ⌊4USFBN4VQFSWJTPS3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQM4USFBN4VQFSWJTPSTUBUVT ⌊qPXVOLOPXOPQFSBUJPO3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQMGVTJOH"DUPS(SBQI ⌊qPXBDUPS3FG4PVSDF3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQM"DUPS3FG4PVSDF"DUP ⌊qPXBDUPS3FG4JOL3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQM"DUPS3FG4JOL"DUPSTUBUV