1. Akka Actors And Futures
Meetu Maltiar
Principal Consultant
Email: meetu@knoldus.com
Twitter:@meetumaltiar
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
20. 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
21. Akka Futures
A Future is a data structure
Used to retrieve of some concurrent
operation
This operation is performed by an Actor or a
dispatcher directly
The result can be accessed synchronously or
asynchronously
22. Execution Context
Futures need ExecutionContext to execute
callback and operations
If we have ActorSystem in scope Future will
use default dispatcher as ExecutionContext
We can use factory methods provided by
ExecutionContext companion object to wrap
Executors and ExecutorServices
23. Use With Actors
There are two ways to get a reply from an
Actor. First one is (myActor ! Msg)
The second way is through a Future. Using
Actors “?” method will return a Future
The simplest way to use Await method call,
though not recommended as the thread blocks
till result is obtained.
24. Future With Akka
andAwait
import akka.actor._
import akka.pattern.ask
import akka.util.duration._
import akka.util.Timeout
import akka.dispatch.Await
object FutureWithAwaitApp extends App {
implicit val timeout = Timeout(50000 milliseconds)
val system = ActorSystem("future")
val echoActor = system.actorOf(Props[EchoActor])
val future = echoActor ? "Hello World"
val result = Await.result(future,
timeout.duration).asInstanceOf[String]
println(result)
}
25. Use Futures Directly
import akka.dispatch._
import akka.util.duration._
import akka.actor.ActorSystem
object MonadicFutureApplication extends App {
implicit val system = ActorSystem("future")
val f1 = Future { "Hello" + "World" }
val f2 = f1 map { x => x.length }
val result = Await.result(f2, 1 second)
println(result)
}
26. Composing Futures
object MultiMonadicFutureApplication extends App {
implicit val system = ActorSystem("future")
val f1 = Future { "Hello" + "World" }
val f2 = Future { 3 }
val f3 = f1 flatMap { x =>
f2 map { y =>
x.length * y
}
}
val result = Await.result(f3, 1 second)
}