Akka (BeJUG)

2,698 views

Published on

Introduction to Akka, as presented on May 3 2012 at the Belgian Java User Group (BeJUG). For more details see: http://www.bejug.org/confluenceBeJUG/display/BeJUG/ForkJoin+and+Akka

Demo code can be found at: http://bit.ly/bejug-akka

0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,698
On SlideShare
0
From Embeds
0
Number of Embeds
16
Actions
Shares
0
Downloads
8
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Akka (BeJUG)

  1. 1. Akka Actors@Sander_Mak
  2. 2. Akka Part of stackWritten in ScalaWith officially supported Java API
  3. 3. Akka ‘s premiseIt’s too hard to write systems that are: Concurrent & Correct Highly scalable Fault-tolerant
  4. 4. Akka 2.0ActorsSoftware Transactional MemoryDataflow concurrencyFutures
  5. 5. Akkawho uses ? traffic management
  6. 6. ActorsInspired by : Asynchronous message passing Scale up, scale out Fault-tolerant (‘let it crash’)
  7. 7. ActorsHow to define an actor? ~400 bytes Local state Behavior mailbox
  8. 8. ActorsHow to define an actor? ~400 bytes Create more actors Send messages Modify state Local state Modify behavior Behavior mailbox
  9. 9. ActorsHow to define an actor?class MyFirstActor extends Actor { var total = 0 def receive = { case MyMessage(payload) => println(“Received: “ + payload”) case countMessage: Int => total += countMessage case _ => println("Unknown message") }}
  10. 10. ActorsDispatcher: contains executor strategy Local state Event-driven Behavior mailbox
  11. 11. Actors and Fork/JoinPing/pong messages (no processing) Improved 48 JSR-166 build ( ?)cores
  12. 12. Demo NearestCityActor NearestCityActor NearestCityActor NearestCityActor RoundRobin Result Router ListenerActorMessages with cities Print results
  13. 13. SupervisionHierarchical supervision Child actors supervised by parentTrap exceptions and apply recoveryError Kernel pattern Actor 1 create child actor Actor 2 dangerous operation critical state superviseDistinguish scratch and critical state
  14. 14. Supervisionclass GuardianActor extends Actor { def supervisorStrategy = OneForOneStrategy({ case _:CompanyBankruptException => Stop case _:InvalidStateException => Restart case _:ServiceUnavailableException => Resume case _ => Escalate }, maxNrOfRetries = 3, 5.seconds) def receive = { ... }} Actor may implement preRestart & postRestart
  15. 15. Remote actorsDispatchers for scale-up, remote actors for scala-out Machine A Machine B actor2 ! SomeMsg Actor 1 Actor 2 Netty, Protobuf Router Actor 3 Actor creation Supervision Remote deploy through config (no code change)
  16. 16. Remote actorsCreating 10 actors on 2 nodes with a router akka  {    actor  {        deployment  {            /serviceA/aggregation  {                router  =  "round-­‐robin"                nr-­‐of-­‐instances  =  10                target  {                    nodes  =  ["akka://app@10.0.0.2:2552",  "akka://app@10.0.0.3:2552"]                }            }        }    } }Looking up a remote actor val  actor  =        context.actorFor("akka://actorSystemName@10.0.0.1:2552/user/actorName")
  17. 17. BlockingActors lift some ForkJoin restrictions Remoting possible Recursive decomposition optional Fault-tolerantStill, there are restrictions similar to ForkJoin tasks No blocking IO No explicit locking No shared memory writes
  18. 18. Async IO Based on composable FuturesServe http mini akka-SprayConsume http async-http-clientPersistence hammersmith
  19. 19. Async IO Based on Java NIOAkka IO Immutable abstractions on top of: Channels Streams (Iteratee based) Bridges (low-level) async IO and actors
  20. 20. Async IO Practical application: JMeter => 1 user, 1 thread Gatling => 1 user, 1 actor+ blocking IO + async-http-client= OutOfMemory errors = or clustering necessary time drift in results
  21. 21. Async IO Practical application: JMeter => 1 user, 1 thread Gatling => 1 user, 1 actor+ blocking IO + async-http-client= OutOfMemory errors = or clustering necessary time drift in results
  22. 22. Is that all? Finite State Machine Akka TestKit core Scheduler Event Busmodules
  23. 23. Is that all? Typesafe ConsoleCommercialsubscription
  24. 24. Questions? Code @ bit.ly/bejug-akka

×