Deep Introduction to Akka

35
akka Introducing Friday, September 30, 2011

description

Introduction to Akka actors with interesting example.

Transcript of Deep Introduction to Akka

Page 1: Deep Introduction to Akka

akkaIntroducing

Friday, September 30, 2011

Page 2: Deep Introduction to Akka

DeepIntroduction to Akka

Patrik NordwallTypesafe

Twitter: @patriknw

Friday, September 30, 2011

Page 3: Deep Introduction to Akka

Vision

Simpler

Concurrency

Scalability

Fault-tolerance

Friday, September 30, 2011

Page 4: Deep Introduction to Akka

Actors

Friday, September 30, 2011

Page 5: Deep Introduction to Akka

What is an Actor?

Friday, September 30, 2011

Page 6: Deep Introduction to Akka

Event-drivenThread

Event-drivenThread

Behavior

State

Actor

Friday, September 30, 2011

Page 7: Deep Introduction to Akka

case object Tick

class Counter extends Actor { var counter = 0

def receive = { case Tick => counter += 1 println(counter) }}

Actors

Friday, September 30, 2011

Page 8: Deep Introduction to Akka

val counter = actorOf[Counter].start()

Create Actors

counter is an ActorRefFriday, September 30, 2011

Page 9: Deep Introduction to Akka

counter ! Tick

Tell: !

fire-forget

Friday, September 30, 2011

Page 10: Deep Introduction to Akka

// returns a futureval future = counter ? Current

val count: Option[Int] = future.as[Int]

Ask: ?

returns the Future directly

Friday, September 30, 2011

Page 11: Deep Introduction to Akka

Replyclass Counter extends Actor { var counter = 0

def receive = { case Tick => counter += 1 case Current => self.reply(counter) }}

Friday, September 30, 2011

Page 12: Deep Introduction to Akka

Future

future.awaitfuture onResult { ... } onException { ... } onTimeout { ... }

val future = Future { "Hello" + "World"}

Friday, September 30, 2011

Page 13: Deep Introduction to Akka

object Shared { val myDispatcher = Dispatchers .newExecutorBasedEventDrivenDispatcher("my") .setCorePoolSize(7) .build}

class MyActor extends Actor { self.dispatcher = Shared.dispatcher ...}

Set dispatcher

Friday, September 30, 2011

Page 14: Deep Introduction to Akka

Translation Service with HTTP API- count words: 100 ms- translate text: 100 ms

We have 8 threads

Must handle 4 concurrent request with average response time of <110 ms

Example

Friday, September 30, 2011

Page 15: Deep Introduction to Akka

Example

Web Frontend

Translation Service

Text Translator

Word Counter

HTTP Req

Friday, September 30, 2011

Page 16: Deep Introduction to Akka

Source code: https://github.com/patriknw/akka-playground

Example

Friday, September 30, 2011

Page 17: Deep Introduction to Akka

Typed Actors

Friday, September 30, 2011

Page 18: Deep Introduction to Akka

trait Counter { def tick(): Unit def current: Int}

Typed Actors

Friday, September 30, 2011

Page 19: Deep Introduction to Akka

class CounterImpl extends TypedActor with Counter {

private var counter = 0

def tick() { counter += 1 }

def current = counter}

Typed Actors

Friday, September 30, 2011

Page 20: Deep Introduction to Akka

val counter = TypedActor.newInstance( classOf[Counter], classOf[CounterImpl]) counter.tick()counter.tick()val cur = counter.current

Typed Actors

Friday, September 30, 2011

Page 21: Deep Introduction to Akka

Remote Actors

Friday, September 30, 2011

Page 22: Deep Introduction to Akka

// use host & port in configActor.remote.start()

Actor.remote.start("darkstar", 2552)

Remoting

Scalable implementation based on NIO (Netty) & Protobuf

Friday, September 30, 2011

Page 23: Deep Introduction to Akka

import Actor._

remote.register(“service:id”, actorOf[MyService])

Server

server partFriday, September 30, 2011

Page 24: Deep Introduction to Akka

val service = remote.actorFor( “service:id”, “darkstar”, 2552)

service ! message

Client

client part

Friday, September 30, 2011

Page 25: Deep Introduction to Akka

Problem Deployment (local vs remote) is a dev decision

We get a fixed and hard-coded topology Can’t change it dynamically and adaptively

Needs to be a deployment & runtime decision

Remoting in Akka 1.2

Friday, September 30, 2011

Page 26: Deep Introduction to Akka

Let it crash fault-tolerance

Friday, September 30, 2011

Page 27: Deep Introduction to Akka

Fault-tolerant onion-layered Error Kernel

Friday, September 30, 2011

Page 28: Deep Introduction to Akka

ErrorKernel

Friday, September 30, 2011

Page 29: Deep Introduction to Akka

link(actor)unlink(actor)

startLink(actor)spawnLink[MyActor]

Linking

Friday, September 30, 2011

Page 30: Deep Introduction to Akka

AllForOneStrategy( errors, maxNrOfRetries, withinTimeRange)

OneForOneStrategy( errors, maxNrOfRetries, withinTimeRange)

Fault handlers

Friday, September 30, 2011

Page 31: Deep Introduction to Akka

class MySupervisor extends Actor { faultHandler = AllForOneStrategy( List(classOf[IllegalStateException]) 5, 5000))

def receive = { case Register(actor) => self.link(actor) }}

Supervision

Friday, September 30, 2011

Page 32: Deep Introduction to Akka

class FaultTolerantService extends Actor { ... override def preRestart(reason: Throwable) = { ... // clean up before restart } override def postRestart(reason: Throwable) = { ... // init after restart }}

Manage failure

Friday, September 30, 2011

Page 33: Deep Introduction to Akka

AMQP

Dataflow

Security

...and much much more

Async-HTTP

Guice

scalaz

FSMSTM

Spring

Camel

MicrokernelTestkit

Friday, September 30, 2011

Page 34: Deep Introduction to Akka

Get it and learn morehttp://akka.io

Friday, September 30, 2011

Page 35: Deep Introduction to Akka

EOFFriday, September 30, 2011