Akka in Practice: Designing Actor-based Applications
-
Upload
nljug -
Category
Technology
-
view
6.649 -
download
6
Transcript of Akka in Practice: Designing Actor-based Applications
![Page 1: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/1.jpg)
PRACTICEAKKA
Real World Application Design Patterns for the Intermediate Akka Developer
@agemooij!
@[email protected]✉[email protected]✉
in
November 6th 2013
![Page 2: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/2.jpg)
Agenda➡ BOOTING UP ➡ THE RECEPTIONIST ➡ CREATING CHILDREN ➡ BECOME ➡ CONFIGURATION BY EXTENSION ➡ STREAMING EVENTS ➡ DOING REQUESTS/RESPONSE
![Page 3: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/3.jpg)
Booting Up:➡ How do we start up our Akka app? ➡ Where do we create the actor system?
Akka applications don’t need any containers!
“Why don’t we just write a main method?”
“Sure, why not?”
![Page 4: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/4.jpg)
Booting Up: it is that easy :)
![Page 5: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/5.jpg)
The Receptionist:➡ How do we get Akka to actually do some work? ➡ We need to talk to it from the “outside” ➡ We need a “receptionist” to take our calls
“Just use a socket!”
Akka IO supports TCP, UDP, and HTTPWe will use Spray, a.k.a. akka-http
![Page 6: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/6.jpg)
The Receptionist:Spray exposes your entry-point actor to the outside world as an HTTP (REST) API
![Page 7: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/7.jpg)
The Receptionist:Your entry-point actor can use Spray’s routing DSL to handle requests
“But who is going to do the work?”
![Page 8: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/8.jpg)
Creating Children:➡ Every actor should do one thing and one thing only! ➡ When it all gets too much, use child labor! ➡ Children help with separation of concerns ➡ Use supervision to deal with failure
![Page 9: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/9.jpg)
Creating Children:➡ Create a child to do the hard work ➡ Here we use the “ask” pattern and futures in order
to produce an HTTP response ➡ There are other ways!
“But how do you MOCK this?”
![Page 10: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/10.jpg)
Creating Children:➡ Direct use of context.actorOf is hard to stub/mock ➡ This is OK for simple children because Akka
TestKit is awesome ➡ But for more complex child hierarchies, it would
be nice if we could stub/mock child actors
![Page 11: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/11.jpg)
Creating Children:➡ Layer traits based on least required dependency ➡ ActorCreationSupport can be mixed with anything ➡ ActorContextCreationSupport requires context
![Page 12: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/12.jpg)
Creating Children:➡ Mix in a trait for creating children ➡ Override during testing
“How to test the http ROUTES?”
![Page 13: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/13.jpg)
Creating Children:➡ Put the routes in a separate Routes trait! ➡ Routes says: “I need an ExecutionContext!”
![Page 14: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/14.jpg)
Creating Children:➡ Layer traits based on least required dependency ➡ ActorCreationSupport can be mixed with anything ➡ ActorContextCreationSupport requires context
TestCreationSupport creates a FakeProcessJobActor
![Page 15: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/15.jpg)
Creating Children:➡ Mix in TestsCreationSupport into Routes trait ➡ Ready to stub!
“DUDE, Specs2 testing is awesome!”
![Page 16: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/16.jpg)
Initializing Actor State:➡ Say an Actor uses a “Stuff” library ➡ Stuff needs some time to start…
“VARS? Concurrent Access?”
![Page 17: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/17.jpg)
➡ Initialize the actor using messages ➡ Only process messages once initialized ➡ Possibly stash messages away until ready ➡ Crash when initialization fails (parent handles failure)
Initializing Actor State:
![Page 18: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/18.jpg)
Initialization using become:➡ Allows you to build simple state machines ➡ Use multiple receive functions and switch behavior ➡ No need for vars!
![Page 19: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/19.jpg)
Become is powerful sh*t:➡ Allows you to build simple state machines ➡ Switch behaviour at runtime based on internal state ➡ Makes it possible to model processes and flows
![Page 20: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/20.jpg)
Configuring Akka Apps:➡ We all know hardcoding configuration is evil ➡ We don’t want to use a heavyweight DI
library for some simple settings ➡ How can we configure our Akka apps?
![Page 21: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/21.jpg)
➡ Start by using the popular typesafe-config library
Configuring Akka Apps:
![Page 22: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/22.jpg)
Configuration by Extension:➡ Use the Akka extension mechanism to get a nice
Akka “Singleton”
![Page 23: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/23.jpg)
Using Settings:➡ Access your Settings wherever you have a reference
to an ActorSystem or an ActorContext
![Page 24: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/24.jpg)
Using Akka’s EventStream:➡ How to communicate between disconnected actors? ➡ You don’t always have (or need) a reference to all
actors in the system who might care about your messages
![Page 25: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/25.jpg)
Using Akka’s EventStream:➡ Publish messages on the Akka EventStream ➡ Anyone can subscribe to receive these
![Page 26: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/26.jpg)
Using Akka’s EventStream:➡ Messages that you have subscribed to are routed to
your receive method like any other actor messages
![Page 27: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/27.jpg)
Using Akka’s EventStream:➡ The EventStream is great for monitoring what
happens in your system ➡ You can use it to implement event stores and other
event-driven patterns ➡ It is also great for making your application easier to
test
![Page 28: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/28.jpg)
Complex request/response➡ So far we have used the “ask” pattern and futures to
do request/response with actors ➡ But what happens when the request handling flow
gets a bit more complex?
![Page 29: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/29.jpg)
Complex request/response➡ This might or might not be your cup of tea… ➡ There are other ways to model a sequential flow of
information…
![Page 30: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/30.jpg)
Complex request/response➡ We can use our old friend Become in a single-use actor ➡ We do need to make sure it gets killed after it is done ➡ We also need to take care of timeouts ourselves
![Page 31: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/31.jpg)
QUESTIONSGot?
ask away!
![Page 32: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/32.jpg)
MOREWant?
do THE exercises!
https://github.com/RayRoestenburg/scala-io-exercise-1
![Page 33: Akka in Practice: Designing Actor-based Applications](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f389eb4c905471e8b4860/html5/thumbnails/33.jpg)
HAKKINGHappy
manning.com/roestenburg
!
@agemooij!