akka - a brief intro

22
& the actor model

Upload: thomas-lockney

Post on 17-Jul-2015

117 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Akka - A Brief Intro

& the actor model

Page 2: Akka - A Brief Intro

What is Akka?

A toolkit that provides:simple concurrencyresiliencyelasticityhigh performance

Page 3: Akka - A Brief Intro

What is an actor?

Receive and send messages

Define behavior for next message

Create other actors

Page 4: Akka - A Brief Intro

The Actor model of computation

processing - behaviorstorage - state modelI/O - messages

Page 5: Akka - A Brief Intro

Key things to understand

messages processed sequentiallymultiple actors can be working at the same timeactors are not threadsan actor can block (but should minimize it)

no shared state

Page 6: Akka - A Brief Intro

A very simple actor

class MyActor extends Actor with ActorLogging { def receive = { case msg ⇒ log.info(“Received: {}”, msg) }}

Page 7: Akka - A Brief Intro

Creating actors in an actor system

Everything begins here:

val system = ActorSystem("example")val myActor = system.actorOf(Props[MyActor], name = “myActor”)myActor ! “a boring message”

Page 8: Akka - A Brief Intro

Messages

Can be any objectShould be immutableShould be serializableIdeally define a protocol for communication

Page 9: Akka - A Brief Intro

Protocol

“a system of rules that explain the correct conduct and procedures to be followed in formal situations”

— Merriam-Webster

Page 10: Akka - A Brief Intro

Message protocols

Define the interaction between actorsSpecify clear boundariesEncode success and failure clearly

Page 11: Akka - A Brief Intro

Message protocols

trait Responsecase class Success(res: Array[Byte]) extends Responsecase class Failure(err: String) extends Response

Page 12: Akka - A Brief Intro

Actor System

Page 13: Akka - A Brief Intro

Actor System

system.actorOf(Props[A], name = “A”)

Page 14: Akka - A Brief Intro

Actor System

context.actorOf(Props[C], name = “C”)

Page 15: Akka - A Brief Intro

Actor System

A is now the supervisor for D

Page 16: Akka - A Brief Intro

Actor System

An exception occurs in D

Page 17: Akka - A Brief Intro

Actor System

A declares a ‘Restart’ for the given exception.

Page 18: Akka - A Brief Intro

Actor System

override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: SomeException ⇒ Restart case _: Exception ⇒ Escalate }

Page 19: Akka - A Brief Intro

Actor System

Page 20: Akka - A Brief Intro

Extras

● Supervisors○ provide exception handling○ “let it crash”○ orthogonal to actor behavior

● Location transparency○ abstracts the idea of “where” an actor lives

Page 21: Akka - A Brief Intro

Extras

● Mailboxes● Routing

○ e.g., round-robin, smallest mailbox, etc.● Dispatching

○ essentially how to allocate thread resources

Page 22: Akka - A Brief Intro

Thank you!