Akka - A Brief Intro
-
Upload
thomas-lockney -
Category
Technology
-
view
116 -
download
1
Transcript of Akka - A Brief Intro
![Page 1: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/1.jpg)
& the actor model
![Page 2: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/2.jpg)
What is Akka?
A toolkit that provides:simple concurrencyresiliencyelasticityhigh performance
![Page 3: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/3.jpg)
What is an actor?
Receive and send messages
Define behavior for next message
Create other actors
![Page 4: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/4.jpg)
The Actor model of computation
processing - behaviorstorage - state modelI/O - messages
![Page 5: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/5.jpg)
Key things to understand
messages processed sequentiallymultiple actors can be working at the same timeactors are not threadsan actor can block (but should minimize it)
no shared state
![Page 6: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/6.jpg)
A very simple actor
class MyActor extends Actor with ActorLogging { def receive = { case msg ⇒ log.info(“Received: {}”, msg) }}
![Page 7: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/7.jpg)
Creating actors in an actor system
Everything begins here:
val system = ActorSystem("example")val myActor = system.actorOf(Props[MyActor], name = “myActor”)myActor ! “a boring message”
![Page 8: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/8.jpg)
Messages
Can be any objectShould be immutableShould be serializableIdeally define a protocol for communication
![Page 9: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/9.jpg)
Protocol
“a system of rules that explain the correct conduct and procedures to be followed in formal situations”
— Merriam-Webster
![Page 10: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/10.jpg)
Message protocols
Define the interaction between actorsSpecify clear boundariesEncode success and failure clearly
![Page 11: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/11.jpg)
Message protocols
trait Responsecase class Success(res: Array[Byte]) extends Responsecase class Failure(err: String) extends Response
![Page 12: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/12.jpg)
Actor System
![Page 13: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/13.jpg)
Actor System
system.actorOf(Props[A], name = “A”)
![Page 14: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/14.jpg)
Actor System
context.actorOf(Props[C], name = “C”)
![Page 15: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/15.jpg)
Actor System
A is now the supervisor for D
![Page 16: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/16.jpg)
Actor System
An exception occurs in D
![Page 17: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/17.jpg)
Actor System
A declares a ‘Restart’ for the given exception.
![Page 18: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/18.jpg)
Actor System
override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: SomeException ⇒ Restart case _: Exception ⇒ Escalate }
![Page 19: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/19.jpg)
Actor System
![Page 20: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/20.jpg)
Extras
● Supervisors○ provide exception handling○ “let it crash”○ orthogonal to actor behavior
● Location transparency○ abstracts the idea of “where” an actor lives
![Page 21: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/21.jpg)
Extras
● Mailboxes● Routing
○ e.g., round-robin, smallest mailbox, etc.● Dispatching
○ essentially how to allocate thread resources
![Page 22: Akka - A Brief Intro](https://reader034.fdocuments.in/reader034/viewer/2022042607/55a864231a28ab4e238b4656/html5/thumbnails/22.jpg)
Thank you!