53
Basics of Akka

Basics of Akka

Embed Size (px)

Citation preview

Page 1: Basics of Akka

Basics of

Akka

Page 2: Basics of Akka

Akka is a toolkit to build *concurrent, *distributed, *fault torerant apps.

Page 3: Basics of Akka

Akka is a toolkit to build *concurrent, *distributed, *fault torerant apps.

並行計算したり分散処理したり耐障害性があるような

Page 4: Basics of Akka

concurrent -> Actorsdistributed -> Remotingfault torerant -> Supervision

Page 5: Basics of Akka

concurrent -> Actorsdistributed -> Remotingfault torerant -> Supervision

アクター

リモートアクター

監視

Page 6: Basics of Akka

Actors

Page 7: Basics of Akka
Page 8: Basics of Akka

Merchant

Warrior

Page 9: Basics of Akka
Page 10: Basics of Akka
Page 11: Basics of Akka
Page 12: Basics of Akka
Page 13: Basics of Akka
Page 14: Basics of Akka

Actors States

Messages

Page 15: Basics of Akka

Each actor runsconcurrently

Page 16: Basics of Akka

Actors communiate through messages

Page 17: Basics of Akka

Each actor has state

Page 18: Basics of Akka

Actors are scalable

Page 19: Basics of Akka

Too much load!

Page 20: Basics of Akka

I scaaale out!

Page 21: Basics of Akka

Remoting

Page 22: Basics of Akka

Server1

It’s O.K.

Page 23: Basics of Akka

Server1

Out of cap

Page 24: Basics of Akka

Server2Server1

Remoting

Ditribute across servers

Page 25: Basics of Akka

Supervision

Page 26: Basics of Akka
Page 27: Basics of Akka

Supervisor

Children

Page 28: Basics of Akka

exceptionexception

Page 29: Basics of Akka

restart!restart!

Page 30: Basics of Akka

Some code

Page 31: Basics of Akka

import akka.actor._import akka.util.Timeoutimport akka.pattern.askimport concurrent.duration._import concurrent.Futureimport concurrent.ExecutionContext.Implicits.global

Page 32: Basics of Akka

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()  }}

Page 33: Basics of Akka

val system = ActorSystem()val merchant: ActorRef = system.actorOf(Props(new Merchant))

Page 34: Basics of Akka

val future: Future[Apple] = (merchant ? Pay(50)).mapTo[Apple]

Page 35: Basics of Akka

// Callbacks

future.onSuccess { case apple: Apple =>    // Eat it}

future.onFailure { case failure =>    println("onFailure called with: " + failure)}

Page 36: Basics of Akka

// Wait for the reply

import concurrent.Await

val result: Apple = Await.result(future, atMost = 10 seconds)

Page 37: Basics of Akka

// for-comprehensions

for { apple <- future foo <- anotherFuture} yield doSomething(apple, foo)

Page 39: Basics of Akka

Scalingout?

Page 40: Basics of Akka

val merchant: ActorRef = system.actorOf(Props(new Merchant))

Page 41: Basics of Akka

val router: ActorRef = system.actorOf( Props[Merchant] .withRouter( SmallestMailboxRouter( nrOfInstances = 10 ) ), name = "merchantRouter" )

Page 42: Basics of Akka

val future: Future[Apple] = (merchant ? Pay(50)).mapTo[Apple]

Page 43: Basics of Akka

val future: Future[Apple] = (router ? Pay(50)).mapTo[Apple]

Page 44: Basics of Akka

Excercise

Page 45: Basics of Akka

“Hi!”

Page 46: Basics of Akka

“Hi!”

“Hi!”

Page 47: Basics of Akka

class EchoActor extends Actor {  def receive = {    case <1> =>      sender ! <2>  }}

“Hi!”

“Hi!”

Page 48: Basics of Akka

class EchoActor extends Actor {  def receive = {    case msg =>      sender ! msg  }}

“Hi!”

“Hi!”

Page 49: Basics of Akka

Further topics

Page 50: Basics of Akka

Akka is composed of many tools:

actor, testkit, remote, cluster, transactor, agent, zeromq, camel, ...

Just use what you need

Page 51: Basics of Akka

See:

http://akka.io/docs/

Page 52: Basics of Akka

Read it now!

http://www.manning.com/roestenburg/

Page 53: Basics of Akka

Thank you!