Skip to content

Instantly share code, notes, and snippets.

@DanielaSfregola
Last active November 22, 2015 09:31
Show Gist options
  • Save DanielaSfregola/6dc52bffa2ed566de9b2 to your computer and use it in GitHub Desktop.
Save DanielaSfregola/6dc52bffa2ed566de9b2 to your computer and use it in GitHub Desktop.
Scala Italy 2015: An Introduction to Akka and the Actor-Based Model: Demo. See article http://danielasfregola.com/2015/05/11/scala-italy-2015-highlights/
import akka.actor._
class SimpleActor extends Actor {
def receive = {
case msg => println(s"${self.path} - $msg")
}
}
/** Main **/
val system = ActorSystem("intro-akka-demo")
val simpleActor = system.actorOf(Props[SimpleActor], name = "simple")
simpleActor ! "Yo! This is a message"
// akka://intro-akka-demo/user/simple - Yo! This is a message
simpleActor ! List(1,2,3)
// akka://intro-akka-demo/user/simple - List(1, 2, 3)
import akka.actor._
case class Person(name: String)
class HelloActor extends Actor {
import context._
def receive = inEnglish
def inEnglish: Receive = {
case p: Person => {
println(s"${self.path} - Hello ${p.name}!")
become(inItalian)
}
}
def inItalian: Receive = {
case p: Person => {
println(s"${self.path} - Ciao ${p.name}!")
become(inEnglish)
}
}
}
/** Main **/
val system = ActorSystem("intro-akka-demo")
val helloActor = system.actorOf(Props[HelloActor], name = "hello")
helloActor ! "This should be ignored"
helloActor ! Person("Martin")
// akka://intro-akka-demo/user/hello - Hello Martin
helloActor ! Person("Scala Italy")
// akka://intro-akka-demo/user/hello - Ciao Scala Italy!
helloActor ! Person("Daniela")
// akka://intro-akka-demo/user/hello - Hello Daniela!
import akka.actor._
import akka.actor.SupervisorStrategy._
case class Person(name: String)
import akka.actor.{Actor, OneForOneStrategy, Props}
class HelloActor extends Actor {
import context._
override def preRestart(reason: Throwable, message: Option[Any]) = {
println(s"${self.path} - Yo, I am restarting...")
super.preRestart(reason, message)
}
override def postRestart(reason: Throwable) = {
println(s"${self.path} -...restart completed!")
super.postRestart(reason)
}
def receive = inEnglish orElse crashIfUnknown
def inEnglish: Receive = {
case p: Person => {
println(s"${self.path} - Hello ${p.name}!")
become(inItalian orElse crashIfUnknown)
}
}
def inItalian: Receive = {
case p: Person => {
println(s"${self.path} - Ciao ${p.name}!")
become(inEnglish orElse crashIfUnknown)
}
}
def crashIfUnknown: Receive = {
case _ => throw new RuntimeException("BOOM!")
}
}
class HelloActorSupervisor extends Actor {
override def supervisorStrategy = OneForOneStrategy() {
case _: RuntimeException => Restart
}
val helloActor = context.actorOf(Props[HelloActor], "hello")
override def receive: Receive = {
case msg => helloActor forward msg
}
}
/** Main **/
val system = ActorSystem("intro-akka-demo")
val helloSupervisor = system.actorOf(Props[HelloActorSupervisor], name = "hello-supervisor")
helloSupervisor ! Person("Martin")
// akka://intro-akka-demo/user/hello-supervisor/hello - Hello Martin
helloSupervisor ! Person("Scala Italy")
// akka://intro-akka-demo/user/hello-supervisor/hello - Ciao Scala Italy!
helloSupervisor ! Person("Daniela")
// akka://intro-akka-demo/user/hello-supervisor/hello - Hello Daniela!
helloSupervisor ! "This should crash the actor"
// akka://intro-akka-demo/user/hello-supervisor/hello - Yo, I am restarting...
// akka://intro-akka-demo/user/hello-supervisor/hello -...restart completed!
// ERROR [OneForOneStrategy]: BOOM!
// java.lang.RuntimeException: BOOM!
// at HelloActor$$anonfun$crashIfUnknown$1.applyOrElse(demo-part3.scala:51) ~[classes/:na]
// at scala.PartialFunction$OrElse.applyOrElse(PartialFunction.scala:171) ~[scala-library-2.11.5.jar:na]
// at akka.actor.Actor$class.aroundReceive(Actor.scala:465) ~[akka-actor_2.11-2.3.9.jar:na]
// at HelloActor.aroundReceive(demo-part3.scala:21) ~[classes/:na]
// at akka.actor.ActorCell.receiveMessage(ActorCell.scala:516) [akka-actor_2.11-2.3.9.jar:na]
// at akka.actor.ActorCell.invoke(ActorCell.scala:487) [akka-actor_2.11-2.3.9.jar:na]
// at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:254) [akka-actor_2.11-2.3.9.jar:na]
// at akka.dispatch.Mailbox.run(Mailbox.scala:221) [akka-actor_2.11-2.3.9.jar:na]
// at akka.dispatch.Mailbox.exec(Mailbox.scala:231) [akka-actor_2.11-2.3.9.jar:na]
// at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.5.jar:na]
// at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.5.jar:na]
// at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.5.jar:na]
// at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.5.jar:na]
helloSupervisor ! Person("Martin")
// akka://intro-akka-demo/user/hello-supervisor/hello - Hello Martin!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment