2.0 Reloaded       Meetu MaltiarEmail: meetu@knoldus.com  Twitter:@meetumaltiar
Akka 2.0Akka name comes from Sami mythology is actually     name of a goddess of wisdom and beauty.    Akka incidentally m...
The ProblemIt is way too hard to build  => correct highly concurrent systems  => truly scalable systems  => self-healing, ...
What is Akka?Right abstraction with actors for concurrent, fault-tolerantand scalable applicationsFor Fault-Tolerance uses...
Introducing ActorsActor is an entity encapsulating behavior, state and amailbox to receive messagesFor a message received ...
Introducing Actors..There is elasticity between message processing and additionof new messages. New messages can be added ...
Create Applicationimport akka.actor.ActorSystemval system =ActorSystem("firstApp")
My First Actorimport akka.actor.{ Actor, Props }class MyFirstActor extends Actor {  def receive = {    case msg => println...
Create Actorsimport akka.actor.{ ActorSystem, Props }val system = ActorSystem("firstApp")val myFirstActor =system.actorOf(...
Stop Actorssystem stop myFirstActorAlso stops all actors in hierarchy
Send: !myFirstActor ! “Hello”      fire-forget
Ask: ?import akka.pattern.askimplicit val timeout = Timeout(50000 milliseconds)val future = myActor ? "hello"Await.result(...
Replyimport akka.actor.Actorclass LongWorkingActor extends Actor {  def receive = {    case number: Int =>      sender ! (...
RoutersRoundRobinRandomSmallestMailBoxBroadCastScatterGaherFirstCompleted
Routers...val router =system.actorOf(Props[RouterWorkerActor].withRouter(RoundRobinRouter(nrOfInstances = 5)))
Let It CrashFault Tolerance
Actor Pathval actorRef =system.actorFor("akka://actorPathApp/user/parent/child")val parent = context.actorFor("..")val sib...
Supervisionclass Supervisor extends Actor {  override val supervisorStrategy =OneForOneStrategy(maxNrOfRetries = 10,within...
Manage Failureclass FaultTolerantService extends Actor {  def receive = {    case msg => println(msg)  }    override def p...
Code Sampleshttps://github.com/meetumaltiar/AkkaKnolX
ReferencesViktor Klang talk on Akka 2.0 at NE Scala symposiumAkka website akka.io
Akka knolx
Akka knolx
Akka knolx
Akka knolx
Akka knolx
Akka knolx
Akka knolx
Akka knolx
Akka knolx
Akka knolx
Upcoming SlideShare
Loading in …5
×

Akka knolx

2,222
-1

Published on

KnolX session by Meetu Maltiar on new features in Akka 2.0.

Friday is KnolX day @Knoldus

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,222
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
23
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Akka knolx

  1. 1. 2.0 Reloaded Meetu MaltiarEmail: meetu@knoldus.com Twitter:@meetumaltiar
  2. 2. Akka 2.0Akka name comes from Sami mythology is actually name of a goddess of wisdom and beauty. Akka incidentally means sister in Telugu!!
  3. 3. The ProblemIt is way too hard to build => correct highly concurrent systems => truly scalable systems => self-healing, fault-tolerant systems
  4. 4. What is Akka?Right abstraction with actors for concurrent, fault-tolerantand scalable applicationsFor Fault-Tolerance uses “let it crash” modelAbstraction for transparent distribution for load
  5. 5. Introducing ActorsActor is an entity encapsulating behavior, state and amailbox to receive messagesFor a message received by Actor a thread is allocated to itThen Actors behavior is applied to the message andpotentially some state is changed or messages is passed toother Actors
  6. 6. Introducing Actors..There is elasticity between message processing and additionof new messages. New messages can be added while actorexecution is happening.When processing of messages is completed thread isdeallocated from the actor. It can be reallocated a thread ata later time
  7. 7. Create Applicationimport akka.actor.ActorSystemval system =ActorSystem("firstApp")
  8. 8. My First Actorimport akka.actor.{ Actor, Props }class MyFirstActor extends Actor { def receive = { case msg => println("Hello!!") }}
  9. 9. Create Actorsimport akka.actor.{ ActorSystem, Props }val system = ActorSystem("firstApp")val myFirstActor =system.actorOf(Props[MyFirstActor]) MyFirstActor is an ActorRef Create a top level actor
  10. 10. Stop Actorssystem stop myFirstActorAlso stops all actors in hierarchy
  11. 11. Send: !myFirstActor ! “Hello” fire-forget
  12. 12. Ask: ?import akka.pattern.askimplicit val timeout = Timeout(50000 milliseconds)val future = myActor ? "hello"Await.result(future, timeout.duration).asInstanceOf[Int] Returns a Future[Any]
  13. 13. Replyimport akka.actor.Actorclass LongWorkingActor extends Actor { def receive = { case number: Int => sender ! (“Hi I received ” + number) }}
  14. 14. RoutersRoundRobinRandomSmallestMailBoxBroadCastScatterGaherFirstCompleted
  15. 15. Routers...val router =system.actorOf(Props[RouterWorkerActor].withRouter(RoundRobinRouter(nrOfInstances = 5)))
  16. 16. Let It CrashFault Tolerance
  17. 17. Actor Pathval actorRef =system.actorFor("akka://actorPathApp/user/parent/child")val parent = context.actorFor("..")val sibling = context.actorFor("../sibling")val refPath = actorRef.path
  18. 18. Supervisionclass Supervisor extends Actor { override val supervisorStrategy =OneForOneStrategy(maxNrOfRetries = 10,withinTimeRange = 1 minute) { case _: ArithmeticException => Resume case _: NullPointerException => Restart case _: IllegalArgumentException => Stop case _: Exception => Escalate }}
  19. 19. Manage Failureclass 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 }}
  20. 20. Code Sampleshttps://github.com/meetumaltiar/AkkaKnolX
  21. 21. ReferencesViktor Klang talk on Akka 2.0 at NE Scala symposiumAkka website akka.io

×