akka - a brief intro

Post on 17-Jul-2015

116 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

& the actor model

What is Akka?

A toolkit that provides:simple concurrencyresiliencyelasticityhigh performance

What is an actor?

Receive and send messages

Define behavior for next message

Create other actors

The Actor model of computation

processing - behaviorstorage - state modelI/O - messages

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

A very simple actor

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

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”

Messages

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

Protocol

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

— Merriam-Webster

Message protocols

Define the interaction between actorsSpecify clear boundariesEncode success and failure clearly

Message protocols

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

Actor System

Actor System

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

Actor System

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

Actor System

A is now the supervisor for D

Actor System

An exception occurs in D

Actor System

A declares a ‘Restart’ for the given exception.

Actor System

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

Actor System

Extras

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

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

Extras

● Mailboxes● Routing

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

○ essentially how to allocate thread resources

Thank you!

top related