Akka 2.0 is a toolkit for building highly concurrent, distributed, and fault-tolerant applications on the JVM. It provides actors as the core abstraction for concurrency and distribution. Actors encapsulate state and behavior and communicate asynchronously by message passing. Akka provides elasticity so that new messages can be processed while an actor is busy. It also includes features for fault tolerance using a "let it crash" model and transparent distribution through routing.
2. 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!!
3. The Problem
It is way too hard to build
=> correct highly concurrent systems
=> truly scalable systems
=> self-healing, fault-tolerant systems
4. 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
5. 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
6. 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
12. My First Actor
import akka.actor.{ Actor, Props }
class MyFirstActor extends Actor {
def receive = {
case msg => println("Hello!!")
}
}
13. Create Actors
import akka.actor.{ ActorSystem, Props }
val system = ActorSystem("firstApp")
val myFirstActor =
system.actorOf(Props[MyFirstActor])
MyFirstActor is an ActorRef
Create a top level actor
27. Actor Path
val actorRef =
system.actorFor("akka://actorPathApp/user/pa
rent/child")
val parent = context.actorFor("..")
val sibling = context.actorFor("../sibling")
val refPath = actorRef.path
28. 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
}
}
29. 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
}
}