SlideShare une entreprise Scribd logo
1  sur  35
Télécharger pour lire hors ligne
akka
              Introducing




Friday, September 30, 2011
Deep
                   Introduction to Akka
                             Patrik Nordwall
                                   Typesafe
                              Twitter: @patriknw




Friday, September 30, 2011
Vision

                             Simpler
                                  Concurrency
                                  Scalability
                                  Fault-tolerance



Friday, September 30, 2011
Actors


Friday, September 30, 2011
What is an Actor?




Friday, September 30, 2011
Event-driven
                               Thread

                              Actor

                              Behavior



                               State




Friday, September 30, 2011
Actors
                             case object Tick

                             class Counter extends Actor {
                               var counter = 0

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


Friday, September 30, 2011
Create Actors

                   val counter =
                     actorOf[Counter].start()




                             counter is an ActorRef
Friday, September 30, 2011
Tell: !

                             counter ! Tick




                               fire-forget


Friday, September 30, 2011
Ask: ?
          // returns a future
          val future = counter ? Current

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




                             returns the Future directly
Friday, September 30, 2011
Reply
                             class Counter extends Actor {
                               var counter = 0

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



Friday, September 30, 2011
Future
                              val future = Future {
                                "Hello" + "World"
                              }




                             future.await
                             future
                               onResult { ... }
                               onException { ... }
                               onTimeout { ... }



Friday, September 30, 2011
Set dispatcher
               object Shared {
                 val myDispatcher = Dispatchers
                   .newExecutorBasedEventDrivenDispatcher("my")
                   .setCorePoolSize(7)
                   .build
               }

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

                     ...
               }


Friday, September 30, 2011
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 ms

Friday, September 30, 2011
Example
                             HTTP Req


                                                 Web
                                               Frontend



                                              Translation
                                                Service



                                           Text              Word
                                        Translator          Counter

Friday, September 30, 2011
Example



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




Friday, September 30, 2011
Typed Actors


Friday, September 30, 2011
Typed Actors
                             trait Counter {
                               def tick(): Unit
                               def current: Int
                             }




Friday, September 30, 2011
Typed Actors
                             class CounterImpl extends
                               TypedActor with Counter {

                                 private var counter = 0

                                 def tick() {
                                   counter += 1
                                 }

                                 def current = counter
                             }


Friday, September 30, 2011
Typed Actors
                     val counter = TypedActor.newInstance(
                       classOf[Counter],
                       classOf[CounterImpl])

                     counter.tick()
                     counter.tick()
                     val cur = counter.current




Friday, September 30, 2011
Remote Actors


Friday, September 30, 2011
Remoting
                             // use host & port in config
                             Actor.remote.start()

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




                              Scalable implementation based on
                                   NIO (Netty) & Protobuf

Friday, September 30, 2011
Server

                  import Actor._

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




                                   server part
Friday, September 30, 2011
Client
                             val service = remote.actorFor(
                               “service:id”,
                               “darkstar”,
                               2552)

                             service ! message



                                     client part
Friday, September 30, 2011
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 decision


Friday, September 30, 2011
Let it crash
                             fault-tolerance


Friday, September 30, 2011
Fault-tolerant
                              onion-layered
                              Error Kernel



Friday, September 30, 2011
Error
    Kernel




Friday, September 30, 2011
Linking
                             link(actor)
                             unlink(actor)

                             startLink(actor)
                             spawnLink[MyActor]




Friday, September 30, 2011
Fault handlers
                              AllForOneStrategy(
                                errors,
                                maxNrOfRetries,
                                withinTimeRange)

                              OneForOneStrategy(
                                errors,
                                maxNrOfRetries,
                                withinTimeRange)

Friday, September 30, 2011
Supervision
                     class MySupervisor extends Actor {
                      faultHandler = AllForOneStrategy(
                        List(classOf[IllegalStateException])
                        5, 5000))

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


Friday, September 30, 2011
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
...and much much more
               STM
                                                    FSM
      Async-HTTP                      Camel
                             Microkernel        Guice
         Testkit
                Dataflow                    Spring

             scalaz            AMQP
                                           Security
Friday, September 30, 2011
Get it and learn more
                             http://akka.io



Friday, September 30, 2011
EOF
Friday, September 30, 2011

Contenu connexe

Dernier

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 

En vedette

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Deep Introduction to Akka

  • 1. akka Introducing Friday, September 30, 2011
  • 2. Deep Introduction to Akka Patrik Nordwall Typesafe Twitter: @patriknw Friday, September 30, 2011
  • 3. Vision Simpler Concurrency Scalability Fault-tolerance Friday, September 30, 2011
  • 5. What is an Actor? Friday, September 30, 2011
  • 6. Event-driven Thread Actor Behavior State Friday, September 30, 2011
  • 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. Create Actors val counter = actorOf[Counter].start() counter is an ActorRef Friday, September 30, 2011
  • 9. Tell: ! counter ! Tick fire-forget Friday, September 30, 2011
  • 10. Ask: ? // returns a future val future = counter ? Current val count: Option[Int] = future.as[Int] returns the Future directly Friday, September 30, 2011
  • 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. Future val future = Future { "Hello" + "World" } future.await future onResult { ... } onException { ... } onTimeout { ... } Friday, September 30, 2011
  • 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. 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 ms Friday, September 30, 2011
  • 15. Example HTTP Req Web Frontend Translation Service Text Word Translator Counter Friday, September 30, 2011
  • 16. Example Source code: https://github.com/patriknw/akka-playground Friday, September 30, 2011
  • 18. Typed Actors trait Counter { def tick(): Unit def current: Int } Friday, September 30, 2011
  • 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. Typed Actors val counter = TypedActor.newInstance( classOf[Counter], classOf[CounterImpl]) counter.tick() counter.tick() val cur = counter.current Friday, September 30, 2011
  • 22. Remoting // use host & port in config Actor.remote.start() Actor.remote.start("darkstar", 2552) Scalable implementation based on NIO (Netty) & Protobuf Friday, September 30, 2011
  • 23. Server import Actor._ remote.register(“service:id”, actorOf[MyService]) server part Friday, September 30, 2011
  • 24. Client val service = remote.actorFor( “service:id”, “darkstar”, 2552) service ! message client part Friday, September 30, 2011
  • 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 decision Friday, September 30, 2011
  • 26. Let it crash fault-tolerance Friday, September 30, 2011
  • 27. Fault-tolerant onion-layered Error Kernel Friday, September 30, 2011
  • 28. Error Kernel Friday, September 30, 2011
  • 29. Linking link(actor) unlink(actor) startLink(actor) spawnLink[MyActor] Friday, September 30, 2011
  • 30. Fault handlers AllForOneStrategy( errors, maxNrOfRetries, withinTimeRange) OneForOneStrategy( errors, maxNrOfRetries, withinTimeRange) Friday, September 30, 2011
  • 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. 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. ...and much much more STM FSM Async-HTTP Camel Microkernel Guice Testkit Dataflow Spring scalaz AMQP Security Friday, September 30, 2011
  • 34. Get it and learn more http://akka.io Friday, September 30, 2011