akka 2.0 reloaded
DESCRIPTION
Introducing Akka 2.0TRANSCRIPT
![Page 2: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/2.jpg)
Akka 2.0
Akka name comes from Sami mythology is actually name of a goddess of wisdom and beauty.
Akka incidentally means sister in Telugu!!
![Page 3: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/3.jpg)
The Problem
It is way too hard to build
=> correct highly concurrent systems
=> truly scalable systems
=> self-healing, fault-tolerant systems
![Page 4: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/4.jpg)
What is Akka?Right abstraction with actors for concurrent, fault-tolerant and scalable applications
For Fault-Tolerance uses “let it crash” model
Abstraction for transparent distribution for load
![Page 5: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/5.jpg)
Introducing Actors
Actor is an entity encapsulating behavior, state and a mailbox to receive messages
For a message received by Actor a thread is allocated to it
Then Actors behavior is applied to the message and potentially some state is changed or messages is passed to other Actors
![Page 6: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/6.jpg)
Introducing Actors..
There is elasticity between message processing and addition of new messages. New messages can be added while actor execution is happening.
When processing of messages is completed thread is deallocated from the actor. It can be reallocated a thread at a later time
![Page 7: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/7.jpg)
![Page 8: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/8.jpg)
![Page 9: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/9.jpg)
![Page 10: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/10.jpg)
![Page 11: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/11.jpg)
Create Application
import akka.actor.ActorSystem
val system = ActorSystem("firstApp")
![Page 12: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/12.jpg)
My First Actorimport akka.actor.{ Actor, Props }
class MyFirstActor extends Actor { def receive = { case msg => println("Hello!!") }}
![Page 13: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/13.jpg)
Create Actors
MyFirstActor is an ActorRefCreate a top level actor
import akka.actor.{ ActorSystem, Props }
val system = ActorSystem("firstApp")val myFirstActor = system.actorOf(Props[MyFirstActor])
![Page 14: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/14.jpg)
Stop Actors
Also stops all actors in hierarchy
system stop myFirstActor
![Page 15: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/15.jpg)
Send: !
fire-forget
myFirstActor ! “Hello”
![Page 16: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/16.jpg)
Ask: ?
Returns a Future[Any]
import akka.pattern.ask
implicit val timeout = Timeout(50000 milliseconds)
val future = myActor ? "hello"
Await.result(future, timeout.duration).asInstanceOf[Int]
![Page 17: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/17.jpg)
Replyimport akka.actor.Actor
class LongWorkingActor extends Actor { def receive = { case number: Int => sender ! (“Hi I received ” + number) }}
![Page 18: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/18.jpg)
Routers
RoundRobin
Random
SmallestMailBox
BroadCast
ScatterGaherFirstCompleted
![Page 19: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/19.jpg)
Routers...
val router = system.actorOf(Props[RouterWorkerActor].withRouter(RoundRobinRouter(nrOfInstances = 5)))
![Page 20: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/20.jpg)
Let It CrashFault Tolerance
![Page 21: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/21.jpg)
![Page 22: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/22.jpg)
![Page 23: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/23.jpg)
![Page 24: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/24.jpg)
![Page 25: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/25.jpg)
![Page 26: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/26.jpg)
![Page 27: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/27.jpg)
Actor Path
val actorRef = system.actorFor("akka://actorPathApp/user/parent/child")
val parent = context.actorFor("..")
val sibling = context.actorFor("../sibling")
val refPath = actorRef.path
![Page 28: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/28.jpg)
Supervision
class Supervisor extends Actor { override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: ArithmeticException => Resume case _: NullPointerException => Restart case _: IllegalArgumentException => Stop case _: Exception => Escalate }}
![Page 29: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/29.jpg)
Manage Failure
class FaultTolerantService extends Actor { def receive = { case msg => println(msg) }
override def preRestart(reason: Throwable, message: Option[Any]) = { // clean up before restart }
override def postRestart(reason: Throwable) = { // init after restart }}
![Page 30: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/30.jpg)
Code Samples
https://github.com/meetumaltiar/AkkaKnolX
![Page 31: Akka 2.0 Reloaded](https://reader036.vdocument.in/reader036/viewer/2022081716/54b724064a7959cc1c8b458a/html5/thumbnails/31.jpg)
References
Viktor Klang talk on Akka 2.0 at NE Scala symposium
Akka website akka.io