Akka knolx

Post on 06-May-2015

2.225 views 13 download

description

KnolX session by Meetu Maltiar on new features in Akka 2.0. Friday is KnolX day @Knoldus

Transcript of Akka knolx

2.0 Reloaded

Meetu MaltiarEmail: meetu@knoldus.com

Twitter:@meetumaltiar

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!!

The Problem

It is way too hard to build

=> correct highly concurrent systems

=> truly scalable systems

=> self-healing, fault-tolerant systems

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

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

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

Create Application

import akka.actor.ActorSystem

val system = ActorSystem("firstApp")

My First Actorimport akka.actor.{ Actor, Props }

class MyFirstActor extends Actor { def receive = { case msg => println("Hello!!") }}

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])

Stop Actors

Also stops all actors in hierarchy

system stop myFirstActor

Send: !

fire-forget

myFirstActor ! “Hello”

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]

Replyimport akka.actor.Actor

class LongWorkingActor extends Actor { def receive = { case number: Int => sender ! (“Hi I received ” + number) }}

Routers

RoundRobin

Random

SmallestMailBox

BroadCast

ScatterGaherFirstCompleted

Routers...

val router = system.actorOf(Props[RouterWorkerActor].withRouter(RoundRobinRouter(nrOfInstances = 5)))

Let It CrashFault Tolerance

Actor Path

val actorRef = system.actorFor("akka://actorPathApp/user/parent/child")

val parent = context.actorFor("..")

val sibling = context.actorFor("../sibling")

val refPath = actorRef.path

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 }}

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 }}

Code Samples

https://github.com/meetumaltiar/AkkaKnolX

References

Viktor Klang talk on Akka 2.0 at NE Scala symposium

Akka website akka.io