Deep Introduction to Akka

3,161 views
2,972 views

Published on

Introduction to Akka actors with interesting example.

Published in: Technology, Design

Deep Introduction to Akka

  1. 1. akka IntroducingFriday, September 30, 2011
  2. 2. Deep Introduction to Akka Patrik Nordwall Typesafe Twitter: @patriknwFriday, September 30, 2011
  3. 3. Vision Simpler Concurrency Scalability Fault-toleranceFriday, September 30, 2011
  4. 4. ActorsFriday, September 30, 2011
  5. 5. What is an Actor?Friday, September 30, 2011
  6. 6. Event-driven Thread Actor Behavior StateFriday, September 30, 2011
  7. 7. Actors case object Tick class Counter extends Actor { var counter = 0 def receive = { case Tick => counter += 1 println(counter) } }Friday, September 30, 2011
  8. 8. Create Actors val counter = actorOf[Counter].start() counter is an ActorRefFriday, September 30, 2011
  9. 9. Tell: ! counter ! Tick fire-forgetFriday, September 30, 2011
  10. 10. Ask: ? // returns a future val future = counter ? Current val count: Option[Int] = future.as[Int] returns the Future directlyFriday, September 30, 2011
  11. 11. Reply class Counter extends Actor { var counter = 0 def receive = { case Tick => counter += 1 case Current => self.reply(counter) } }Friday, September 30, 2011
  12. 12. Future val future = Future { "Hello" + "World" } future.await future onResult { ... } onException { ... } onTimeout { ... }Friday, September 30, 2011
  13. 13. Set dispatcher object Shared { val myDispatcher = Dispatchers .newExecutorBasedEventDrivenDispatcher("my") .setCorePoolSize(7) .build } class MyActor extends Actor { self.dispatcher = Shared.dispatcher ... }Friday, September 30, 2011
  14. 14. Example 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 msFriday, September 30, 2011
  15. 15. Example HTTP Req Web Frontend Translation Service Text Word Translator CounterFriday, September 30, 2011
  16. 16. Example Source code: https://github.com/patriknw/akka-playgroundFriday, September 30, 2011
  17. 17. Typed ActorsFriday, September 30, 2011
  18. 18. Typed Actors trait Counter { def tick(): Unit def current: Int }Friday, September 30, 2011
  19. 19. Typed Actors class CounterImpl extends TypedActor with Counter { private var counter = 0 def tick() { counter += 1 } def current = counter }Friday, September 30, 2011
  20. 20. Typed Actors val counter = TypedActor.newInstance( classOf[Counter], classOf[CounterImpl]) counter.tick() counter.tick() val cur = counter.currentFriday, September 30, 2011
  21. 21. Remote ActorsFriday, September 30, 2011
  22. 22. Remoting // use host & port in config Actor.remote.start() Actor.remote.start("darkstar", 2552) Scalable implementation based on NIO (Netty) & ProtobufFriday, September 30, 2011
  23. 23. Server import Actor._ remote.register(“service:id”, actorOf[MyService]) server partFriday, September 30, 2011
  24. 24. Client val service = remote.actorFor( “service:id”, “darkstar”, 2552) service ! message client partFriday, September 30, 2011
  25. 25. Remoting in Akka 1.2 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 decisionFriday, September 30, 2011
  26. 26. Let it crash fault-toleranceFriday, September 30, 2011
  27. 27. Fault-tolerant onion-layered Error KernelFriday, September 30, 2011
  28. 28. Error KernelFriday, September 30, 2011
  29. 29. Linking link(actor) unlink(actor) startLink(actor) spawnLink[MyActor]Friday, September 30, 2011
  30. 30. Fault handlers AllForOneStrategy( errors, maxNrOfRetries, withinTimeRange) OneForOneStrategy( errors, maxNrOfRetries, withinTimeRange)Friday, September 30, 2011
  31. 31. Supervision class MySupervisor extends Actor { faultHandler = AllForOneStrategy( List(classOf[IllegalStateException]) 5, 5000)) def receive = { case Register(actor) => self.link(actor) } }Friday, September 30, 2011
  32. 32. Manage failure class FaultTolerantService extends Actor { ... override def preRestart(reason: Throwable) = { ... // clean up before restart } override def postRestart(reason: Throwable) = { ... // init after restart } }Friday, September 30, 2011
  33. 33. ...and much much more STM FSM Async-HTTP Camel Microkernel Guice Testkit Dataflow Spring scalaz AMQP SecurityFriday, September 30, 2011
  34. 34. Get it and learn more http://akka.ioFriday, September 30, 2011
  35. 35. EOFFriday, September 30, 2011

×