Concurrency and scalability with akka
-
Upload
bardia-heydari-nejad -
Category
Engineering
-
view
102 -
download
4
Transcript of Concurrency and scalability with akka
![Page 1: Concurrency and scalability with akka](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/1.jpg)
![Page 2: Concurrency and scalability with akka](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/2.jpg)
![Page 3: Concurrency and scalability with akka](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/3.jpg)
![Page 4: Concurrency and scalability with akka](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/4.jpg)
![Page 5: Concurrency and scalability with akka](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/5.jpg)
![Page 6: Concurrency and scalability with akka](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/7.jpg)
●
●
●
●
![Page 8: Concurrency and scalability with akka](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/8.jpg)
●
●
●
![Page 9: Concurrency and scalability with akka](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/9.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/11.jpg)
![Page 12: Concurrency and scalability with akka](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/12.jpg)
●
●
●
●
●
●
●
●
●
![Page 13: Concurrency and scalability with akka](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/13.jpg)
![Page 14: Concurrency and scalability with akka](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/14.jpg)
![Page 15: Concurrency and scalability with akka](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/15.jpg)
![Page 16: Concurrency and scalability with akka](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/16.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/17.jpg)
![Page 18: Concurrency and scalability with akka](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/18.jpg)
![Page 19: Concurrency and scalability with akka](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/19.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/20.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/21.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/22.jpg)
●
●
●
●
●
●
![Page 23: Concurrency and scalability with akka](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/23.jpg)
![Page 24: Concurrency and scalability with akka](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/24.jpg)
![Page 25: Concurrency and scalability with akka](https://reader034.fdocuments.in/reader034/viewer/2022042605/58f16cac1a28abc51f8b45fd/html5/thumbnails/25.jpg)