Upload
yusuke-kuoka
View
2.051
Download
0
Embed Size (px)
Citation preview
Basics of
Akka
Akka is a toolkit to build *concurrent, *distributed, *fault torerant apps.
Akka is a toolkit to build *concurrent, *distributed, *fault torerant apps.
並行計算したり分散処理したり耐障害性があるような
concurrent -> Actorsdistributed -> Remotingfault torerant -> Supervision
concurrent -> Actorsdistributed -> Remotingfault torerant -> Supervision
アクター
リモートアクター
監視
Actors
Merchant
Warrior
Actors States
Messages
Each actor runsconcurrently
Actors communiate through messages
Each actor has state
Actors are scalable
Too much load!
I scaaale out!
Remoting
Server1
It’s O.K.
Server1
Out of cap
Server2Server1
Remoting
Ditribute across servers
Supervision
Supervisor
Children
exceptionexception
restart!restart!
Some code
import akka.actor._import akka.util.Timeoutimport akka.pattern.askimport concurrent.duration._import concurrent.Futureimport concurrent.ExecutionContext.Implicits.global
case class Pay(amount: Int)
case class Apple()
class Merchant extends Actor { var gold: Int = 0 def receive = { case Pay(amount) if amount >= 50 => gold += 50 sender ! Apple() }}
val system = ActorSystem()val merchant: ActorRef = system.actorOf(Props(new Merchant))
val future: Future[Apple] = (merchant ? Pay(50)).mapTo[Apple]
// Callbacks
future.onSuccess { case apple: Apple => // Eat it}
future.onFailure { case failure => println("onFailure called with: " + failure)}
// Wait for the reply
import concurrent.Await
val result: Apple = Await.result(future, atMost = 10 seconds)
// for-comprehensions
for { apple <- future foo <- anotherFuture} yield doSomething(apple, foo)
See:
SIP-14 - Futures and Promises
http://docs.scala-lang.org/sips/pending/futures-promises.html
Scalingout?
val merchant: ActorRef = system.actorOf(Props(new Merchant))
val router: ActorRef = system.actorOf( Props[Merchant] .withRouter( SmallestMailboxRouter( nrOfInstances = 10 ) ), name = "merchantRouter" )
val future: Future[Apple] = (merchant ? Pay(50)).mapTo[Apple]
val future: Future[Apple] = (router ? Pay(50)).mapTo[Apple]
Excercise
“Hi!”
“Hi!”
“Hi!”
class EchoActor extends Actor { def receive = { case <1> => sender ! <2> }}
“Hi!”
“Hi!”
class EchoActor extends Actor { def receive = { case msg => sender ! msg }}
“Hi!”
“Hi!”
Further topics
Akka is composed of many tools:
actor, testkit, remote, cluster, transactor, agent, zeromq, camel, ...
Just use what you need
Read it now!
http://www.manning.com/roestenburg/
Thank you!