Scalaåå¼·æ¥èª - Akka ãã®ï¼
Scala勉強日誌 - Actor - 成らぬは人の為さぬなりけり
大ååã«Actorã®åå¼·ãã¦ãç¶ãã§Akkaã®åå¼·ãããã¨æã£ã¦ã¦ãå®å
¨ã«å¿ãã¦ããã®ã§ã
åéãããã¨æãã¾ãã
ï¼ä»äºã§å¿
è¦ã«ãªã£ã¦ãåå¼·ããã®ã§ãã¡ã¢ãã¦ããã ããããï¼
ä»æ¥ã®ãã¼ãã¯ããã
- SBTããã¸ã§ã¯ããä½ã
- Akkaã®Actorãæ¸ãã¦ã¿ã
- ActorããActorãå¼ã³åºãã¦ãçµæãåãåã£ã¦ã¿ã
- Routerã使ã£ã¦ã¿ãã
yagince/akka_practice · GitHub
ä¾ã«ãã£ã¦ãç°å¢ã¯ããã
- OSï¼MacOSX10.8
- Scala : 2.10.0
- sbt :0.12.2
- Akka : 2.1.0
SBTããã¸ã§ã¯ããä½ã
ã¾ãã¯ãããã¸ã§ã¯ããä½ãã¾ãã
ä»åã¯ãå
¨é¨ãã«ãå®ç¾©ãã¡ã¤ã«ã¯å
¨é¨Scalaã§æ¸ãããã£ãã®ã§ã
ãããªæãã§ä½æ
project/Build.scala
import sbt._ import Keys._ object BuildSettings { val buildOrganization = "yagince" val buildVersion = "0.0.1" val buildScalaVersion = "2.10.0" val buildSettings = Defaults.defaultSettings ++ Seq ( organization := buildOrganization, version := buildVersion, scalaVersion := buildScalaVersion ) } object Resolvers { val typeSafe = "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/" val otherResolvers = Seq(typeSafe) } object Dependencies { val akkaCore = "com.typesafe.akka" %% "akka-actor" % "2.1.0" } object AkkaPracticeBuild extends Build { import Resolvers._ import Dependencies._ import BuildSettings._ val dependencies = Seq ( akkaCore ) val project = Project ( "akka-practice", file("."), settings = buildSettings ++ Seq ( resolvers := otherResolvers, libraryDependencies ++= dependencies ) ) }
project/build.properties
sbt.version=0.12.2
Akkaã®Actorãæ¸ãã¦ã¿ã
ã§ã¯ãæ©éæ¸ãã¦ã¿ã¾ãã
src/main/scala/akka/sample/AkkaExample01.scala
package akka.sample import actor.PrintActor import akka.actor.{Props, ActorSystem} object AkkaExample01 extends App { val system = ActorSystem("sample") val actor = system.actorOf(Props[PrintActor], "hoge") actor ! "HelloWorld!" system.shutdown }
package akka.sample import actor.PrintActor import akka.actor.{Props, ActorSystem} object AkkaExample01 extends App { val system = ActorSystem("sample") val actor = system.actorOf(Props[PrintActor], "hoge") actor ! "HelloWorld!" system.shutdown }
src/main/scala/akka/sample/actor/PrintActor.scala
package akka.sample.actor import akka.actor.Actor class PrintActor extends Actor { def receive = { case x => println(x) } }
å®è¡çµæ
> run-main akka.sample.AkkaExample01 [info] Running akka.sample.AkkaExample01 HelloWorld! [success] Total time: 0 s, completed 2013/02/13 22:16:22
åç´ã«æ¸¡ãããªãã¸ã§ã¯ããåºåããã ãã®ã¢ã¯ã¿ã¼ã§ãã
ããã ãè¦ã¦ããscalaæ¨æºActorããè¨è¿°éãå°ãªãã§ããã
ããã¦ãã·ã³ãã«ï¼
ç´ æ´ãããã
ActorããActorãå¼ãã§çµæãåãåã£ã¦ã¿ã
Scalaæ¨æºActorã®åæã¡ã½ããã¿ããã®ããã®ããªï¼ï¼
ç¡ãããï¼ï¼ï¼ã¨æã£ã¦ãããã
ãããªé¢¨ã«ããã®ãæ®éãªã®ããªï¼ï¼ï¼
ã¨ããããã§æ¸ãã¦ã¿ã¾ãã
package akka.sample import akka.actor.{ActorSystem, Actor, Props} import akka.routing.RoundRobinRouter object AkkaExample02 extends App { val system = ActorSystem("sample") val actor = system.actorOf(Props[Master]) actor ! 100 Thread.sleep(500) system.shutdown } class Master extends Actor { val actor = context.actorOf(Props[DoubleActor]) def receive = { case i:Int => actor ! i case Doubled(x) => println("received : %d".format(x)) } } class DoubleActor extends Actor { def receive = { case i:Int => sender ! Doubled(i*2) } } case class Doubled(i:Int)
senderã¸çµæãè¿ãã¦ãå¼ã³åºãå´ã®receiveã¸ã¡ãã»ã¼ã¸ããã·ã³ã°ããæãã§ãããï¼
確ãã«ãããã¯ã·ã³ãã«ã§ç¶ºéºã ã
ããããèããã¨ãåæã¡ã½ããã£ã¦å¿
è¦ãªãããããªããããããã
Routerã使ã£ã¦ã¿ã
Akkaã«ã¯Routerã¨ããæ©è½ãããããã§ãã
Actorã®ã¤ã³ã¹ã¿ã³ã¹ã管çãã¦ã
Routerã«ã¡ãã»ã¼ã¸éä¿¡ããã¨ã
Actorã¸ãããªã«ãã©ã³ã·ã³ã°ããªããã¡ãã»ã¼ã¸ã横æµããã¦ããããããªæãã§ããããï¼
æ¸ãã¦ã¿ã¾ãã
package akka.sample import actor.{Doubled, DoubleActor} import akka.actor.{ActorSystem, Actor, Props} import akka.routing.RoundRobinRouter object AkkaExample02 extends App { val system = ActorSystem("sample") val actor = system.actorOf(Props[Master]) (0 to 10).foreach(actor ! _) Thread.sleep(100) system.shutdown } class Master extends Actor { val router = context.actorOf(Props[DoubleActor].withRouter(RoundRobinRouter(2))) def receive = { case i:Int => router ! i case Doubled(x) => println("received : %d".format(x)) } }
> run-main akka.sample.AkkaExample02 [info] Running akka.sample.AkkaExample02 received : 0 received : 2 received : 6 received : 10 received : 4 received : 14 received : 8 received : 18 received : 12 received : 16 received : 20 [success] Total time: 0 s, completed 2013/02/13 22:27:41
ä¸ååã®ä¾ã®Masterã¯ã©ã¹ãæ¸ãæãã¦ã
DoubleActorãRouterã§ç®¡çããããã«ãã¦ã¿ã¾ããã
Routerã®ç¨®é¡ã¯
- akka.routing.RoundRobinRouter
- akka.routing.RandomRouter
- akka.routing.SmallestMailboxRouter
- akka.routing.BroadcastRouter
- akka.routing.ScatterGatherFirstCompletedRouter
- akka.routing.ConsistentHashingRouter
Routing (Scala) — Akka Documentation
ããã ãããããã§ãã
ããããã®éãã¯ãã¾ã次åã¨ãããã¨ã§ããã
ãã¦ãä»æ¥ã¯ããã¾ã§ã«ãã¾ãã