Concurrency and scalability with akka

25

Transcript of Concurrency and scalability with akka

Page 1: Concurrency and scalability  with akka
Page 2: Concurrency and scalability  with akka
Page 3: Concurrency and scalability  with akka
Page 4: Concurrency and scalability  with akka
Page 5: Concurrency and scalability  with akka
Page 6: Concurrency and scalability  with akka

Akka is a toolkit and runtime for building highly concurrent, distributed, and resilient, message-driven applications on the JVM.

Page 7: Concurrency and scalability  with akka

Page 8: Concurrency and scalability  with akka

Page 9: Concurrency and scalability  with akka

import akka.actor.{ActorSystem, Props}

object Main { def main(args: Array[String]) {

val system = ActorSystem("mySystem") val props = Props[TestActor] val myActor = system.actorOf(props)

myActor ! HelloMessage }}

case class HelloMessage()

import akka.actor.Actor

class TestActor extends Actor{ override def receive: Receive = {

case HelloMessage => println("Hi everyone!")

case _ => print("It doesn't make sense!") }}

Page 10: Concurrency and scalability  with akka

import akka.actor.{ActorSystem, Props}

object Main { def main(args: Array[String]) {

val system = ActorSystem("mySystem") val props = Props[TestActor] val myActor = system.actorOf(props)

myActor ! HelloMessage }}

case class HelloMessage()

import akka.actor.UntypedActor;

public class TestActor extends UntypedActor { public void onReceive(Object message) throws Exception {

if (message instanceof HelloMessage) { System.out.println("Hi everyone!"); } else System.out.println("It doesn't make sense!"); }}

Page 11: Concurrency and scalability  with akka
Page 12: Concurrency and scalability  with akka

Page 13: Concurrency and scalability  with akka
Page 14: Concurrency and scalability  with akka
Page 15: Concurrency and scalability  with akka
Page 16: Concurrency and scalability  with akka

import akka.actor.{Actor, Props}

class Supervisor extends Actor { import akka.actor.OneForOneStrategy import akka.actor.SupervisorStrategy._ import scala.concurrent.duration._

override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: ArithmeticException => Resume case _: NullPointerException => Restart case _: IllegalArgumentException => Stop case _: Exception => Escalate }

def receive = { case p: Props => sender() ! context.actorOf(p) }}

import akka.actor.Actor

class Supervisee extends Actor {

var state = 0

def receive = { case ex: Exception => throw ex case x: Int => state = x case "get" => sender() ! state }}

Page 17: Concurrency and scalability  with akka
Page 18: Concurrency and scalability  with akka
Page 19: Concurrency and scalability  with akka

class ClusterListener extends Actor with ActorLogging {

val cluster = Cluster(context.system)

// subscribe to cluster changes, re-subscribe when restart override def preStart(): Unit = { cluster.subscribe(self, initialStateMode = InitialStateAsEvents, classOf[MemberEvent], classOf[UnreachableMember]) }

override def postStop(): Unit = cluster.unsubscribe(self)

def receive = { case MemberUp(member) => log.info("Member is Up: {}", member.address) case UnreachableMember(member) => log.info("Member detected as unreachable: {}", member) case MemberRemoved(member, previousStatus) => log.info("Member is Removed: {} after {}", member.address, previousStatus) case _: MemberEvent => // ignore }}

Page 20: Concurrency and scalability  with akka

akka { actor { provider = "akka.cluster.ClusterActorRefProvider" } remote { log-remote-lifecycle-events = off netty.tcp { hostname = "127.0.0.1" port = 0 } } cluster { seed-nodes = [ "akka.tcp://[email protected]:2551", "akka.tcp://[email protected]:2552"] }}

Page 21: Concurrency and scalability  with akka

val address = AddressFromURIString("akka.tcp://sys@host:1234") val ref = context.actorOf(Props[ClientSession]. withDeploy(Deploy(scope = RemoteScope(address))))

val selection = context.actorSelection("akka.tcp://[email protected]:2552/user/actorName")

selection ! "Hello from the other side"

Page 22: Concurrency and scalability  with akka

Page 23: Concurrency and scalability  with akka
Page 24: Concurrency and scalability  with akka
Page 25: Concurrency and scalability  with akka