deep introduction to akka
DESCRIPTION
Introduction to Akka actors with interesting example.TRANSCRIPT
![Page 1: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/1.jpg)
akkaIntroducing
Friday, September 30, 2011
![Page 2: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/2.jpg)
DeepIntroduction to Akka
Patrik NordwallTypesafe
Twitter: @patriknw
Friday, September 30, 2011
![Page 3: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/3.jpg)
Vision
Simpler
Concurrency
Scalability
Fault-tolerance
Friday, September 30, 2011
![Page 4: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/4.jpg)
Actors
Friday, September 30, 2011
![Page 5: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/5.jpg)
What is an Actor?
Friday, September 30, 2011
![Page 6: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/6.jpg)
Event-drivenThread
Event-drivenThread
Behavior
State
Actor
Friday, September 30, 2011
![Page 7: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/8.jpg)
val counter = actorOf[Counter].start()
Create Actors
counter is an ActorRefFriday, September 30, 2011
![Page 9: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/9.jpg)
counter ! Tick
Tell: !
fire-forget
Friday, September 30, 2011
![Page 10: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/10.jpg)
// 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](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/12.jpg)
Future
future.awaitfuture onResult { ... } onException { ... } onTimeout { ... }
val future = Future { "Hello" + "World"}
Friday, September 30, 2011
![Page 13: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/15.jpg)
Example
Web Frontend
Translation Service
Text Translator
Word Counter
HTTP Req
Friday, September 30, 2011
![Page 16: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/16.jpg)
Source code: https://github.com/patriknw/akka-playground
Example
Friday, September 30, 2011
![Page 17: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/17.jpg)
Typed Actors
Friday, September 30, 2011
![Page 18: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/18.jpg)
trait Counter { def tick(): Unit def current: Int}
Typed Actors
Friday, September 30, 2011
![Page 19: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/21.jpg)
Remote Actors
Friday, September 30, 2011
![Page 22: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/22.jpg)
// 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](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/23.jpg)
import Actor._
remote.register(“service:id”, actorOf[MyService])
Server
server partFriday, September 30, 2011
![Page 24: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/24.jpg)
val service = remote.actorFor( “service:id”, “darkstar”, 2552)
service ! message
Client
client part
Friday, September 30, 2011
![Page 25: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/25.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/26.jpg)
Let it crash fault-tolerance
Friday, September 30, 2011
![Page 27: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/27.jpg)
Fault-tolerant onion-layered Error Kernel
Friday, September 30, 2011
![Page 28: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/28.jpg)
ErrorKernel
Friday, September 30, 2011
![Page 29: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/29.jpg)
link(actor)unlink(actor)
startLink(actor)spawnLink[MyActor]
Linking
Friday, September 30, 2011
![Page 30: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/30.jpg)
AllForOneStrategy( errors, maxNrOfRetries, withinTimeRange)
OneForOneStrategy( errors, maxNrOfRetries, withinTimeRange)
Fault handlers
Friday, September 30, 2011
![Page 31: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/31.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/32.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/33.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/34.jpg)
Get it and learn morehttp://akka.io
Friday, September 30, 2011
![Page 35: Deep Introduction to Akka](https://reader033.vdocuments.net/reader033/viewer/2022052522/5549ac0cb4c905fa728b559d/html5/thumbnails/35.jpg)
EOFFriday, September 30, 2011