introduction to akka
TRANSCRIPT
![Page 1: Introduction to Akka](https://reader033.vdocuments.us/reader033/viewer/2022061601/55a644d91a28abed148b45c2/html5/thumbnails/1.jpg)
Introduction to
AkkaActor Framework
![Page 2: Introduction to Akka](https://reader033.vdocuments.us/reader033/viewer/2022061601/55a644d91a28abed148b45c2/html5/thumbnails/2.jpg)
Akka – an actor framework
A
B
C
D
X
Actor system:• An object model composed
of actors• Communication by
asynchronous message passing
• Message delivery (in general) not guaranteed
• Java & Scala API
Microservices in one app
E
![Page 3: Introduction to Akka](https://reader033.vdocuments.us/reader033/viewer/2022061601/55a644d91a28abed148b45c2/html5/thumbnails/3.jpg)
An actor
• Reacts to messages• Is a ‘living’ object• Has a:
o Nameo Locationo Mutable stateo Lifecycleo A mailboxo A parento Optionally, childreno Changeable behavior
A
Props
ActorRef
Internally synchronous!
![Page 4: Introduction to Akka](https://reader033.vdocuments.us/reader033/viewer/2022061601/55a644d91a28abed148b45c2/html5/thumbnails/4.jpg)
Actor hierarchy
A
B
C
D
X
• Actors form a supervision tree
• A child’s life is governed by its parent
• Failure handling isindependent from cause
• Failure is local
• Failure will happen…
E
![Page 5: Introduction to Akka](https://reader033.vdocuments.us/reader033/viewer/2022061601/55a644d91a28abed148b45c2/html5/thumbnails/5.jpg)
What does it all give?
Actors are independent processing cells It’s fast, non blocking It gives a horizontally scalable application architecture Composes well with regular objects
![Page 6: Introduction to Akka](https://reader033.vdocuments.us/reader033/viewer/2022061601/55a644d91a28abed148b45c2/html5/thumbnails/6.jpg)
Log sources
The problem to crunch – log processing
File
Single line
JsonLogAggregatorLogReceiver Json
LogReceiverService
![Page 7: Introduction to Akka](https://reader033.vdocuments.us/reader033/viewer/2022061601/55a644d91a28abed148b45c2/html5/thumbnails/7.jpg)
Log sources
Possible Architecture
File
Single line
JsonLogAggregatorLogReceiver
Single LineProcessor
Single Line
Multiline Processor
Multiple lines
Single Line
Json
Json
![Page 8: Introduction to Akka](https://reader033.vdocuments.us/reader033/viewer/2022061601/55a644d91a28abed148b45c2/html5/thumbnails/8.jpg)
Log sources
Another possibility
File
Single line
JsonLogAggregatorLogReceiver
Single LineProcessor
Single Line
Multiline Processor
Multiple lines
Single Line
Json
Json
Json
![Page 9: Introduction to Akka](https://reader033.vdocuments.us/reader033/viewer/2022061601/55a644d91a28abed148b45c2/html5/thumbnails/9.jpg)
Log sources
Another one – concepts changed
File
Single line
JsonLogAggregatorLogReceiver
Line Parser
Single Line
LogProcessor
Multiple lines
Single Line
Json
Json
Json
![Page 10: Introduction to Akka](https://reader033.vdocuments.us/reader033/viewer/2022061601/55a644d91a28abed148b45c2/html5/thumbnails/10.jpg)
Actor lifecycle
ARestart
Start
Stop
PoisonPill
Exception
Supervised by actor’s parent
• Stop the child?• Restart the child?• Restart all children?• Escalate (throw to parent)?
![Page 11: Introduction to Akka](https://reader033.vdocuments.us/reader033/viewer/2022061601/55a644d91a28abed148b45c2/html5/thumbnails/11.jpg)
Old-school web application vs actor hierarchy
Controller
ServiceCServiceB
DAOServiceD
StatelessStatic
A
B
CD
X
E
StatefulDynamic
![Page 12: Introduction to Akka](https://reader033.vdocuments.us/reader033/viewer/2022061601/55a644d91a28abed148b45c2/html5/thumbnails/12.jpg)
A
B
C
The internals
Thread
Thread
Thread
ExecutionContextDispatcher
Future Future
• Fork-Join Pool• Threadpool
![Page 13: Introduction to Akka](https://reader033.vdocuments.us/reader033/viewer/2022061601/55a644d91a28abed148b45c2/html5/thumbnails/13.jpg)
Best practices
• Think of the hierarchy up-front• Avoid singleton root actors• Use ActorRefs, avoid the slow ActorSelection
• Always be prepared for failure => use timeouts• Use context.become to separate behavior by states• Monitor, log, measure everything• Don’t optimize the performance (yet)• If you can, delegate it => ensure single responsibility
![Page 14: Introduction to Akka](https://reader033.vdocuments.us/reader033/viewer/2022061601/55a644d91a28abed148b45c2/html5/thumbnails/14.jpg)
Readings• http://doc.akka.io/docs/akka/2.3.7/scala.html• http://letitcrash.com/• Akka Concurency• Akka in Depth• Akka in Action - MEAP• Functional and Reactive Domain Modeling – MEAP• Code samples via Activator